使用 plotly 创建多个箱线图
Creating multiple boxplots using plotly
我正在尝试使用 plotly 复制我在 matplotlib 中制作的以下箱线图:
我的数据位于从 Excel 文件导入的非常简单的数据框中,如下所示:
如您所见,我希望在 x 轴和 y 轴上有不同的条件,涵盖从 0 到 10,并且每个条件都有不同的箱线图,有自己的均值、SD 等。但是,当我尝试以下代码时,我得到了一个非常奇怪的输出:
import plotly.express as px
conditions = df1.keys() #all conditions, df1 being my DataFrame
conditions=list(conditions)
fig = px.box(df1, x = conditions)
fig.show()
输出:
任何人都知道我需要做什么才能得到与我用 matplotlib 得到的相似的情节,但用 plotly 得到的情节?
您可以使用 plotly.express
或 plotly.graph_objects
。
读取数据:
import pandas as pd
df = pd.read_csv(r'Documents\test.csv', sep=',')
print(df)
print(df.to_dict())
p=0; t=0 p=1; t=6" p=1; t=30" p=3; t=6" p=3; t=30" p=3; t=1'
0 0 3 3 2 10 10
1 2 3 5 4 9 9
2 2 6 1 1 10 9
3 1 1 4 2 7 8
{'p=0; t=0': {0: 0, 1: 2, 2: 2, 3: 1}, 'p=1; t=6"': {0: 3, 1: 3, 2: 6, 3: 1},
'p=1; t=30"': {0: 3, 1: 5, 2: 1, 3: 4}, 'p=3; t=6"': {0: 2, 1: 4, 2: 1, 3: 2},
'p=3; t=30"': {0: 10, 1: 9, 2: 10, 3: 7}, "p=3; t=1'": {0: 10, 1: 9, 2: 9, 3: 8}}
plotly.graph_objects
:
import plotly.graph_objects as go
fig = go.Figure()
for col in df:
fig.add_trace(go.Box(y=df[col].values, name=df[col].name))
fig.show()
plotly.express
:
import plotly.express as px
fig = px.box(pd.melt(df), x="variable", y="value", points="outliers")
fig.show()
import plotly.express as px
df = px.data.tips()
fig = px.box(df, x="day", y="total_bill", color="smoker")
fig.update_traces(quartilemethod="exclusive") # or "inclusive", or "linear" by default
fig.show()
我正在尝试使用 plotly 复制我在 matplotlib 中制作的以下箱线图:
我的数据位于从 Excel 文件导入的非常简单的数据框中,如下所示:
如您所见,我希望在 x 轴和 y 轴上有不同的条件,涵盖从 0 到 10,并且每个条件都有不同的箱线图,有自己的均值、SD 等。但是,当我尝试以下代码时,我得到了一个非常奇怪的输出:
import plotly.express as px
conditions = df1.keys() #all conditions, df1 being my DataFrame
conditions=list(conditions)
fig = px.box(df1, x = conditions)
fig.show()
输出:
任何人都知道我需要做什么才能得到与我用 matplotlib 得到的相似的情节,但用 plotly 得到的情节?
您可以使用 plotly.express
或 plotly.graph_objects
。
读取数据:
import pandas as pd
df = pd.read_csv(r'Documents\test.csv', sep=',')
print(df)
print(df.to_dict())
p=0; t=0 p=1; t=6" p=1; t=30" p=3; t=6" p=3; t=30" p=3; t=1'
0 0 3 3 2 10 10
1 2 3 5 4 9 9
2 2 6 1 1 10 9
3 1 1 4 2 7 8
{'p=0; t=0': {0: 0, 1: 2, 2: 2, 3: 1}, 'p=1; t=6"': {0: 3, 1: 3, 2: 6, 3: 1},
'p=1; t=30"': {0: 3, 1: 5, 2: 1, 3: 4}, 'p=3; t=6"': {0: 2, 1: 4, 2: 1, 3: 2},
'p=3; t=30"': {0: 10, 1: 9, 2: 10, 3: 7}, "p=3; t=1'": {0: 10, 1: 9, 2: 9, 3: 8}}
plotly.graph_objects
:
import plotly.graph_objects as go
fig = go.Figure()
for col in df:
fig.add_trace(go.Box(y=df[col].values, name=df[col].name))
fig.show()
plotly.express
:
import plotly.express as px
fig = px.box(pd.melt(df), x="variable", y="value", points="outliers")
fig.show()
import plotly.express as px
df = px.data.tips()
fig = px.box(df, x="day", y="total_bill", color="smoker")
fig.update_traces(quartilemethod="exclusive") # or "inclusive", or "linear" by default
fig.show()