Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating sets of default values for Matplotlib

I am frequently making plots for my own research and all of the default settings are fine, but often have to switch over to making plots designed for talks/presentations; I manually set all of the font sizes a bit bigger for easier reading:

plot(xdata, ydata)
xlabel("x-axis data", fontsize=20)
ax = gca()
for labeltick in ax.xaxis.get_majorticklabels() + ax.yaxis.get_majorticklabels():
        labeltick.set_fontsize(15)

and so on.

Thanks to documentation and questions like this one I know how to control default plotting parameters when I start up matplotlib. I thought about writing something really quick (mpl_defaults.py):

import matplotlib as mpl
def plot_for_talks():
    mpl.rcParams['font.size'] = 20
    mpl.rcParams['figure.subplot.left'] = .2
    mpl.rcParams['figure.subplot.right'] = .8
    mpl.rcParams['figure.subplot.bottom'] = .2
    mpl.rcParams['figure.subplot.top'] = .8

Then my plotting code could just include

import mpl_defaults
plot_for_talks()

My question: is there a more appropriate way to do this? Maybe something already built in?

like image 612
physicsmichael Avatar asked Nov 29 '10 05:11

physicsmichael


People also ask

What is the default colormap for matplotlib?

The new default colormap used by matplotlib. cm. ScalarMappable instances is 'viridis' (aka option D).

What is the default plot in matplotlib?

By default, the size of the Matplotlib plots is 6 x 4 inches. The default size of the plots can be checked using this command: import matplotlib.pyplot as plt print(plt.rcParams.get('figure.figsize'))

What does rcParams do in python?

Changing the Defaults: rcParams Each time Matplotlib loads, it defines a runtime configuration (rc) containing the default styles for every plot element you create. This configuration can be adjusted at any time using the plt.

How do I customize matplotlib?

There are three ways to customize Matplotlib: Setting rcParams at runtime. Using style sheets. Changing your matplotlibrc file.


2 Answers

Try this:

import matplotlib as mpl    
mpl.rc('figure.subplot', left=.2, right=.8, bottom=.2, top=.8)

And there should be a "site-packages/matplotlib/mpl-data/matplotlibrc" file, described in doc 5.1.

Use mpl.matplotlib_fname() to get your rc file path, and modify it so the setting will be permanent.

like image 195
Kabie Avatar answered Sep 28 '22 08:09

Kabie


If you manage your separate presentation modes by directories, you can put a matplotlibrc file in each project directory, and matplotlib will use the one in the current directory.

like image 22
Jouni K. Seppänen Avatar answered Sep 28 '22 08:09

Jouni K. Seppänen