[英]how can i plot multiple graph into one with matplotlib or seaborn
我的dataframe是這樣的:點擊在線下載dataframe
我嘗試了以下代碼:
plt.plot(LessDF['DeptAvg'][LessDF['classes'] == 'COA111'], LessDF['week1'])
plt.plot(LessDF['DeptAvg'][LessDF['classes'] == 'COA111'], LessDF['week2'])
plt.plot(LessDF['DeptAvg'][LessDF['classes'] == 'COA111'], LessDF['week3'])
我得到了下面的 output,它只顯示了一行,以及我的代碼。
我怎樣才能用 matplotlib 或 seaborn 得到這個 output?
對於您應用的過濾器,DeptAvg 列中的所有值都是 67。
此外,您還提供了一個 boolean 作為您的 x: LessDF['DeptAvg'] == 'COA111'
。
此外,您在錯誤的列DeptAvg
而不是classes
上應用條件
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv('../../../Desktop/LessDF.csv')
df_filtered = df[df['classes'] == 'COA111' ]
plt.plot(df_filtered['week1'],df_filtered['DeptAvg'],alpha=.5,)
plt.plot(df_filtered['week2'],df_filtered['DeptAvg'],alpha=.5)
plt.plot(df_filtered['week3'],df_filtered['DeptAvg'],alpha=.5)
plt.legend(['week1','week2','week3'])
plt.show()
更多信息在這里
對於 Seaborn,其object 接口從 v0.12 開始可用,您可以這樣做:
import pandas as pd
import seaborn as sns
import seaborn.objects as so
sns.set_theme()
首先,將數據框轉換為長格式,以便在第二張圖中進行處理。
df = pd.read_csv("LessDF.csv", index_col=0)
df_long = (
# Convert to a long-form
pd.melt(df,
id_vars=["Id", "classes", "LessAvg", "DeptAvg"],
var_name=["week"],
value_name="point"
)
# Make `week1` to `1`
.assign(week=lambda df_: df_.week.str.replace("week", ""))
)
然后
(
so.Plot(
# We don't have to drop rows but since `DeptAvg` doesn't change
# over `classes` and `week`, we can de-duplicate them
df_long.drop_duplicates(["classes", "week"]),
x="week", y="DeptAvg", color="classes"
)
.add(so.Line())
.limit(y=(0, 100))
)
如果您還想呈現每周的個人Id
點,您可以這樣做:
(
so.Plot(data=df_long, x="week", y="point", color="classes")
.add(so.Dots(), so.Dodge(), so.Jitter(.3))
.add(so.Line(linewidth=3, alpha=.8), y="DeptAvg")
.limit(y=(0, 100))
)
# I done this using seaborn you can use matplotlib in between to code
plt.figure(figsize=(16, 16))
plt.subplot(no_of_rows, no_of_columns, plot_num)
plt.title('Any title 1')
sns.boxplot(df['column_name'])
Example :- we want 2 rows with columns of plots then we use
plt.subplot(2, 2, 1)
plt.title('Any title 1')
sns.distplot(df['column_name'], bins=20)
plt.subplot(2, 2, 2)
plt.title('Any title 2')
sns.distplot(df['column_name'], bins=20)
plt.subplot(2, 2, 3)
plt.title('Any title 3')
sns.distplot(df['column_name'], bins=20)
plt.subplot(2, 2, 4)
plt.title('Any title 4')
sns.distplot(df['column_name'], bins=20)
plt.show()
fig, ax = plt.subplots(2,3, figsize=(16,10))
sns.boxplot(df,y="arpu_6",ax=ax[0,0])
ax[0,0].set_title("June ARPU")
ax[0,0].set_ylim([0, 5000])
sns.boxplot(df,y="arpu_7",ax=ax[0,1])
ax[0,1].set_title("July ARPU")
ax[0,1].set_ylim([0, 5000])
sns.boxplot(df,y="arpu_8",ax=ax[0,2])
ax[0,2].set_title("Aug ARPU")
ax[0,2].set_ylim([0, 5000])
密碼是我自己筆記本上摘下來的,和你的dataframe不符,按你的需要修改即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.