简体   繁体   中英

Adding a second Y-Axis to an inset zoom axes

I draw a figure relating two y-axis (ie two different SI scale) to a single x-axis. I have to zoom on some value and I manage it with the zoom_inset_locator trick from Matplotlib. I achieve the zoom axes but I am missing the second y-axis (see example below):

双y轴和插入变焦

It did try to add a second axis using twinx() again, but it failed as it plot the axis on the main twinx (right) axis but leave blank ticks on the zoom right axis and seems to give the x-axis the right treatment, see below:

在此输入图像描述

Is there any workaround? Here is the code I used to draw the figure:

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()

You might consider using a ticker formatter:

The code is something like this:

formatter = matplotlib.ticker.EngFormatter(unit='S', places=3)
formatter.ENG_PREFIXES[-6] = 'u'
plt.axes().yaxis.set_major_formatter(formatter)

Have a look at this post for what the plot would look like: matplotlib; fractional powers of ten; scientific notation

So I found the answer to my question... Sorry for the delay, but I put this issue on hold... I did find the bug but just a workaround by generating an another zoom inset, using the alpha canal and disabling a lot of stuff...

Here is my code:

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()

Maybe not the best way, but it works !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM