Различные фигуры генерируются внутри цикла, и я бы хотел, чтобы масштабирование напрямую зависело от цикла. переменная.
Вот код работает, но масштабные коэффициенты фиксированы.
Код: Выделить всё
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
data = np.random.normal(0, 0.01, (20))
# The following works, right axis of both figures is scaled by 2 and 10, respectively
for right_scale in [2, 10]:
fig, ax = plt.subplots()
ax.plot(data)
ax2 = ax.twinx()
ax2.set_ylim(ax.get_ylim())
if right_scale == 2:
formatter = FuncFormatter(lambda x, pos: '{:.2f}'.format(x * 2))
else:
formatter = FuncFormatter(lambda x, pos: '{:.3f}'.format(x * 10))
ax2.yaxis.set_major_formatter(formatter)
plt.show()
Код: Выделить всё
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
data = np.random.normal(0, 0.01, (20))
# The following does not work, right axis of both figures is scaled by 10
for right_scale in [2, 10]:
fig, ax = plt.subplots()
ax.plot(data)
ax2 = ax.twinx()
ax2.set_ylim(ax.get_ylim())
if right_scale == 2:
formatter = FuncFormatter(lambda x, pos: '{:.2f}'.format(x * right_scale))
else:
formatter = FuncFormatter(lambda x, pos: '{:.3f}'.format(x * right_scale))
ax2.yaxis.set_major_formatter(formatter)
plt.show()
Подробнее здесь: https://stackoverflow.com/questions/792 ... c-variable