【发布时间】:2022-01-22 09:02:11
【问题描述】:
我的数据库:
大家好,我有一个包含“位置”、“年”、“月”、“值”列的数据库,我想在同一个图上显示三行(对应于 2019 年、2020 年的值, 2021)在 x 轴上是月份,在 y 轴上是对应于那一年的“值”列。我还想为我的数据库中包含的每个国家/地区重复相同的图,我该怎么做?
那是我失败的尝试:
import matplotlib.pyplot as plt
import pandas as pd
url4 = 'https://drive.google.com/file/d/1MmrleW_pe_vopiXK9z5P_EV-CTftsPWV/view?usp=sharing'
path4 = 'https://drive.google.com/uc?export=download&id=' + url4.split('/')[-2]
rb = pd.read_csv(path4)
rbn = rb.loc[:, ["LOCATION", "Time", "Value"]]
rbn['Time'] = pd.to_datetime(rbn['Time'])
yearss = rbn.Time.dt.year.unique()
rbn["year"] = yearss = rbn.Time.dt.year
rbn["Month"] = rbn.Time.dt.month
countryi = rbn.LOCATION.unique()
rbn.Value = round(rbn.Value, 3)
year1 = yearss.unique()
for el in countryi:
for i in year1:
x = rbn.loc[(rbn["LOCATION"] == el) & (rbn["year"] == i), ["Month"]]
y = rbn.loc[(rbn["LOCATION"] == el) & (rbn["year"] == i), ["Value"]]
plt.plot(x, y)
【问题讨论】:
-
你为什么要标记 pandas、matplotlib 和 seaborn,向我们展示你目前的代码,你使用的是哪个库? Stackoverflow 的人不会为你写 ocde
-
我正在尝试附加我的代码,但我是新手,我将粘贴两个 cmets,对不起
-
恳请您不要在 cmets 中发布代码。弄清楚如何在您的问题中添加代码。
-
您在寻找这样的东西吗?对于 df["LOCATION"].unique() 中的位置: df.loc[df["LOCATION"]==location].set_index("Month").groupby("year")["Value"].plot( )`
-
但是我需要在同一个情节上的三行和每个国家不同的情节,我尝试了你的代码并将所有内容都绘制在同一个情节上,对吗?
标签: python pandas matplotlib seaborn