我正在尝试使用3D条形图绘制该数据集
I am trying to plot this data set using 3D bar
B A freq 1 2003 2 1 2003 2 2 2008 1 2 2007 2 2 2007 2 3 2004 1 1 2004 3 1 2004 3 1 2004 3我已经在这里编写了代码.
I have written the code here.
data = pandas.DataFrame({'A':[2003,2003,2008,2007,2007,2004,2004,2004,2004] , 'B': [1,1,2,2,2,3,1,1,1] ,'C': [2,2,1,2,2,1,3,3,3] }) fig = plt.figure() ax = plt.axes(projection='3d') # put 0s on the y-axis, and put the y axis on the z-axis #ax.plot(data.A.values, data.B.values,data.freq.values, marker='o', linestyle='--', color="blue", label='ys=0, zdir=z') xpos= range(len( data.A.values)) ypos= range(len( data.B.values)) zpos= range(len( data.freq.values)) ax.bar3d(xpos, ypos, zpos, data.A.values, data.B.values,data.freq.values, color='b', alpha=0.5) x_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False) ax.xaxis.set_major_formatter(x_formatter) ax.set_xticks(data.A.values) ax.set_yticks(data.B.values) ax.set_zticks(data.freq.values) plt.savefig("test.png", dpi=300) plt.show()但这似乎不是正确的方法?显示我们如何自定义轴可以帮助任何人吗?
But it doesn't seem to be the right way to do that? Can any one help by showing how do we customize axes ?
当我使用情节时它会起作用
It works when I use plot
ax.plot(data.A.values, data.B.values,data.freq.values,marker='o', linestyle='--', color='r')代替bar3D
ax.bar3d(xpos, ypos, zpos, data.A.values, data.B.values,data.freq.values, color='b', alpha=0.5)但是我想使用3D直方图来更好地理解.
but I wanna use 3D histogram for better understading.
推荐答案似乎您误解了 bar3d 函数上的参数:
It seems you're misunderstanding the parameters on the bar3d function:
bar3d(x, y, z, dx, dy, dz)
- 参数 x , y 和 z 是x,y和z轴上条形的坐标.
- 参数 dx , dy 和 dz 分别是x,y和z轴上条形的大小.
- Parameters x, y and z are the coordinates of the bars on the x, y and z axis respectively.
- Parameters dx, dy and dz are the sizes of the bars on the x, y and z axis respectively.
例如,如果要绘制以下数据集:
For example, if you want to plot the following dataset:
{'A': [1, 2], 'B': [2003, 2008] ,'freq': [2, 3] }
您必须像这样定义这些参数:
You have to define these parameters like so:
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D xpos = [1, 2] ypos = [2003, 2008] zpos = [0, 0] dx = 1 dy = 1 dz = [2, 3] fig = plt.figure() ax = plt.axes(projection='3d') ax.bar3d(xpos, ypos, zpos, dx, dy, dz) plt.show()这是:
- 您在高度 2 的(1,2003,0)(x,y,z)中绘制了一个条形图.
- 您在(2,2008,0)(x,y,z)中绘制一个条形,高度为 3 .
- 两个条形图在x轴和y轴上的大小均为 1 ,但可能会更少,这只是一个美学问题.
- You plot one bar in (1, 2003, 0) (x, y, z) with height 2.
- You plot one bar in (2, 2008, 0) (x, y, z) with height 3.
- Both bars have a size of 1 on the x and y axis, it could be less though, it's just an aesthetic issue.
上面的脚本生成以下图:
The script above generates the following plot:
如果您查看图片,会发现一些较小的格式问题:
If you look at the image you'll notice some minor format problems:
- 年份以指数表示.
- 条形图不在其(x,y)坐标上居中.
我们实际上可以通过一些调整来解决此问题:
We can actually solve this with a few tweaks:
import matplotlib import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D xpos = [1, 2] ypos = [2003, 2008] zpos = [0, 0] dx = 1 dy = 1 dz = [2, 3] # Move each (x, y) coordinate to center it on the tick xpos = map(lambda x: x - 0.5, xpos) ypos = map(lambda y: y - 0.5, ypos) fig = plt.figure() ax = plt.axes(projection='3d') ax.bar3d(xpos, ypos, zpos, dx, dy, dz) # Do not print years in exponential notation y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False) ax.yaxis.set_major_formatter(y_formatter) plt.show()最后这就是我们得到的:
And finally this is what we'll get:
更多推荐
如何在3D历史记录python/matplotlib中自定义轴
发布评论