在时间序列线图中绘制点

在时间序列线图中绘制点

本文介绍了在时间序列线图中绘制点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据框,我想对其进行线条绘制.如我所绘.

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

图是

要生成的代码是

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))

我想在开,闭线上标注一个带有价值(其中InnovationScore不为0)的innovationScore.我想证明这就是InnovationScore更改时的更改.

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.

推荐答案

您必须解决两个问题-在曲线上标记相应的点,并在x轴上使用日期.第一个问题可以通过以下方法解决:确定分数不为零的日期,然后在这些日期的曲线顶部绘制标记.第二个问题更多是结构性的-当涉及到时间对象时,熊猫通常会干扰matplotlib.使用熊猫标准绘图功能会很好,因为它可以解决常见问题.但是,将熊猫与matplotlib绘图混合(并设置标记,您必须恢复为matplotlib afaik)通常是一个坏主意,因为它们不一定以相同的格式显示日期时间.

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()

示例输出:

这篇关于在时间序列线图中绘制点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 07:21