Using header of different dataframe as title in for loop in pandas

Viewed 53

I managed to plot a few charts out with using this for loop:

for i in df.columns[2:7]:
   df.plot.scatter(x='out_date', y=i, figsize = (10,7))
   plt.axvline(x=cutoff_date, color='r')
   plt.xlabel('out date')
   plt.ylabel('sucesses')

I wanted to add titles to the plots using the header of a different dataframe with the following code but the title will only be added to the last plot instead of every plot:

for x in df2.columns[67:72]:
   plt.title(x)

Is there a way to fix this?

1 Answers

Try to zip your 2 dataframes:

for i, title in zip(df.columns[2:7], df2.columns[67:72]):
   ax = df.plot.scatter(x='out_date', y=i, figsize = (10,7), title=title)
   ax.axvline(x=cutoff_date, color='r')
   ax.set_xlabel('out date')
   ax.set_ylabel('sucesses')

You can also use AxesSubplot instance returned by df.plot methods instead of functions of plt module.

Related