简体   繁体   中英

Removing or adjusting ticks for inset_axis

TL;DR: I cannot remove or adjust xticks from inset_axis.

I was trying to prepare a zoom-in plot, where a box will display a zoomed version of the plot. However, the x-ticks of the zoomed in plot were too entangled and I decided to manually assign them.

This is a snip from the original plot.

原始下降

So I tried the following lines:

inset_axe.set_xticks([])
inset_axe.set_yticks([]) 

It indeed removed the yticks, but xticks are not affected.

Here is a minimum working example. The issue persists in the MWE as well.

from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import numpy as np
import matplotlib.pyplot as plt

#####Just creating random N,Q function
a=0.1
Q=np.linspace(-5,4,1000)
M=a*np.ones(2000)
P=1.4**4*np.ones(2000)+a
N=[1.4**(x)+a for x in Q]
N=np.asarray(list(M)+N+list(P))
Q=np.logspace(-9,6,5000)
####################################

g, axes = plt.subplots(1)
inset_axe = inset_axes(axes,width="60%", height="25%", loc='lower left',
            bbox_to_anchor=(0.6,0.15,0.7,.7), bbox_transform=axes.transAxes)

inset_axe.semilogx(Q[2200:2400],N[2200:2400])
inset_axe.set_yticks([])
inset_axe.set_xticks([])
axes.semilogx(Q,N)
plt.show()

Is this a bug or do I have a small mistake that I cannot see? Is there a way around this?

If it helps, I use Microsoft VS and matplotlib version is 3.3.3.

Most of your ticks are minor.

You may also want to use the more lightweight axes.inset_axes :

import numpy as np
import matplotlib.pyplot as plt

#####Just creating random N,Q function
a=0.1
Q=np.linspace(-5,4,1000)
M=a*np.ones(2000)
P=1.4**4*np.ones(2000)+a
N=[1.4**(x)+a for x in Q]
N=np.asarray(list(M)+N+list(P))
Q=np.logspace(-9,6,5000)
####################################

g, ax = plt.subplots(1)
inset_axe = ax.inset_axes([0.6, 0.2, 0.2, 0.2], transform=ax.transAxes)

inset_axe.semilogx(Q[2200:2400], N[2200:2400])
inset_axe.set_yticks([])
#################

inset_axe.set_xticks([], minor=True)

#################
inset_axe.set_xticks([])
ax.semilogx(Q,N)
ax.set_xticks([])
plt.show()

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