Reputation: 123
How to set the x-label
and y-label
in a matplotlib-plot passing them as an parameter to the plot()
function.
Basically I want to do something like this:
def plot_something(data, plot_conf):
data.plot(**plot_conf)
...do some other stuff...
plot_conf = {'title': 'Blabla', 'xlabel':'Time (s)', 'ylabel': 'Speed (m/s)'}
plot_something(data,plot_conf)
I would prefer not to use any additional function calls, like xlabel()
Upvotes: 3
Views: 7600
Reputation: 58865
As @nordev already explained, you cannot pass through plot()
the axis label, but inside your function you can get the active figure and then set the axis labels like the example below:
import matplotlib.pyplot as plt
def plot_something(x, y, **kwargs):
title = kwargs.pop( 'title' )
xlabel = kwargs.pop( 'xlabel' )
ylabel = kwargs.pop( 'ylabel' )
plt.figure()
plt.plot(x, y, **kwargs)
fig = plt.gcf()
for axis in fig.axes:
axis.set_title( title )
axis.xaxis.set_label_text( xlabel )
axis.yaxis.set_label_text( ylabel )
return axis
plot_conf = {'title': 'Blabla', 'xlabel':'Time (s)', 'ylabel': 'Speed (m/s)'}
x = [1.,2.,3.]
y = [1.,4.,9.]
axis = plot_something(x=x,y=y, **plot_conf)
Upvotes: 4