pythonmatplotlib3dz轴大小_的Python - 增加了双x轴matplotlib图形尺寸

确实有可能在任何尺寸的图中创建双轴。一个人必须确保了解代码的写作。即请勿使用figure创建新数字,然后抱怨显示第二个数字。

坚持到matplotlib状态机接口,一个解决办法是这样的:

import matplotlib.pyplot as plt

import numpy as np

#get data

x=np.arange(40)

y=np.random.rand(len(x))*20000+30000

y2=np.random.rand(len(x))*0.5

#create a figure

plt.figure(figsize=(10,10))

#plot to first axes

plt.plot(x,y,color='blue',label="label1")

plt.ylim(0,50000)

plt.ylabel('ylabel1')

plt.xticks(rotation=90)

#create twin axes

ax2=plt.gca().twinx()

#plot to twin axes

plt.plot(x,y2,color='purple',label='label2')

plt.ylabel('ylabel2')

plt.legend(loc='upper right')

plt.show()

或者,如果你喜欢的matplotlib API:

import matplotlib.pyplot as plt

import numpy as np

#get data

x=np.arange(40)

y=np.random.rand(len(x))*20000+30000

y2=np.random.rand(len(x))*0.5

#create a figure

fig = plt.figure(figsize=(10,10))

ax = fig.add_subplot(111)

#plot to first axes

ax.plot(x,y,color='blue',label="label1")

ax.set_ylim(0,50000)

ax.set_ylabel('ylabel1')

ax.set_xticklabels(ax.get_xticklabels(),rotation=90)

#create twin axes

ax2=ax.twinx()

#plot to twin axes

ax2.plot(x,y2,color='purple',label='label2')

ax2.set_ylabel('ylabel2')

h1, l1 = ax.get_legend_handles_labels()

h2, l2 = ax2.get_legend_handles_labels()

ax.legend(handles=h1+h2, labels=l1+l2, loc='upper right')

plt.show()

您可以使用PythonMatplotlib库中的mpl_toolkits.mplot3d来绘制3D柱状图和直方图,其中ax.bar3d函数可以用于创建3D柱状图。 例如,以下是一个使用ax.bar3d绘制3D柱状图的示例代码: ```python import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # 数据 x = np.random.randint(0, 10, 10) y = np.random.randint(0, 10, 10) z = np.random.randint(0, 10, 10) dx = np.ones_like(x) dy = np.ones_like(y) dz = z # 绘制3D柱状图 ax.bar3d(x, y, z, dx, dy, dz) # 添加标签 ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') plt.show() ``` 如果您想要绘制3D直方图,可以使用ax.hist3d函数。以下是一个使用ax.hist3d绘制3D直方图的示例代码: ```python import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # 数据 x = np.random.randn(100) y = np.random.randn(100) z = np.random.randn(100) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # 绘制3D直方图 hist, xedges, yedges = np.histogram2d(x, y, bins=10) xpos, ypos = np.meshgrid(xedges[:-1]+xedges[1:], yedges[:-1]+yedges[1:]) xpos = xpos.flatten()/2. ypos = ypos.flatten()/2. zpos = np.zeros_like(xpos) dx = xedges[1] - xedges[0] dy = yedges[1] - yedges[0] dz = hist.flatten() ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average') # 添加标签 ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') plt.show() ``` 希望这能帮到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值