如何在3D hist python/matplotlib中自定义轴

如何在3D hist python/matplotlib中自定义轴,python,matplotlib,pandas,Python,Matplotlib,Pandas,我正在尝试使用3D栏绘制此数据集 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 我已经在这里编写了代码 data = pandas.DataFrame({'A':[2003,2003,2008,2007,2007,200

我正在尝试使用3D栏绘制此数据集

  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
我已经在这里编写了代码

  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()
但这似乎不是正确的方法?任何人都可以通过展示如何定制轴来帮助我们吗

当我使用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直方图来更好的理解

有太多地方你弄错了,所以我只想发布它应该是什么样子:

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

data = pd.DataFrame({'A': [1,1,2,2,2,3,1,1,1], 'B': [2003,2003,2008,2007,2007,2004,2004,2004,2004] ,'freq': [2,2,1,2,2,1,3,3,3] })
fig = plt.figure()
ax = fig.add_subplot(111, 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')
PV = pd.pivot_table(data, values='freq',rows='A',cols='B')
xpos=np.arange(PV.shape[0])
ypos=np.arange(PV.shape[1])
xpos, ypos = np.meshgrid(xpos+0.25, ypos+0.25)
xpos = xpos.flatten()
ypos = ypos.flatten()
zpos=np.zeros(PV.shape).flatten()
dx=0.5 * np.ones_like(zpos)
dy=0.5 * np.ones_like(zpos)
dz=PV.values.ravel()
dz[np.isnan(dz)]=0.

ax.bar3d(xpos,ypos,zpos,dx,dy,dz,color='b', alpha=0.5)
ax.set_xticks([.5,1.5,2.5])
ax.set_yticks([.5,1.5,2.5,3.5])
ax.w_yaxis.set_ticklabels(PV.columns)
ax.w_xaxis.set_ticklabels(PV.index)
ax.set_xlabel('A')
ax.set_ylabel('B')
ax.set_zlabel('Occurrence')

plt.savefig("test.png", dpi=300)
plt.show()

有太多的地方你弄错了,所以我只想发布它应该是什么样子:

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

data = pd.DataFrame({'A': [1,1,2,2,2,3,1,1,1], 'B': [2003,2003,2008,2007,2007,2004,2004,2004,2004] ,'freq': [2,2,1,2,2,1,3,3,3] })
fig = plt.figure()
ax = fig.add_subplot(111, 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')
PV = pd.pivot_table(data, values='freq',rows='A',cols='B')
xpos=np.arange(PV.shape[0])
ypos=np.arange(PV.shape[1])
xpos, ypos = np.meshgrid(xpos+0.25, ypos+0.25)
xpos = xpos.flatten()
ypos = ypos.flatten()
zpos=np.zeros(PV.shape).flatten()
dx=0.5 * np.ones_like(zpos)
dy=0.5 * np.ones_like(zpos)
dz=PV.values.ravel()
dz[np.isnan(dz)]=0.

ax.bar3d(xpos,ypos,zpos,dx,dy,dz,color='b', alpha=0.5)
ax.set_xticks([.5,1.5,2.5])
ax.set_yticks([.5,1.5,2.5,3.5])
ax.w_yaxis.set_ticklabels(PV.columns)
ax.w_xaxis.set_ticklabels(PV.index)
ax.set_xlabel('A')
ax.set_ylabel('B')
ax.set_zlabel('Occurrence')

plt.savefig("test.png", dpi=300)
plt.show()

您似乎误解了bar3d函数的参数:

bar3dx,y,z,dx,dy,dz

参数x、y和z分别是x、y和z轴上钢筋的坐标。 参数dx、dy和dz分别是x、y和z轴上钢筋的尺寸。 例如,如果要打印以下数据集:

{'A':[1,2],'B':[2003,2008],'freq':[2,3]}

您必须这样定义这些参数:

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()
这是:

在1、2003、0 x、y、z中绘制一个高度为2的条形图。 在2008年2月,0 x,y,z中绘制一个高度为3的条形图。 这两条线在x轴和y轴上的尺寸都是1,但可能更小,这只是一个美学问题。 上面的脚本生成以下绘图:

如果查看图像,您会注意到一些小的格式问题:

年以指数表示。 条形图不以其x、y坐标为中心。 我们实际上可以通过一些调整来解决这个问题:

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()
最后,我们将得到:


您似乎误解了bar3d函数的参数:

bar3dx,y,z,dx,dy,dz

参数x、y和z分别是x、y和z轴上钢筋的坐标。 参数dx、dy和dz分别是x、y和z轴上钢筋的尺寸。 例如,如果要打印以下数据集:

{'A':[1,2],'B':[2003,2008],'freq':[2,3]}

您必须这样定义这些参数:

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()
这是:

在1、2003、0 x、y、z中绘制一个高度为2的条形图。 在2008年2月,0 x,y,z中绘制一个高度为3的条形图。 这两条线在x轴和y轴上的尺寸都是1,但可能更小,这只是一个美学问题。 上面的脚本生成以下绘图:

如果查看图像,您会注意到一些小的格式问题:

年以指数表示。 条形图不以其x、y坐标为中心。 我们实际上可以通过一些调整来解决这个问题:

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()
最后,我们将得到:


请您澄清一下如何调整轴。数据帧{'A':np.random.rand100*1000,'B':np.random.rand100*10,'freq':np.random.rand100*2220}。如果我要更改ax.set_xticks或w_yaxis.set_ticklebels,这会让人有点困惑是的,我想你需要这样做,但你首先需要从数据帧数据制作一个pivot_表,你的ticks和ticklebels将根据pivot_数据表进行调整。你能澄清一下你是如何调整轴的吗。数据帧{'A':np.random.rand100*1000,'B':np.random.rand100*10,'freq':np.random.rand100*2220}。如果我更改ax.set_xticks或w_yaxis.set_ticklebels,这会让人有点困惑是的,我认为您需要这样做,但您首先需要从数据帧数据创建一个pivot_表,您的ticks和ticklebels将根据pivot_表的数据进行调整。