定义 Matplotlib 3D 条形图的颜色 [英] Defining colors of Matplotlib 3D bar plot
问题描述
我想不出在我的 iPython 笔记本中的 matplotlib 中为 3d 条形图设置 cmap(或颜色)的正确方法.我可以在 X 和 Y 平面中正确设置我的图表(28 x 7 标签),并带有一些随机的 Z 值.该图很难解释,原因之一是 x_data 标签 [1,2,3,4,5] 的默认颜色都相同.
I can't figure out the right way to set a cmap (or colors) for a 3d bar plot in matplotlib in my iPython notebook. I can setup my chart correctly (28 x 7 labels) in the X and Y plane, with some random Z values. The graph is hard to interpret, and one reason is that the default colors for the x_data labels [1,2,3,4,5] are all the same.
代码如下:
%matplotlib inline
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as npfig = plt.figure(figsize=(18,12))
ax = fig.add_subplot(111, projection='3d')
x_data, y_data = np.meshgrid(np.arange(5),np.arange(3))
z_data = np.random.rand(3,5).flatten()
ax.bar3d(x_data.flatten(),
y_data.flatten(),np.zeros(len(z_data)),1,1,z_data,alpha=0.10)
产生以下图表:
我不想为标签 x_data 手动定义颜色.如何为 x_data 中的每个标签设置不同的随机"cmap 颜色,同时保持
I don't want to define the colors manually for the labels x_data. How can I set up different 'random' cmap colors for each of the labels in x_data, still keeping the
ax.bar3d
参数?下面是使用
ax.bar
和不同的颜色,但我需要的是ax.bar3d.
and different colors, but what I need is ax.bar3d.
推荐答案
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(18,12))
ax = fig.add_subplot(111, projection='3d')
x_data, y_data = np.meshgrid(np.arange(5),np.arange(3))
z_data = np.random.rand(3,5)
colors = ['r','g','b'] # colors for every line of y
# plot colored 3d bars
for i in xrange(3): # cycle though y
# I multiply one color by len of x (it is 5) to set one color for y line
ax.bar3d(x_data[i], y_data[i], z_data[i], 1, 1, z_data[i], alpha=0.1, color=colors[i]*5)
# or use random colors
# ax.bar3d(x_data[i], y_data[i], z_data[i], 1, 1, z_data[i], alpha=0.1, color=[np.random.rand(3,1),]*5)
plt.show()
结果:
这篇关于定义 Matplotlib 3D 条形图的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!