Installing spikes in matplotlibrc

For a strange reason, I cannot find a way to specify the configuration of the spine in the matplotlibrc Python file. Any idea on how to get matplotlib not to draw top and right spikes by default? spines
(source: sourceforge.net )

Read more about spikes in matplotlib here

.Thank

+6
python matplotlib
Aug 09 '10 at 11:00
source share
3 answers

To hide the right and top spikes of the subtitle, you need to set the color of the corresponding spikes to 'none' , and also set the tick position to 'left' for xtick and 'bottom' for ytick (to hide the checkmarks, as well as the spikes).

Unfortunately, none of them are currently available through matplotlibrc . The parameters specified in matplotlibrc are checked and then stored in a dict called rcParams . Then the individual modules check the key in this dict, the value of which will act by default. If they do not check it for one of their options, this option cannot be changed using the rc file.

Due to the nature of the rc system and the way spikes are written, changing the code for this would not be easy:

Spikes currently get their color through the same rc parameter, which is used to determine the colors of the axes; you cannot set it to 'none' without hiding the entire axis pattern. They are also agnostic about whether they are top , right , left or bottom β€” in fact, these are just four separate spikes stored in a dict. Individual spinal objects do not know which side of the plot they are composed on, so you cannot just add new rc parameters and assign the correct ones during the initialization of the spine.

 self.set_edgecolor( rcParams['axes.edgecolor'] ) 

(./matplotlib/lib/matplotlib/spines.py, __init __ (), line 54)

If you have a large amount of existing code, so adding the axis parameters manually to each of them will be too burdensome, you can use the helper function one by one to iterate over all Axis objects and set values ​​for you.

Here is an example:

 import matplotlib import matplotlib.pyplot as plt import numpy as np from matplotlib.pyplot import show # Set up a default, sample figure. fig = plt.figure() x = np.linspace(-np.pi,np.pi,100) y = 2*np.sin(x) ax = fig.add_subplot(1,2,2) ax.plot(x,y) ax.set_title('Normal Spines') def hide_spines(): """Hides the top and rightmost axis spines from view for all active figures and their respective axes.""" # Retrieve a list of all current figures. figures = [x for x in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()] for figure in figures: # Get all Axis instances related to the figure. for ax in figure.canvas.figure.get_axes(): # Disable spines. ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') # Disable ticks. ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') hide_spines() show() 

Just call hide_spines() before show() and it will hide them in all digits displayed by show() . I can not think of an easier way to change a large number of digits without wasting time fixing matplotlib and add rc support for the necessary parameters.

+17
Aug 18
source share

To prevent matplotlib from drawing the top and right spikes, you can set the following in the matplotlibrc file

 axes.spines.right : False axes.spines.top : False 
+13
Mar 21 '16 at 16:00
source share
 ax.spines["top"].set_visible(False) ax.spines["right"].set_visible(False) 
0
Jul 28 '19 at 14:27
source share



All Articles