概述
MWE(来自MPL邮件列表的精确副本)是:
from matplotlib.transforms import Transform,BlendedGenericTransform,IdentityTransform import matplotlib.pyplot as plt from mpl_toolkits.axes_grid.parasite_axes import subplotHost import numpy as np c = 3.e2 class Freq2WavelengthTransform(Transform): input_dims = 1 output_dims = 1 is_separable = False has_inverse = True def transform(self,tr): return c/tr def inverted(self): return Wavelength2FreqTransform() class Wavelength2FreqTransform(Freq2WavelengthTransform): def inverted(self): return Freq2WavelengthTransform() aux_trans = BlendedGenericTransform(Freq2WavelengthTransform(),IdentityTransform()) fig = plt.figure(2) ax_GHz = subplotHost(fig,1,1) fig.add_subplot(ax_GHz) ax_GHz.set_xlabel("Frequency (GHz)") xvals = np.arange(199.9,999.9,0.1) #make some test data data = np.sin(0.03*xvals) ax_mm = ax_GHz.twin(aux_trans) ax_mm.set_xlabel('Wavelength (mm)') ax_mm.set_viewlim_mode("transform") ax_mm.axis["right"].toggle(ticklabels=False) ax_GHz.plot(xvals,data) ax_GHz.set_xlim(200,1000) plt.draw() plt.show()
这产生了
谁能告诉我如何在MPL 1.4.2中解决这个问题?
import numpy as np import matplotlib.pyplot as plt c=3.e2 fig = plt.figure() ax1 = fig.add_subplot(111) ax2 = ax1.twiny() xvals = np.arange(199.9,0.1) data = np.sin(0.03*xvals) ax1.plot(xvals,data) ax1Ticks = ax1.get_xticks() ax2Ticks = ax1Ticks def tick_function(X): V = c/X return ["%.3f" % z for z in V] ax2.set_xticks(ax2Ticks) ax2.set_xbound(ax1.get_xbound()) ax2.set_xticklabels(tick_function(ax2Ticks)) ax1.set_xlabel("Frequency (GHz)") ax2.set_xlabel('Wavelength (mm)') ax1.grid(True) plt.ylim(ymin=-1.1,ymax=1.1) plt.show()
这产生;
我希望这有帮助!
总结
以上是编程之家为你收集整理的python – Matplotlib第二个带有转换值的x轴全部内容,希望文章能够帮你解决python – Matplotlib第二个带有转换值的x轴所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧