简体   繁体   中英

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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