0

I have an unusual request, but I have a question that has been bothering me for some time regarding matplotlib.

When I plot figures, even with the basic commands, for example (example), my plots do not have the same look. That is to say that in my case the ticks are systematically on the outside and only on the left and bottom edges, see:

My plot with outside ticks + only 2 axis with ticks on.

However, while looking at some ppl plots, they don't look like this, and they systematically have the four sides with ticks that are pointing inside the plot:

Plot from someone giving tips on stackoverflow

I know how to modify this for a single particular plot. But I would like to know if there is a way to specify somewhere that all my plots should have this style.

Is it possible to do so?

Bastian
  • 5
  • 2
  • Please have a look through the [Matplotlib example gallery](https://matplotlib.org/stable/gallery/index). It is likely some example in there can give you the details you need. In fact, there is a separate section called [Ticks](https://matplotlib.org/stable/gallery/index#ticks) that may be appropriate (but look around at the other examples if that section doesn't show your problem and solution). – 9769953 Jan 20 '22 at 19:02
  • 1
    You can edit your rcparams file to change defaults for most of matplotlib. Here's [the rcparams documentation](https://matplotlib.org/stable/tutorials/introductory/customizing.html), here's [a stackoverflow example about ticks](https://stackoverflow.com/questions/60792981/how-to-use-xtick-major-width-and-xtick-minor-widtch-rcparams-keys). – cphlewis Jan 20 '22 at 19:31
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jan 30 '22 at 05:24

1 Answers1

0

You can patch the Figure.add_subplot method and put your customization in there. For example:

import matplotlib.pyplot as plt
from matplotlib.figure import Figure


_original_add_subplot = Figure.add_subplot


def new_add_subplot(*args, **kwargs):
    ax = _original_add_subplot(*args, **kwargs)
    ax.tick_params(left='on', right='on', top='on', bottom='on')
    return ax


Figure.add_subplot = new_add_subplot


fig, ax = plt.subplots()
ax.axline((0,0), slope=1)
plt.show()

Then you could put all this in a separate package which would execute this when imported. So all you would need to do is import mplcustom. If that's still too much work, you can also put the code into sitecustomize or usercustomize modules, which will be imported automatically on startup (see Site-specific configuration hook for more information).

a_guest
  • 34,165
  • 12
  • 64
  • 118