简体   繁体   English

Plot 时间序列线图上的点

[英]Plot point on time series line graph

I have this dataframe and I want to line plot it.我有这个 dataframe,我想把它排成 plot。 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.我想要一个在开、关线上具有价值(其中创新分数不为 0)的创新分数标记。 I want to show that that is the change when InnovationScore changes.我想表明,这就是 InnovationScore 发生变化时的变化。

You have to address two problems - marking the corresponding spots on your curves and using the dates on the x-axis.您必须解决两个问题 - 标记曲线上的相应点并使用 x 轴上的日期。 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.第二个问题更多是结构性的——当涉及到日期时间对象时,pandas 经常干扰 matplotlib。 Using pandas standard plotting functions is good because it addresses common problems.使用 pandas 标准绘图功能很好,因为它解决了常见问题。 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.但是将 pandas 与 matplotlib 绘图混合(并且要设置标记,您必须恢复为 matplotlib afaik)通常是一个坏主意,因为它们不一定以相同的日期时间格式显示。

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:样品 output:

![在此处输入图像描述

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 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://matplotlib.org/3.3.3/gallery/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM