Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: Overriding "ggplot" default style properties

I am using matplotlibs ggplot style for plotting and want to overide only specific standard parameters such as the color of the xticklabels, grid background color and linewidth.

import numpy as np
import pandas as pd
import matplotlib

# changing matplotlib the default style
matplotlib.style.use('ggplot')

# dataframe plot
df = pd.DataFrame(np.random.randn(36, 3))
df.plot()

returns: enter image description here

I know I can set single properties for axes-objects like this:

ax.set_axis_bgcolor('red')

But how can I override the default propertiers (e.g. label-color, background color and linewidth to have them in all plots?

Thanks in advance!

like image 865
Cord Kaldemeyer Avatar asked Feb 05 '16 11:02

Cord Kaldemeyer


People also ask

How do I make my matplotlib look like ggplot?

To make the matplotlib similar to the ggplot you can pass on a style argument. There is a ggplot style already available but I personally prefer the white background as in ggplot's theme_bw .

Is matplotlib better than ggplot?

The plots are pretty much identical, aesthetics-wise, but ggplot2 beats Matplotlib once again when it comes to code amount. It's also much easier to format the x-axis to display dates in R than it is in Python.

What is style use (' ggplot ')?

This example demonstrates the "ggplot" style, which adjusts the style to emulate ggplot (a popular plotting package for R). These settings were shamelessly stolen from [1] (with permission).

What is the default matplotlib style?

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


1 Answers

You could use rcParams to set parameters globally. e.g.

import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt

# changing matplotlib the default style
matplotlib.style.use('ggplot')

plt.rcParams['lines.linewidth']=3
plt.rcParams['axes.facecolor']='b'
plt.rcParams['xtick.color']='r'

# dataframe plot
df = pd.DataFrame(np.random.randn(36, 3))
df.plot()

plt.show()

enter image description here

like image 185
tmdavison Avatar answered Sep 28 '22 10:09

tmdavison