Reputation: 1
I am trying to add a vertical line to a Pandas.dataframe.plot(subplot), but am struggling big time. Current Code:
df_final = df_final / df_final.max(axis=0)
df_final.loc[30102040].plot(subplots=True, layout=(17, 1), figsize=(25, 25), sharex=True, sharey=True)
plt.legend(loc='center left')
plt.axvline(sepsis_onset, color='red', linestyle='dashed')
plt.show()
what it currently looks like:
Neither the legend, nor the axvline is currently displayed correctly. What am I missing?
Upvotes: 0
Views: 904
Reputation: 20517
It was not possible to verify that sepsis_onset
is actually on your y-axis, but with subplots=True
the question arises on which of the four plots you actually want the line. plt.axvline
will not plot to all of them, but calling the function directly on the individual axis objects gives you more control. Conveniently, the plot()
function returns an array of all axis objects which we need to plot your vertical line in each of the subplots and control its legend location. An example:
import pandas as pd
import numpy as np
#Create some dummy dataframe
df = pd.DataFrame(np.random.randn(1000, 4), columns=list("ABCD"))
df = df.cumsum()
axes = df.plot(subplots=True, figsize=(6, 6));
#Loop over all axis objects and create a vertical line in each of them and set legend location
for ax in axes:
ax.axvline(400, color='red', linestyle='dashed')
ax.legend(loc='center left')
Of course you could also the vertical line only to one specific subplot:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(1000, 4), columns=list("ABCD"))
df = df.cumsum()
axes = df.plot(subplots=True, figsize=(6, 6));
for ax in axes:
ax.legend(loc='center left')
axes[1].axvline(400, color='red', linestyle='dashed')
Upvotes: 1