Fer*_*uzz 4 python plot matplotlib
Axes3D的bar3d函数有一个'color'参数,该参数可以接受数组以对各个条形进行不同颜色的着色-但是我将如何以与plot_surface函数相同的方式应用颜色图(即cmap = cm.jet)?这将使一定高度的条成为反映其高度的颜色。
http://matplotlib.sourceforge.net/examples/mplot3d/hist3d_demo.html
http://matplotlib.sourceforge.net/mpl_toolkits/mplot3d/api.html
小智 5
遵循Ferguzz提供的答案,这是一个更完整/最新的解决方案:
import matplotlib.colors as colors
import matplotlib.cm as cm
dz = height_values
offset = dz + np.abs(dz.min())
fracs = offset.astype(float)/offset.max()
norm = colors.Normalize(fracs.min(), fracs.max())
color_values = cm.jet(norm(fracs.tolist()))
ax.bar3d(xpos,ypos,zpos,1,1,dz, color=color_values)
Run Code Online (Sandbox Code Playgroud)
请注意以下几点:
您应该定义所有变量(例如xpos,ypos),类似于https://matplotlib.org/examples/pylab_examples/hist_colormapped.html中的代码
normalize()现在是Normalize()
fracs是Series类型(来自pandas),必须转换为list
这是我的解决方案:
offset = dz + np.abs(dz.min())
fracs = offset.astype(float)/offset.max()
norm = colors.normalize(fracs.min(), fracs.max())
colors = cm.jet(norm(fracs))
ax.bar3d(xpos,ypos,zpos,1,1,dz, color=colors)
Run Code Online (Sandbox Code Playgroud)
仅当您的数据变为负数时才需要第一行。
代码改编自此处http://matplotlib.sourceforge.net/examples/pylab_examples/hist_colormapped.html。
| 归档时间: |
|
| 查看次数: |
5348 次 |
| 最近记录: |