简体   繁体   English

如何在 python 中使用 for 循环 plot 数据?

[英]how can I plot data with a for loop in python?

My database:我的数据库:

Hi everybody, i have the database with columns "LOCATION, "year", "month", "Value" and I would like to show on the same plot three lines (for the value corresponding to the years 2019, 2020, 2021) with on the x axe the month and on the y axe the column "Value" corresponding to that year. I also would like to repeat the same plot for each country contained in my database, how can I do that?大家好,我有一个包含“位置”、“年”、“月”、“值”列的数据库,我想在同一 plot 上显示三行(对应于 2019、2020、2021 年的值)在 x 轴上的月份和在 y 轴上对应于那一年的列“值”。我还想为我的数据库中包含的每个国家/地区重复相同的 plot,我该怎么做?

That was my failed attempt:那是我失败的尝试:

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)

sns.relplot(kind='line', x='month', y='value', hue='year', col='LOCATION', col_wrap=4) sns.relplot(kind='line', x='month', y='value', hue='year', col='LOCATION', col_wrap=4)

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

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