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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值