如何使用matplot lib

时间:2017-05-14 13:51:27

标签: python matplotlib matplotlib-basemap mplot3d

enter image description here如何调整条形图条形的宽度。此帖子附有图片,显示实际情况。从2011年到2015年,我们可以看到显示年份的标签。

我找到了一种解决方案,以减少沿y轴的各个条的宽度。我尝试了许多随机解决方案,这些解决方案可以在线使用,但没有任何工作。

给定的图像实际上是3D图形,但由于清晰的视图,仅显示了2D图像。

请帮助:

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

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

#data = data.transpose()

row_names = ['Ch','Bi','Ba','Ho','Ja','Pu','N','Ga','Ke','Ma','Me','Ti','Ka','Th','Di','Wa','Ha','My','Tu','Ma','Bi','Ut Ka','Ra','Da','Ch','Ko','B','Bid','Gu','D&N','Ra','Pu','San','Dhu','Jal','Gon','Gand','Alw','Jal','SAS','Gur','Pat','Hos','Lu','Pan','Kur','Jan']


column_names = ["2011","2012","2013","2014","2015"]



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


xpos = np.arange(0,lx,1)    
ypos = np.arange(0,ly,1)
xpos, ypos = np.meshgrid(xpos+0.5, ypos+0.5)

xpos = xpos.flatten()  
ypos = ypos.flatten()
zpos = np.ones(lx*ly)*1e-10

dx = 1. * np.ones_like(zpos)
dy = dx.copy()
dz = data.flatten()
print dz
cs = ['r', 'g', 'b', 'y', 'c'] * ly

values = np.linspace(0.2, 1., xpos.ravel().shape[0])
colors = cm.rainbow(values)

ticksx = np.arange(1, len(row_names)+1,1)
plt.xticks(ticksx, row_names)

z_data = np.random.rand(0,10)

ticksy = np.arange(0.5, len(column_names),1)
plt.yticks(ticksy, column_names)

#ax.tick_params(width=2, colors='r')



ax.w_xaxis.set_ticklabels(row_names,rotation=90,size=12)

ax.w_yaxis.set_ticklabels(column_names,rotation=40,size=12)

ax.bar3d(xpos,ypos,zpos, dx, dy, dz,   alpha=0.7, zsort='max',color=colors)
plt.ion()

plt.show()
raw_input(" " )

更新 将“dx”值减少1到0.5,减小了条宽,但现在它显示了我想要完全移除的条之间的巨大填充。

enter image description here

上次更新: 我还附上了一张图片。我期待这种3D条形图减小条宽(完成),并最小化条之间的间隙。

enter image description here

1 个答案:

答案 0 :(得分:3)

来电签名或bar3d

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

其中dxdy是条形的宽度和深度。因此,通过向dx提供不同的数组或数字来更改宽度。例如。要有一个0.5宽的条,请使用

ax.bar3d(..., dx=0.5, ...)

如果您想改变轴的宽度,可以查看this question。在这里实施此解决方案,

ax.bar3d(xpos,ypos,zpos, 1, 1, dz,   alpha=0.7, zsort='max',color=colors)
ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([1,0.4, 1, 1]))

会给:

enter image description here