How to remove the top and right axes at the same time and glue the pliers out

I would like to make a matplotlib graph that has only the left and bottom axes, as well as ticks facing outward and not inward by default. I found two questions that address both topics separately:

Each of them works on its own, but, unfortunately, both solutions seem to be incompatible with each other. axes_grid hit my head several times, I found a warning in the axes_grid documentation that says

"some commands (mainly related to ticks) do not work"

This is the code I have:

 from matplotlib.pyplot import * from mpl_toolkits.axes_grid.axislines import Subplot import matplotlib.lines as mpllines import numpy as np #set figure and axis fig = figure(figsize=(6, 4)) #comment the next 2 lines to not hide top and right axis ax = Subplot(fig, 111) fig.add_subplot(ax) #uncomment next 2 lines to deal with ticks #ax = fig.add_subplot(111) #calculate data x = np.arange(0.8,2.501,0.001) y = 4*((1/x)**12 - (1/x)**6) #plot ax.plot(x,y) #do not display top and right axes #comment to deal with ticks ax.axis["right"].set_visible(False) ax.axis["top"].set_visible(False) #put ticks facing outwards #does not work when Sublot is called! for l in ax.get_xticklines(): l.set_marker(mpllines.TICKDOWN) for l in ax.get_yticklines(): l.set_marker(mpllines.TICKLEFT) #done show() 
+4
source share
1 answer

With a little code change and using the trick (or hack?) From this link , this works:

 import numpy as np import matplotlib.pyplot as plt #comment the next 2 lines to not hide top and right axis fig = plt.figure() ax = fig.add_subplot(111) #uncomment next 2 lines to deal with ticks #ax = fig.add_subplot(111) #calculate data x = np.arange(0.8,2.501,0.001) y = 4*((1/x)**12 - (1/x)**6) #plot ax.plot(x,y) #do not display top and right axes #comment to deal with ticks ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) ## the original answer: ## see http://old.nabble.com/Ticks-direction-td30107742.html #for tick in ax.xaxis.majorTicks: # tick._apply_params(tickdir="out") # the OP way (better): ax.tick_params(axis='both', direction='out') ax.get_xaxis().tick_bottom() # remove unneeded ticks ax.get_yaxis().tick_left() plt.show() 

If you want external ticks on all your charts, it might be easier to set the direction of the tick in the rc file - on this page search for xtick.direction

+7
source

Source: https://habr.com/ru/post/1394581/


All Articles