Matplotlib: set linewidth before plotting

Viewed 27

Is there a method in matplotlib to set the attributes of a plot BEFORE actually plotting it? To the context: I have a config file in which I configure aspects of a plot. However, some keys may not be needed to be set (as e.g. the linewidth). I would like to have a function which says something like this :

if 'linethicknes' in config:
       ax.set_linewidth = linethickness

I know that there is the possibility to give the linewidth property in the ax.plot(x, y ,linewidth = 1). But here the attribute is set in the plotting function, and I need to set it before the plotting. Is it possibile to set the linewidth (and other attributes) before this plotting function?

1 Answers

You can set the runtime configuration rcparams at the start and this will set the configuration. As long as you don't overwrite it, this will be set as the configuration. A simple example is given below...

plt.rcParams['lines.linewidth'] = 6.0

x=[1,2,3,4,5]
y=[5,4,3,2,1]
plt.plot(x, y)
plt.show()

enter image description here

Related