7

I tend to use the following axes options in my Matplotlib (v1.3.1) plots:

        ax.spines["top"].set_visible(False)  
        ax.spines["bottom"].set_visible(True)  
        ax.spines["right"].set_visible(False)  
        ax.spines["left"].set_visible(True)  

        ax.get_xaxis().tick_bottom()  
        ax.get_yaxis().tick_left()  

        ax.tick_params(axis="both", which="both", bottom="off", top="off",  
                labelbottom="on", left="off", right="off", labelleft="on")  

This works fine after grabbing the current axis, but I was wondering whether I could make all this default behaviour by setting things in rcParams?

Jose
  • 2,089
  • 2
  • 23
  • 29
  • So, it would appear that this is [not currently possible](http://matplotlib.1069221.n5.nabble.com/Plotting-style-tp45034p45071.html). Any tips on how I could streamline things a bit? – Jose Mar 06 '15 at 13:29
  • The helper function in the answer to that question looks perfectly handy to me. Put it in your module of utility functions. – cphlewis Mar 06 '15 at 22:45
  • @cphlewis That's what I have right now, a function that cleans up the axis, but it would be really nice to have it as a default, rather than having to remember to call this function everytime – Jose Mar 08 '15 at 12:54
  • 1
    Just for those seeing this old question: The option to set spines and ticks via [rcParams](https://matplotlib.org/users/customizing.html) is present in any newer matplotlib version. – ImportanceOfBeingErnest Nov 09 '17 at 20:48

2 Answers2

4

As mentioned in ImportanceOfBeingErnest's comment, this is now possible:

plt.rcParams['axes.spines.top'] = False
plt.rcParams['axes.spines.bottom'] = False
plt.rcParams['axes.spines.left'] = False
plt.rcParams['axes.spines.right'] = False

For multiple rcParams use a dict:

plt.rcParams.update({'axes.spines.top': False, 'axes.spines.right': False})
Dan
  • 45,079
  • 17
  • 88
  • 157
0

I had the same problem and have, between this answer and have found the following:

Matplotlib 1.3.1 does not recognize any of these inputs as rcParams:

>>> plt.matplotlib.__version__
'1.3.1'
>>> plt.rcParams['axes.spines.top'] = False
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\<username>\Software\WinPython-64bit-2.7.6.4\python-2.7.6.amd64\lib\site-packages\matplotlib\__init__.py", line 811, in __setitem__
    See rcParams.keys() for a list of valid parameters.' % (key,))
KeyError: 'axes.spines.top is not a valid rc parameter.See rcParams.keys() for a list of valid parameters.' 

Matplotlib 1.5.1 does, however:

In[1]: from matplotlib import pyplot as plt 
In[2]: plt.matplotlib.__version__
Out[2]: '1.5.1'
In[3]: plt.rcParams
Out[3]: 
RcParams({u'agg.path.chunksize': 0,
          [...]
          u'axes.spines.bottom': True,
          u'axes.spines.left': True,
          u'axes.spines.right': False,
          u'axes.spines.top': False,
          [...]
In[4]: x = np.linspace(0, 6)
  ...:     y = np.sin(x)
  ...:     plt.close('all')
  ...:     plt.plot(x,y,'k-')

Figure with axes.spines deactivated for top and right edge in matplotlibrc

To remove the ticks as well, I had to removee them after the fact, using ax.tick_params(top="off", right="off"). I did not find anything related in rcParams.

I also tested matplotlib 2.2.3. There, the axes.spine.* and got no ticks on the top and bottom without specifying any additional parameters. Either the entries in the matplotlibrc remove the whole axis, including tick marks, or the new ytick.right, xtick.top entries in the rcParams dictionary are defaulting to False

The same figure with matplotlib 2.2.3, using the same matplotlibrc

Unfortunately, that still hides the last gridline on the right-hand side if it is exactly at the edge, even though the top gridline is shown under the same circumstances.

==> in other words:

  • version 1.3.1 does not have a global switch and needs ax.spines["top"].set_visible(False) applied.
  • 1.5.1 can remove the lines, globally, using the method Dan suggested (set axes.spine.top and axes.spine.right to False), but not the ticks, which still need the ax.tick_params() command
  • still later versions (not sure which version introduced it) also have ytick.right and xtick.top settings which can be set to True or False to also omit the ticks on those edges.
Zak
  • 3,063
  • 3
  • 23
  • 30