英文:
Plotly with Pandas dataframe side by side in Jupyter notebook
问题
以下是翻译好的部分:
有关如何在Jupyter笔记本中并排创建两个Plotly图表的问题或如何在Jupyter笔记本中并排显示两个Pandas数据框的问题,有一些问题。但我想在Jupyter笔记本中将一个Plotly图与一个Pandas数据框并排显示。以下是一些用于图表和Pandas数据框的可重现代码:
import pandas as pd
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length")
fig.show()
# 简单的Pandas数据框
df[["sepal_length", "species"]].groupby("species").agg(['mean', 'count', 'median', 'min', 'max'])
输出:
现在输出是垂直排列的,但我想要它们并排显示。所以我想知道是否有人知道如何在Pandas数据框旁边并排显示一个Plotly图表?
英文:
There are some questions about how to create two plotly graphs side-by-side in Jupyter notebook or how to show two pandas dataframes side by side. But I would like to display a plotly graph with a pandas dataframe side by side in a Jupyter notebook. Here is some reproducible code for the graph and pandas dataframe:
import pandas as pd
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length")
fig.show()
# Simple pandas dataframe
df[["sepal_length", "species"]].groupby("species").agg(['mean', 'count', 'median', 'min', 'max'])
Output:
Now the output is below each other, but I would like to have them side by side. So I was wondering if anyone knows how to show a plotly graph side by side with a pandas dataframe?
答案1
得分: 3
你可以创建一个子图,并将表格视图放在第二列。这个示例应该可以运行:
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
df = px.data.iris()
specs = [
[{"type": "xy"}, {"type": "table"}]
]
fig = make_subplots(
rows=1,
cols=2,
specs=specs,
subplot_titles=[
"散点图",
"数据表格"
]
)
fig.add_trace(go.Scatter(
x=df.index,
y=df["petal_length"].values
),
row=1, col=1)
fig.add_trace(go.Table(
header={"values": df.columns},
cells={"values": (
df
.transpose()
.values
.tolist())}
),
row=1, col=2)
fig.update_layout(template="plotly_dark")
fig.show()
英文:
You can make a subplot and place the table view on the second column. This example should run:
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
df = px.data.iris()
specs = [
[{"type": "xy"}, {"type": "table"}]
]
fig = make_subplots(
rows=1,
cols=2,
specs=specs,
subplot_titles=[
"A Scatter Plot",
"A DataFrame"
]
)
fig.add_trace(go.Scatter(
x=df.index,
y=df["petal_length"].values
),
row=1, col=1)
fig.add_trace(go.Table(
header={"values": df.columns},
cells={"values": (
df
.transpose()
.values
.tolist()}
)
),
row=1, col=2)
fig.update_layout(template="plotly_dark")
fig.show()
答案2
得分: 0
我认为这里的原始帖子发布了一个更新的答案,使用make_subplots()
来实现你正在寻找的功能:
https://stackoverflow.com/q/71296687/8508004
它做得比你需要的要多;但是,你应该能够根据你的代码进行修改以去掉多余的部分,并集成到你的代码中。
英文:
I think the original poster here posted an updated answer that does what you are looking for using make_subplots()
:
https://stackoverflow.com/q/71296687/8508004
It does more than you need; however, you should be able to adapt it to remove that and build in your code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论