在 matplot lib 中格式化 3d 条形图 [英] Formatting a 3d bar plot in matplot lib

查看:37
本文介绍了在 matplot lib 中格式化 3d 条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理这个代码示例

from mpl_toolkits.mplot3d 导入 Axes3D导入 matplotlib.pyplot 作为 plt将 numpy 导入为 np数据 = np.array([[0,1,0,2,0],[0,3,0,2,0],[6,1,1,7,0],[0,5,0,2,9],[0,1,0,4,0],[9,1,3,4,2],[0,0,2,1,3],])column_names = ['a','b','c','d','e']row_names = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']fig = plt.figure()ax = Axes3D(图)lx= len(data[0]) # 计算矩阵维度ly=len(数据[:,0])xpos = np.arange(0,lx,1) # 设置位置网格ypos = np.arange(0,ly,1)xpos, ypos = np.meshgrid(xpos+0.25, ypos+0.25)xpos = xpos.flatten() # 将位置转换为一维数组ypos = ypos.flatten()zpos = np.zeros(lx*ly)dx = 0.5 * np.ones_like(zpos)dy = dx.copy()dz = data.flatten()ax.bar3d(xpos,ypos,zpos, dx, dy, dz, color='b')#sh()ax.w_xaxis.set_ticklabels(column_names)ax.w_yaxis.set_ticklabels(row_names)ax.set_xlabel('信')ax.set_ylabel('天')ax.set_zlabel('发生次数')plt.show()

并且我已经成功地根据我的需要调整了它,现在我需要更改每列的颜色以使我的数据更具可读性,如另一个示例所示

I am working with this code example

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

data = np.array([
[0,1,0,2,0],
[0,3,0,2,0],
[6,1,1,7,0],
[0,5,0,2,9],
[0,1,0,4,0],
[9,1,3,4,2],
[0,0,2,1,3],
])

column_names = ['a','b','c','d','e']
row_names = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']

fig = plt.figure()
ax = Axes3D(fig)

lx= len(data[0])            # Work out matrix dimensions
ly= 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()

ax.bar3d(xpos,ypos,zpos, dx, dy, dz, color='b')

#sh()
ax.w_xaxis.set_ticklabels(column_names)
ax.w_yaxis.set_ticklabels(row_names)
ax.set_xlabel('Letter')
ax.set_ylabel('Day')
ax.set_zlabel('Occurrence')

plt.show()

and I have successfully adapted it to my needs now I need to change the colours for each column to make my data more readable as shown in this other example http://matplotlib.org/examples/mplot3d/bars3d_demo.html but as the graphs are constructed in totally different ways I cannot figure out how to apply one to the other

解决方案

basically, you just pass an array of colors to color=. Each element of the array is one of your bars. So depending on how you construct your array, you can group your bars by columns or by rows.

for example:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

data = np.array([
[0,1,0,2,0],
[0,3,0,2,0],
[6,1,1,7,0],
[0,5,0,2,9],
[0,1,0,4,0],
[9,1,3,4,2],
[0,0,2,1,3],
])

column_names = ['a','b','c','d','e']
row_names = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']

fig = plt.figure()
ax = Axes3D(fig)

lx= len(data[0])            # Work out matrix dimensions
ly= 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()

cs = ['r', 'g', 'b', 'y', 'c'] * ly

ax.bar3d(xpos,ypos,zpos, dx, dy, dz, color=cs)

#sh()
ax.w_xaxis.set_ticklabels(column_names)
ax.w_yaxis.set_ticklabels(row_names)
ax.set_xlabel('Letter')
ax.set_ylabel('Day')
ax.set_zlabel('Occurrence')

plt.show()

这篇关于在 matplot lib 中格式化 3d 条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆