Closed
Description
In the first example, ax.spines['right'].set_position()
is called first, and the negative signs on the right y-axis are smaller hyphen-minus symbols because they were passed through ax.yaxis.set_major_formatter()
. in the second example, the order is reversed, and the formatter is not applied or is reset (i.e., the negative signs are the larger unicode character \u2212
).
I was under the impression that matplotlib settings were order-agnostic before plt.show()
- is this a bug?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter, MultipleLocator
fig, ax1 = plt.subplots()
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
ax1.plot(t, s1, 'b-')
ax1.set_xlabel('time (s)')
# Make the y-axis label and tick labels match the line color.
ax1.set_ylabel('exp', color='b')
for tl in ax1.get_yticklabels():
tl.set_color('b')
ymajorFormatter = FormatStrFormatter("%.1f")
ax2 = ax1.twinx()
s2 = np.sin(2*np.pi*t)
ax2.plot(t, s2, 'r.')
ax2.set_ylabel('sin', color='r')
for tl in ax2.get_yticklabels():
tl.set_color('r')
# ax2.yaxis.set_major_formatter(ymajorFormatter)
ax2.spines['right'].set_position(('axes', 1.1))
ax2.yaxis.set_major_formatter(ymajorFormatter)
plt.show()
fig, ax1 = plt.subplots()
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
ax1.plot(t, s1, 'b-')
ax1.set_xlabel('time (s)')
# Make the y-axis label and tick labels match the line color.
ax1.set_ylabel('exp', color='b')
for tl in ax1.get_yticklabels():
tl.set_color('b')
ymajorFormatter = FormatStrFormatter("%.1f")
ax2 = ax1.twinx()
s2 = np.sin(2*np.pi*t)
ax2.plot(t, s2, 'r.')
ax2.set_ylabel('sin', color='r')
for tl in ax2.get_yticklabels():
tl.set_color('r')
ax2.yaxis.set_major_formatter(ymajorFormatter)
ax2.spines['right'].set_position(('axes', 1.1))
# ax2.yaxis.set_major_formatter(ymajorFormatter)
plt.show()