gpt4 book ai didi

Python 绘制两个变量之间的每月数据

转载 作者:行者123 更新时间:2023-12-02 02:49:28 25 4
gpt4 key购买 nike

我想绘制两个变量之间的数据。我想使用特殊颜色绘制每月数据。

我的代码和预期输出:

import matplotlib.pyplot as plt

df
A B
2019-01-01 10 20
2019-01-02 20 30
2019-02-01 10 15
2019-02-02 20 40
2019-03-01 12 32
2019-03-02 5 14

plt.plot(df['A'],df['B'])
plt.show()

我当前的绘图像往常一样绘制了所有数据,但我期望出现不同的情况,如下所示。我的预期输出:

enter image description here2019-03-01 10 20

最佳答案

你可以这样做:

markers = 'dsxo'
months = pd.to_datetime(df.index).to_period('M')

for i, (k,d) in enumerate(df.groupby(months) ):
plt.plot(d['A'],d['B'], label=k, marker=markers[i])

plt.legend()

输出:

enter image description here

关于Python 绘制两个变量之间的每月数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62291345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com