Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a line to a Pandas plot

I'm currently tracking my internet speed and want to generate a plot of my measurements with a Timestamp, Upload value and Download value.

I'm using this to create the plot

df.plot(
    kind='line', 
    x=timestamp_column_name, 
    y=[download_column_name, upload_column_name],
    figsize=(12,5)
)

Generated plot: enter image description here

Now I would like to add a line to this plot with the constant height of y=100000 but I can't figure out how to do this correctly. How should I do this with Pandas?

like image 375
herhuf Avatar asked Sep 01 '17 11:09

herhuf


People also ask

How do you plot a line in Python?

You can plot a vertical line in matplotlib python by either using the plot() function and giving a vector of the same values as the y-axis value-list or by using the axvline() function of matplotlib. pyplot that accepts only the constant x value. You can also use the vlines() function of the matplotlib.

How do you graph a vertical line in a time series?

To create a time series plot, we can use simply apply plot function on time series object and if we want to create a vertical line on that plot then abline function will be used with v argument.


1 Answers

You can use axhline. Since df.plot() is a wrapper for matplotlib and returns the Matplotlib axes, which contain all the methods for interacting with the plot, it can be used straight forward as:

ax = df.plot( ... )
ax.axhline(y=100000)
like image 115
ImportanceOfBeingErnest Avatar answered Sep 27 '22 21:09

ImportanceOfBeingErnest