[英]Change bar color in a 3D bar plot in matplotlib based on value
I have a 3D bar plot in matplotlib which consists of a total 165 bars and at the moment it is quite chaotic. 我在matplotlib中有一个3D条形图,它总共有165个条形图,目前它非常混乱。
I would like to change the colour of the bars based on the discreet z-values: 0,1,2. 我想根据谨慎的z值更改条形的颜色:0,1,2。
I know there is the option to change colour bar in 1D bar plots based on specific values by using masks as in Color matplotlib bar chart based on value . 我知道可以选择根据特定值更改一维条形图中的颜色条,方法是使用蒙版,如基于值的颜色matplotlib条形图 。
And there is also a question on how to change bar colour based on values: Defining colors of Matplotlib 3D bar plot 还有一个关于如何根据值更改条形颜色的问题: 定义Matplotlib 3D条形图的颜色
I am not sure If i perfectly comprehend the given answer but I cannot make it work in this case. 我不确定如果我完全理解给定的答案,但我无法在这种情况下使其工作。
Code is: 代码是:
data = [[0 0 0 2 0 0 1 2 0 0 0]
[0 0 2 2 0 0 0 0 2 0 0]
[1 0 2 2 1 2 0 0 2 0 2]
[1 0 2 2 0 2 0 2 2 2 2]
[2 2 2 2 2 2 2 2 2 2 2]
[2 2 0 2 2 2 2 2 2 2 2]
[0 2 2 0 2 2 2 2 2 2 2]
[1 2 0 0 2 1 2 2 0 0 2]
[0 0 2 1 0 0 2 0 0 0 0]
[2 1 2 2 0 0 0 2 0 0 2]
[2 2 2 0 2 0 0 0 2 2 2]
[2 2 0 0 2 2 2 2 2 0 0]
[2 2 1 2 0 0 0 2 2 2 0]
[2 0 0 2 0 0 2 2 2 2 2]
[2 0 0 2 0 2 2 2 2 2 2]]
ly = len(data[0])
lx = len(data[:,0])
xpos = np.arange(0,lx,1) # Set up a mesh of positions
ypos = np.arange(0,ly,1)
xpos, ypos = np.meshgrid(xpos+0.25, ypos+0.25)
xpos = xpos.flatten() # Convert positions to 1D array
ypos = ypos.flatten()
zpos = np.zeros(lx*ly)
dx = 0.5 * np.ones_like(zpos)
dy = dx.copy()
dz = data.flatten()
ys = np.array([float(yi) for yi in y[1:]])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# all blue bars
#ax.bar3d(xpos,ypos,zpos, dx, dy, dz, color='b')
# try changing color bars
colors = ['r','g','b']
for i in range(0,3):
ax.bar3d(xpos[i], ypos[i], zpos[i], dx, dy, dz[i], alpha=0.1,
color=colors[i])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
As seen from the documentation of bar3d , color
can be an array, with one color per bar. 从bar3d的文档中可以看出 , color
可以是一个数组,每个条带有一种颜色。
This makes it quite easy to colorize all bars in a single call to bar3d
; 这使得在一次调用bar3d
时很容易为所有条形图着色; we just need to convert the data
array to an array of colors which can be done using a colormap, 我们只需要将data
数组转换为可以使用色彩映射完成的颜色数组,
colors = plt.cm.jet(data.flatten()/float(data.max()))
(Note, that a colormap takes values between 0 and 1, so we need to normalize the values into this range.) (注意,colormap的值介于0和1之间,因此我们需要将值标准化为此范围。)
Complete example: 完整的例子:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
data = np.array([ [0, 0, 0, 2, 0, 0, 1, 2, 0, 0, 0],
[0, 0, 2, 2, 0, 0, 0, 0, 2, 0, 0],
[1, 0, 2, 2, 1, 2, 0, 0, 2, 0, 2],
[1, 0, 2, 2, 0, 2, 0, 2, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2],
[0, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2],
[1, 2, 0, 0, 2, 1, 2, 2, 0, 0, 2],
[0, 0, 2, 1, 0, 0, 2, 0, 0, 0, 0],
[2, 1, 2, 2, 0, 0, 0, 2, 0, 0, 2],
[2, 2, 2, 0, 2, 0, 0, 0, 2, 2, 2],
[2, 2, 0, 0, 2, 2, 2, 2, 2, 0, 0],
[2, 2, 1, 2, 0, 0, 0, 2, 2, 2, 0],
[2, 0, 0, 2, 0, 0, 2, 2, 2, 2, 2],
[2, 0, 0, 2, 0, 2, 2, 2, 2, 2, 2]])
ypos, xpos = np.indices(data.shape)
xpos = xpos.flatten()
ypos = ypos.flatten()
zpos = np.zeros(xpos.shape)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
colors = plt.cm.jet(data.flatten()/float(data.max()))
ax.bar3d(xpos,ypos,zpos, .5,.5,data.flatten(), color=colors)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.