Plot point on time series line graph

回眸只為那壹抹淺笑 提交于 2021-02-08 11:33:36

问题


I have this dataframe and I want to line plot it. As I have plotted it.

Graph is

Code to generate is

fig, ax = plt.subplots(figsize=(15, 5))
date_time = pd.to_datetime(df.Date)
df = df.set_index(date_time)
plt.xticks(rotation=90)
pd.DataFrame(df,  columns=df.columns).plot.line( ax=ax, 
xticks=pd.to_datetime(frame.Date))

I want a marker of innovationScore with value(where innovationScore is not 0) on open, close line. I want to show that that is the change when InnovationScore changes.


回答1:


You have to address two problems - marking the corresponding spots on your curves and using the dates on the x-axis. The first problem can be solved by identifying the dates, where the score is not zero, then plotting markers on top of the curve at these dates. The second problem is more of a structural nature - pandas often interferes with matplotlib when it comes to date time objects. Using pandas standard plotting functions is good because it addresses common problems. But mixing pandas with matplotlib plotting (and to set the markers, you have to revert to matplotlib afaik) is usually a bad idea because they do not necessarily present the date time in the same format.

import pandas as pd
from matplotlib import pyplot as plt

#fake data generation, the following code block is just for illustration
import numpy as np
np.random.seed(1234)
n = 50
date_range = pd.date_range("20180101", periods=n, freq="D")
choice = np.zeros(10)
choice[0] = 3
df = pd.DataFrame({"Date": date_range, 
                   "Open": np.random.randint(100, 150, n), 
                   "Close": np.random.randint(100, 150, n), 
                   "Innovation Score": np.random.choice(choice, n)})


fig, ax = plt.subplots()

#plot the three curves
l = ax.plot(df["Date"], df[["Open", "Close", "Innovation Score"]])
ax.legend(iter(l), ["Open", "Close", "Innovation Score"])

#filter dataset for score not zero
IS = df[df["Innovation Score"] > 0]
#plot markers on these positions
ax.plot(IS["Date"], IS[["Open", "Close"]], "ro")
#and/or set vertical lines to indicate the position
ax.vlines(IS["Date"], 0, max(df[["Open", "Close"]].max()), ls="--")

#label x-axis score not zero
ax.set_xticks(IS["Date"])
#beautify the output
ax.set_xlabel("Month")
ax.set_ylabel("Artifical score people take seriously")
fig.autofmt_xdate() 
plt.show()

Sample output:




回答2:


You can achieve it like this:

import matplotlib.pyplot as plt

plt.plot([1, 2, 3], "ro-") # r is red, o is for larger marker, - is for line
plt.plot([3, 2, 1], "b.-") # b is blue, . is for small marker, - is for line

plt.show()

Check out also example here for another approach:

https://matplotlib.org/3.3.3/gallery/lines_bars_and_markers/markevery_prop_cycle.html

I very often get inspiration from this list of examples:

https://matplotlib.org/3.3.3/gallery/index.html



来源:https://stackoverflow.com/questions/65628567/plot-point-on-time-series-line-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!