Search code examples
pythonpandasmatplotlibpython-ggplot

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!


Solution

  • 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