# Individual custom markers and colorbar

Hi all,

I am trying to get a scatter plot using a colormap. Additionally, I need to define every marker for every data point individually – each being a rectangle with fixed height but varying width as a function of the y-value. X and y being the data coordinates, z being a number to be color coded with the colormap.

Ideally, I would like to create a list of width and height values for each data point and tell the scatter plot to use those.

So far I got colormapped data with custom markers (simplified):

``````
import numpy as np

import matplotlib.pyplot as plt

from pylab import *

x = y = [1,2,3,4,5]

z = [2,4,6,8,10]

colors = cm.gnuplot2

verts_vec = list(zip([-10.,10.,10.,-10.],[-5.,-5.,5.,5.]))

fig = plt.figure(1, figsize=(14.40, 9.00))

sc = ax.scatter(x, y, c=np.asarray(z), marker=None, edgecolor='None', verts=verts_vec, cmap=colors, alpha=1.)

plt.colorbar(sc, orientation='horizontal')

plt.savefig('test.png', dpi=200)

plt.close(1)
``````

But I need to define a marker size for each point, and I also need to do that in axis scale values, not in points.

I imagine giving verts a list of N*2 tuples instead of 2 tuples, N being len(x), to define N individual markers.

But when doing that I get the error that vertices.ndim==2.

A less elegant way would be to plot every data point in an individual scatter plot function, using a for-loop iterating over all data points. Then, however, I see no way to apply a colormap and colorbar.

What is the best way to accomplish that then?

Thanks,

-Hackstein

Hackstein,

Unfortunately, I'm not sure of an 'elegant' way to do what your asking with a single call to scatter. Others may know a better way. However, you can use rectangle patches and patch collections. (Requires a bit more code than scatter but is ultimately more flexible.)

I think the example below does what you need, but with random numbers.

Hope it helps a little.

Ryan

···

#######################
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection

n = 100

# Get your xy data points, which are the centers of the rectangles.
xy = np.random.rand(n,2)

# Set a fixed height
height = 0.02
# The variable widths of the rectangles
widths = np.random.rand(n)*0.1

# Get a color map and color values (normalized between 0 and 1)
cmap = plt.cm.jet
colors = np.random.rand(n)

rects = []
for p, w, c in zip(xy, widths, colors):
xpos = p - w/2 # The x position will be half the width from the center
ypos = p - height/2 # same for the y position, but with height
rect = Rectangle( (xpos, ypos), w, height ) # Create a rectangle
rects.append(rect) # Add the rectangle patch to our list

# Create a collection from the rectangles
col = PatchCollection(rects)
# set the alpha for all rectangles
col.set_alpha(0.3)
# Set the colors using the colormap
col.set_facecolor( cmap(colors) )

# Make a figure and add the collection to the axis.
ax = plt.subplot(111)
plt.show()

###############################

On 4/24/2013 5:35 PM, Hackstein wrote:

Hi all,

I am trying to get a scatter plot using a colormap. Additionally, I need to define every marker for every data point individually -- each being a rectangle with fixed height but varying width as a function of the y-value. X and y being the data coordinates, z being a number to be color coded with the colormap.

Ideally, I would like to create a list of width and height values for each data point and tell the scatter plot to use those.

So far I got colormapped data with custom markers (simplified):

[code]

import numpy as np

import matplotlib.pyplot as plt

from pylab import *

x = y = [1,2,3,4,5]

z = [2,4,6,8,10]

colors = cm.gnuplot2

verts_vec = list(zip([-10.,10.,10.,-10.],[-5.,-5.,5.,5.]))

fig = plt.figure(1, figsize=(14.40, 9.00))

sc = ax.scatter(x, y, c=np.asarray(z), marker=None, edgecolor='None', verts=verts_vec, cmap=colors, alpha=1.)

plt.colorbar(sc, orientation='horizontal')

plt.savefig('test.png', dpi=200)

plt.close(1)

[/code]

But I need to define a marker size for each point, and I also need to do that in axis scale values, not in points.

I imagine giving verts a list of N*2 tuples instead of 2 tuples, N being len(x), to define N individual markers.

But when doing that I get the error that vertices.ndim==2.

A less elegant way would be to plot every data point in an individual scatter plot function, using a for-loop iterating over all data points. Then, however, I see no way to apply a colormap and colorbar.

What is the best way to accomplish that then?

Thanks,

-Hackstein

------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_apr

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users