簡體   English   中英

Python中的多線繪圖

[英]multi-line plotting in Python

所以我想繪制這樣的多線圖: 在此處輸入圖片說明

現在我正在嘗試自己做,但是白人、黑人、亞洲人等種族都在“種族”標題下的專欄中

這是從原始數據幀過濾后我的數據的樣子:

plt1_data = df1.loc[(df1.Region == "All") & (df1.Age == "All") & (df1.Sex == "All"),["Ethnicity","Value","Time"]]
                       Ethnicity Value  Time
12                           All   4.8  2004
192                        Asian   9.4  2004
372                  Asian Other   9.3  2004
552                        Black  12.8  2004
732                       Indian   6.9  2004
...                          ...   ...   ...
34212  Pakistani and Bangladeshi   8.4  2018
34392                    Unknown   4.1  2018
34572                      White   3.7  2018
34752              White British   3.8  2018
34932                White Other   3.4  2018

這是我用來獲得類似於上面的圖形的代碼:

plt.figure(figsize=(20,10))
plt.plot( 'Time', 'Ethnicity', data=plt1_data, color='red')
plt.plot( 'Time', 'Ethnicity', data=plt1_data, color='yellow')
plt.plot( 'Time', 'Ethnicity', data=plt1_data, color='magenta')
plt.plot( 'Time', 'Ethnicity', data=plt1_data, color='blue')
plt.legend()

但它不能很好地工作,並給了我下圖: 在此處輸入圖片說明

所以我需要幫助修復我的圖表。 非常感謝!

更新圖: 在此處輸入圖片說明

更新圖的代碼:

plt.figure(figsize=(30,30))
for cat, df_cat in plt1_data.groupby('Ethnicity'):
    plt.plot(df_cat['Time'],df_cat['Value'], label=cat)
plt.legend()

考慮使用seaborn

import seaborn as sns

sns.lineplot(data=plt1_data, x='Time', y='Value', hue='Ethnicity')

你需要的是做一個“groupby”並遍歷產生的種族和數據框

plt.figure(figsize=(20,10))
for cat, df_cat in plt1_data.groupby('Ethnicity'):
    plt.plot(df_cat['Time'],df_cat['Value'], label=cat)
plt.legend()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM