How did you build a vertical line on a time series chart in Pandas?

How did you build a vertical line (vlines) in Pandas? I use Pandas to build moving environments, etc. And I want to mark important positions with a vertical line. Is it possible to use vlines or something similar for this? If yes, can someone please give an example? In this case, the x axis is the date-time.

+69
python matplotlib pandas plot
Oct 06 '13 at
source share
3 answers
plt.axvline(x_position) 

Standard formatting options required ( linestlye , color , ect)

(doc)

If you have a link to your axes object:

 ax.axvline(x, color='k', linestyle='--') 
+100
Oct 06 '13 at
source share
— -

If you have a time axis and you imported Pandas as pd, you can use:

 ax.axvline(pd.to_datetime('2015-11-01'), color='r', linestyle='--', lw=2) 

For multiple lines:

 xposition = [pd.to_datetime('2010-01-01'), pd.to_datetime('2015-12-31')] for xc in xposition: ax.axvline(x=xc, color='k', linestyle='-') 
+32
Mar 09 '16 at 20:59
source share

The DataFrame chart function returns an AxesSubplot object, and you can add as many rows to it as you want. Take a look at the sample code below:

 %matplotlib inline import pandas as pd df = pd.DataFrame(index=pd.date_range("2019-07-01", "2019-07-31")) df["y"] = pd.np.logspace(0, 1, num=len(df)) ax = df.plot() # you can add here as many lines as you want ax.axhline(6, color="red", linestyle="--") ax.axvline("2019-07-24", color="red", linestyle="--") 

enter image description here

0
Sep 10 '19 at 10:58 on
source share



All Articles