【问题标题】:Using matplotlib colorbar/colorbarbase with point data将 matplotlib colorbar/colorbarbase 与点数据一起使用
【发布时间】:2015-12-18 22:21:35
【问题描述】:

所以我需要使用“z 轴”值创建带有设定点和颜色的 2D 图,就像您看到的那样,这不是问题。

我现在需要的是在右侧添加具有全色阶的颜色条。我试过这样做,但没有效果:

x=numpy.loadtxt('h2o.dat',unpack=True)
shiftposition(x,-45.1*prze,266.67)
vmin=x[0].min()
vmax=x[0].max()
dv=vmax-vmin

cmap = cm.jet
colors = matplotlib.colors.Normalize(vmin+0.0, vmax+0.0)


fig, ax = plt.subplots()


plt.arrow(-46,345, 100*math.sin(math.radians(40)), 100*math.cos(math.radians(40)), head_width=20, head_length=20, fc='r', ec='r',color='r')
plt.arrow(25,163, 100*math.sin(math.radians(125)), 100*math.cos(math.radians(125)), head_width=20, head_length=20, fc='r', ec='r',color='r')
plt.arrow(-280,342, 100*math.sin(math.radians(-55)), 100*math.cos(math.radians(-55)), head_width=20, head_length=20, fc='b', ec='b',color='b')
plt.arrow(-233,120, 100*math.sin(math.radians(-140)), 100*math.cos(math.radians(-140)), head_width=20, head_length=20, fc='b', ec='b',color='b')

plt.scatter(-20*prze,242.8,marker='+',s=50,color='k')
ax.scatter(x[1],x[2],marker='^',color=cm.jet((x[0]-vmin)/dv,1),s=50,zorder=3)
cax = matplotlib.colorbar.ColorbarBase(ax,  cmap=cmap,norm=colors,orientation='vertical',spacing='uniform',ticklocation='auto')
ax.set_xlim(-500,250)
ax.set_ylim(-200,550)
ax.invert_xaxis()
plt.show()

我得到的是:

我做错了什么?

【问题讨论】:

  • 我没有看到plt.colorbar函数,matplotlib.org/examples/pylab_examples/…
  • 我觉得你没有将颜色条添加到你的情节中。 fig.colorbar(args)
  • 如果我理解正确 plt.colorbar 是用于 ScalarMappable 的图像和等高线图,在我的情况下它没有用......并且 colorbar.ColorbarBase 应该更通用,但它不适用于我和我不知道为什么。

标签: python matplotlib


【解决方案1】:

如果您设置 cmap 的 scatter 并将颜色设置为值数组,而不是调用您现在拥有的颜色图,则可以执行此操作。

例如:

import matplotlib.pyplot as plt
import numpy as np

x=y=z=np.linspace(0,10,11)

fig,ax = plt.subplots()

p=ax.scatter(x,y,c=z,cmap='viridis',marker='^',edgecolor='None',s=200)

fig.colorbar(p,ax=ax)

plt.show()

对于您的示例,我想应该这样做:

p = ax.scatter(x[1],x[2],marker='^',color=(x[0]-vmin)/dv,s=50,zorder=3,cmap='jet')
fig.colorbar(p,ax=ax)

【讨论】:

    猜你喜欢
    • 2021-12-26
    • 2019-06-25
    • 2022-08-06
    • 2015-04-19
    • 1970-01-01
    • 2016-06-20
    • 2013-03-07
    • 2011-09-23
    • 1970-01-01
    相关资源
    最近更新 更多