开发者

Data plotting in boxes with python

I have an array

  d[0:100]

which is defined in boxes (or cells) whose center are stored in these two arrays

  x[0:100]
  y[0:100]

and whose size is

  h[0:100]

I wish I could plot an image showing boxes/cells colored according to the value of the array defined in each of them. It seems and histogram but it i开发者_如何学Pythons not. Do you have any idea where to start?


Given the desire to draw squares with area h**2 and a color based on the value in d, you could draw rectangles using matplotlib with a color obtained from a colormap (scaled to 0-1):

import pylab
import numpy as np
import matplotlib as mpl
import random
import matplotlib.cm as cm

my_cmap = cm.bone

def my_square_scatter(axes, x_array, y_array, size_array, color_array):
    for x, y, size, color in zip(x_array, y_array, size_array, color_array):
        square = pylab.Rectangle((x-size/2,y-size/2), size, size, facecolor = my_cmap(color))
        axes.add_patch(square)
    return True

x = np.arange(100)
y = np.arange(100)
random.shuffle(y)
h = np.arange(100)/10.0
d = np.arange(100)/100.0
random.shuffle(d)

fig = pylab.figure(1)
fig.clf()
axes = pylab.axes()
my_square_scatter(axes, x, y, h, d)
pylab.axis('scaled')

#Create your own colorbar based on the parent axes.
ax, _ = mpl.colorbar.make_axes(axes)
cbar = mpl.colorbar.ColorbarBase(ax, cmap=my_cmap, norm=mpl.colors.Normalize(vmin=0.0, vmax=1.0))
#cbar.set_clim(0.0,1.0) #Scale the colorbar; default is 0--1


pylab.show()

Sample output:

Data plotting in boxes with python

Your d array should be normalized to 0-1. Otherwise you should scale this in the color selection.

Adapted from Plot/scatter position and marker size in the same coordinates.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜