Reputation: 4005
I am using several subplots. In these subplots data is plotted using imshow
. The options I pass to imshow
are identical for almost all of the subplots. However, they pollute my code due to the length of the options.
The options I use include:
extent
cmap
norm
origin
interpolation
To have a better overview of my code I would like to define the options once and call them for each subplot. What is a pythonic way to do this?
Upvotes: 1
Views: 62
Reputation: 53668
You can store the options in a dictionary and then unpack them within the function call using the **kwargs
operator, as below (example uses plot
rather than imshow
but it's the same idea).
import matplotlib.pyplot as plt
import numpy as np
options = {'linewidth':2,
'color':'red',
'linestyle':'dashed'}
x = np.linspace(0,10,100)
y = x**2
fig, ax = plt.subplots()
ax.plot(x,y, **options)
plt.show()
Above I have stored the options in the options
dictionary and then unpacked them within the plot function call with ax.plot(x,y, **options)
.
Upvotes: 3