在python中更改yaxis的ticks

2024-04-26 07:11:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在python中更改y轴记号。我在用密码

import pylab as plt
y1 = [0,1,2,...10]
y2 = [90,40,65,12,....]
labels = [0.30,0.29,0.28,....]
plt.plot(y1)
plt.plot(y2,'r')
plt.yticks(y1, labels)
plt.yticks(y2, labels)
plt.show()

但是所有的y轴标签都出现在一个位置上


Tags: import密码labelsplotasshowplt标签
1条回答
网友
1楼 · 发布于 2024-04-26 07:11:21

下面的代码大量借鉴了这个example,演示了在一个图形上有两个绘图的一种可能方法。在

import pylab as plt

fig, ax1 = plt.subplots()
y1 = [0,1,2,3,4,5,6,7,8,9,10]

labels = [0.30,0.29,0.28,0.27,0.26,0.25,0.24,0.23,0.22,0.21,0.20]
ax1.plot(labels, y1, 'b-')

ax1.set_xlabel('labels')
# Make the y-axis label, ticks and tick labels match the line color.
ax1.set_ylabel('y1', color='b', rotation="horizontal")
ax1.tick_params('y', colors='b')

ax2 = ax1.twinx()
y2 = [90,40,65,12,23,43,54,13,42,53,63]
ax2.plot(labels, y2, 'r-')
ax2.set_ylabel('y2', color='r', rotation="horizontal")
ax2.tick_params('y', colors='r')

fig.tight_layout()
plt.show()

相关问题 更多 >