How to include an input argument in function which is not always required (in python)?

Viewed 67

I want to include an input argument in a function which is not always necessary .how to do that? for example:

df=

0   75.713669  35.917743
1   75.556753  35.626998
2   75.296503  35.190880
3   75.124278  34.881007
4   75.047734  34.708855

def plot_df(Dataframe,color):
 df=Dataframe
 if color!= None:
  a=df.plot()
 else:
  plt.plot(color=color)
 plt.show()

I want to use this function sometimes with the intention of having a specific color in output plot plot_df(DataFrame,color='red')

And sometimes I am not strict about the color factor in the plot plot_df(DataFrame) How to implement this in the function?

2 Answers

You could set the colour to some default value, then the colour would always take that value unless otherwise specified.

def plot_df(Dataframe,color=None):
 df=Dataframe
 if color!= None:
  a=df.plot()
 else:
  plt.plot(color=color)
 plt.show()

per this implementation if there is no color argument provided it will just deafult to None

You can use a default value for the color argument:

def plot_df(Dataframe,color="black"):
    df = Dataframe   
    df.plot(color=color)
    plt.show()

This way, if you don't specify the color it will be black by default. Otherwise it will be the color you specified.

Related