【发布时间】:2021-11-24 13:52:56
【问题描述】:
如何通过secondary_yaxis设置轴标签的格式?
在下面的示例中,我希望更多地控制第二个轴标签(例如 format='%g')。我认为应该可以将ticklabel_format 或format 之类的东西作为kwargs 传递,但似乎都不起作用(两者都提高AttributeError: 'SecondaryAxis' object has no property ...)。
import matplotlib.pyplot as plt
import numpy as np
# Conversion functions to and from km and hPa
def km2hPa(input):
return np.exp( input / -7.6 ) * 1013.
def hPa2Km(input):
return -7.6 * np.log( input / 1013. )
# Fake data to plot
Y = np.linspace(1000, 100, 100)
X = np.array( [i**2 for i in np.linspace(0, 10, len(Y)) ] )
# Plot up data with both primary (hPa) and secondary axis (km)
plt.close('all')
kwargs = {} # Add a format specifier here?
fig, ax = plt.subplots()
plt.plot(X, Y)
ax.set_ylabel('Pressure altitude (hPa)')
ax.invert_yaxis()
secax = ax.secondary_yaxis('right', functions=(hPa2Km, km2hPa), **kwargs)
secax.set_ylabel('Altitude (km)')
plt.show()
【问题讨论】:
标签: python matplotlib plot