I'm using Matplotlib creating a number of plot functions (to be put in a module). The different functions refer to different types of plots, and I would like to have different "style sheets" (i.e. different settings for lines, markers, axes etc) depending on the plot type. However, I run into problem when trying to set predefined Matplotlib styles using the pyplot.style.use() function from within the plotting functions. Setting it in the main script works, but then I can't have different styles for different plots.
So, this code sets the style as I want
# A plot function
def my_plot(ax, xd, yd):
'''Create a plot'''
ax.scatter(xd, yd)
# Main Script
import numpy as np
import matplotlib.pyplot as plt
# Set plot style sheet in main script
plt.style.use('ggplot')
fig, ax = plt.subplots(1)
x = np.arange(1, 101)
y = 20 + 3 * x + np.random.normal(0, 60, 100)
sc = my_plot(ax, x, y)
plt.show()
However, the below does not, although the rc parameter settings have been changed after the function call, so running the script again from within python changes the style of the figure accordingly.
# A plot function
def my_plot(ax, xd, yd):
'''Create a plot'''
# Set plot style sheet in function
plt.style.use('ggplot')
ax.scatter(xd, yd)
# Main Script
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1)
x = np.arange(1, 101)
y = 20 + 3 * x + np.random.normal(0, 60, 100)
sc = my_plot(ax, x, y)
plt.show()