将第二个Y轴添加到插入的缩放轴

bsit 发布于 2019-02-11 matplotlib 最后更新 2019-02-11 21:14 9 浏览

我绘制了一个关于两个y轴(即两个不同的S.I.尺度)的图形到一个单一的x轴。我必须放大一些价值,我用Matplotlib的zoom_inset_locator技巧来管理它。我实现了缩放轴,但我错过了第二个Y轴(请参阅下面的示例):

Two-y axis and an inset zoom 它试图再次使用twinx()添加第二个轴,但是它失败了,因为它在主twinx(右)轴上绘制轴,但在缩放右轴上留下空白滴答,似乎给了x轴正确的处理方式, 见下文: enter image description here 有什么解决方法吗?以下是我用来绘制图形的代码:
import numpy,os,sys
import pylab
import scipy.optimize
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
# Initializing the curve
fig_cal=pylab.figure()
host_weight = fig_cal.add_subplot(111)
host_mass = host_weight.twinx()
Tension = numpy.linspace(0,0.08,100)
Weight = 0.5* Tension
Mass = Weight/9.81
# Plotting the curve
host_weight.plot(Tension, Weight, 'r', label='Fitted line',lw=2)
host_mass.plot(Tension, Mass)
# Cosmetic on the Figure
host_weight.set_xlabel("Tension U [$V$]")
host_weight.set_ylabel("Weight F [$N$]")
host_mass.set_ylabel("Mass M [$kg$]")
host_mass.set_ylim(host_weight.axis()[-2]/9.81, host_weight.axis()[-1]/9.81)
host_weight.grid(False)
# Zoom on the first measurement
zoom_weight = zoomed_inset_axes(host_weight, zoom = 7.5, bbox_to_anchor=(0.95,0.5), bbox_transform=host_weight.transAxes)
zoom_weight.plot(Tension[:4], Weight[:4], 'r', lw=2)
zoom_weight.set_xticks(zoom_weight.xaxis.get_majorticklocs()[::2])
zoom_weight.set_yticks(zoom_weight.yaxis.get_majorticklocs()[::2])
# zoom_mass = zoom_weight.twinx()
# zoom_mass.plot(Tension[:4], Mass[:4],alpha=0)
# zoom_mass.set_ylim(zoom_weight.axis()[-2]/9.81,zoom_weight.axis()[-1]/9.81)
mark_inset(host_weight, zoom_weight, loc1=2, loc2=4, fc="none", ec="0.5")
pylab.show()
已邀请:

trerum

赞同来自:

您可以考虑使用自动收报机格式化程序: 代码是这样的:

formatter = matplotlib.ticker.EngFormatter(unit='S', places=3)
formatter.ENG_PREFIXES[-6] = 'u'
plt.axes().yaxis.set_major_formatter(formatter)
看一下这篇文章的情节: matplotlib; fractional powers of ten; scientific notation

miure

赞同来自:

所以我找到了我的问题的答案...抱歉延迟了,但是我把这个问题搁置了......我确实找到了这个错误,但只是通过生成另一个缩放插图,使用alpha运河并禁用了很多来解决问题的东西...... 这是我的代码:

import numpy,os,sys
import pylab
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
# Initializing the curve
fig_cal=pylab.figure()
host_weight = fig_cal.add_subplot(111)
host_mass = host_weight.twinx()
Tension = numpy.linspace(0,0.08,100)
Weight = 0.5* Tension
Mass = Weight/9.81
# Plotting the curve
host_weight.plot(Tension, Weight, 'r', label='Fitted line',lw=2)
host_mass.plot(Tension, Mass, alpha=0)
# Cosmetic on the Figure
host_weight.set_xlabel("Tension U [$V$]")
host_weight.set_ylabel("Weight F [$N$]")
host_mass.set_ylabel("Mass M [$kg$]")
host_mass.set_ylim(host_weight.axis()[-2]/9.81, host_weight.axis()[-1]/9.81)
host_weight.grid(False)
# Zoom on the first measurement
zoom_weight = zoomed_inset_axes(host_weight, zoom = 7.5, bbox_to_anchor=(0.95,0.5), bbox_transform=host_weight.transAxes)
zoom_weight.plot(Tension[:4], Weight[:4], 'r', lw=2)
zoom_weight.set_xticks(zoom_weight.xaxis.get_majorticklocs()[::2])
zoom_weight.set_yticks(zoom_weight.yaxis.get_majorticklocs()[::2])
zoom_mass = zoomed_inset_axes(host_mass, zoom = 7.5, bbox_to_anchor=(0.95,0.5),     bbox_transform=host_mass.transAxes)
zoom_mass.xaxis.set_visible(False)
zoom_mass.spines['left'].set_visible(False)
zoom_mass.spines['top'].set_visible(False)
zoom_mass.patch.set_alpha(00)
zoom_mass.yaxis.tick_right()
zoom_mass.yaxis.set_label_position('right')
zoom_mass.yaxis.set_offset_position('right')
zoom_mass.plot(Tension[:4], Mass[:4],color='w', alpha=0)
zoom_mass.set_ylim(zoom_weight.axis()[-2]/9.81,zoom_weight.axis()[-1]/9.81)
pylab.show()
也许不是最好的方式,但它的工作原理!