根据第3个值(无散点图)在绘图中使用自己的色彩映射

时间:2013-06-03 15:32:24

标签: python matplotlib plot

我为matplotlib创建了自己的colormap,我可以将它用于散点图(由于堆栈溢出)。
如何在普通的绘图函数(plt.plot())中使用它。它不接受“cmap”参数 我想绘制x,y值并使用我的色彩图根据z值对它们进行着色,并根据z值设置不同的标记。

1 个答案:

答案 0 :(得分:2)

没有内置函数可以做你想要的,所以我写了一个python脚本来回答你的问题(评论解释代码):

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm


fig = plt.figure()

width=4.467    # width in inches of my figure
height=4.344   # height in inches of my figure
DPI=90         # dots per inch

xmin=0
xmax=2*np.pi
xx_step=1.0*(xmax-xmin)/(width*DPI)
print "xx_step:", xx_step
xx=np.arange(xmin, xmax+xx_step, xx_step)

ymin=-1
ymax=1
yy_step=1.0*(ymax-ymin)/(height*DPI)
print "yy_step:", yy_step
yy=np.arange(ymin, ymax+yy_step, yy_step)

x_surf, y_surf = np.meshgrid(xx, yy)                   # generate a mesh
z_surf = np.zeros(x_surf.shape)+float('+inf')          # to paint with white color the regions that will not contain the function's curve (the default value depends on your colormap)

y_dots=np.sin(xx)
colors=[i for i in range(len(xx))]                     # colors of each point of the function, in this example they vary linearly

def find(my_list, func):
    # this find() method is from:
    # http://stackoverflow.com/questions/5957470/matlab-style-find-function-in-python
    return [j for (j, value) in enumerate(my_list) if func(value)]

def insert_dot(x, y, color):
    global z_surf
    def insert_except_at_contours(x, y, color):
        try:
            z_surf[y, x]=color
        except:
            pass
    insert_except_at_contours(x-1, y, color)
    insert_except_at_contours(x, y-1, color)
    insert_except_at_contours(x, y, color)
    insert_except_at_contours(x, y+1, color)
    insert_except_at_contours(x+1, y, color)

flag=0
delta_y=0
for i in range(len(xx)):   # for each pair of values in xx and the corresponding function that is stored in y_dots, find the corresponding indices in the mesh
    xx_index=i
    cur_array=abs(y_dots[i]-yy)
    cmp_test=min(cur_array)
    yy_index=find(cur_array, lambda x: (x==cmp_test))[0]
    if not flag:
        insert_dot(xx_index, yy_index, colors[i])
        flag=1
    else:       # this "else" part (and the corresponding flag) paints a straight line between the given dots, to improve the resolution (optional).
        delta_y=np.sign(last_yy_index-yy_index)
        if delta_y==0:
            delta_y=1
            last_yy_index+=1
        for j in range(yy_index, last_yy_index, delta_y):
            insert_dot(xx_index, j, colors[i])
    last_yy_index=yy_index

print "in pcolor()..."
obj=plt.pcolor(x_surf, y_surf, z_surf, cmap=cm.hot)     # plot the graph
print "done!"
plt.clim(min(colors), max(colors))
fig.colorbar(obj)

axis=plt.gca()
axis.set_xlim([min(xx), max(xx)])
axis.set_ylim([min(yy), max(yy)])

plt.show()

结果:

http://s14.postimg.org/tt44e4uqp/graph_CM.png