0

I want to write a function that simply plots the data. How can I write the function in a way that it takes as arguments known keywords from plt.plot? For example:

def plot_data(x, y):
    plt.plot(x, y)
    plt.axvline(max(y)) 

Say I have 2 different data sets: I want to assign different colors and different labels.

The way I am doing it now is:

def plot_data(x, y, color=None, label=None):
    plt.plot(x, y, color=color, label=label)
    plt.axvline(max(y)) 

plt.plot(x1, y1, color='g', label='set1')
plt.plot(x2, y2, color='k', label='set2')

But what if I also want to assign different linestyle, different linewidth, different markers, ....

Is there a neat way to pass these keywords into the function instead of just writing them all?

def plot_data(x, y, color=None, label=None, ls=None, lw=None, marker=None):
    plt.plot(x, y, color=color, label=label, ls=ls, lw=lw, marker=marker)
    plt.axvline(max(y)) 

I can imagine something like:

def plot_data(x, y, **kwargs):
    plt.plot(x, y, **kwargs)
    plt.axvline(max(y)) 
aloha
  • 4,554
  • 6
  • 32
  • 40

0 Answers0