编辑:我尝试了以下。
auto_y_ticks=list(axes2.get_yticklabels())
但输出仍然不是刻度值列表。它显示为 Matplotlib 文本。
以下代码在同一图表中生成条形图和折线图。在辅助 y 轴上,ytick 值的范围是 -1 到 +1。我的问题是,如何将这些值存储在列表中?
from matplotlib import pyplot as plt
import numpy as np
plt.figure()
N = 5
menMeans = (20, 35, 30, 35, 27)
menStd = (2, 3, 4, 1, 2)
width = 0.35 # the width of the bars
womenMeans = (25, 32, 34, 20, 25)
womenStd = (3, 5, 2, 3, 3)
ind = np.arange(N)
plt.ylim(0.0, 65.0)
plt.bar(ind, menMeans, width, color='r', yerr=menStd, label='Men means')
plt.bar(ind+width, womenMeans, width, color='y', yerr=womenStd, label='Women means')
plt.ylabel('Bar plot')
x = np.linspace(0, N)
y = np.sin(x)
axes2 = plt.twinx()
axes2.plot(x, y, color='k', label='Sine')
axes2.set_ylim(-1, 1)
axes2.set_ylabel('Line plot')
plt.show()
auto_y_ticks=list(逻辑是什么)