Plotly Express有一种直观的方式,可以用最少的代码行提供预先格式化的绘图;有点像Seaborn为matplotlib所做的事情。
可以在Plotly上添加图的轨迹,以在现有线图上获得散点图。但是,我在Plotly Express中找不到这样的功能。
在Plotly Express中可以将散点图和折线图结合起来吗?
发布于 2020-12-04 05:17:54
您可以使用:
fig3 = go.Figure(data=fig1.data + fig2.data)其中fig1和fig2分别使用px.line()和px.scatter()构建。如您所见,fig3是使用plotly.graph_objects构建的。
以下是一些详细信息:
我使用alot的一种方法是使用plotly.express构建两个图形fig1和fig2,然后使用它们的数据属性将它们与go.Figure / plotly.graph_objects对象组合在一起,如下所示:
import plotly.express as px
import plotly.graph_objects as go
df = px.data.iris()
fig1 = px.line(df, x="sepal_width", y="sepal_length")
fig1.update_traces(line=dict(color = 'rgba(50,50,50,0.2)'))
fig2 = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
fig3 = go.Figure(data=fig1.data + fig2.data)
fig3.show()绘图:

发布于 2021-11-09 06:27:51
如果您想要扩展分配
fig3 = go.Figure(data=fig1.data + fig2.data)正如在另一个答案中所述,这里有一些提示。
fig1.data和fig2.data是常用的元组,包含绘图所需的所有信息,+只是将它们连接在一起。
# this will hold all figures until they are combined
all_figures = []
# data_collection: dictionary with Pandas dataframes
for df_label in data_collection:
df = data_collection[df_label]
fig = px.line(df, x='Date', y=['Value'])
all_figures.append(fig)
import operator
import functools
# now you can concatenate all the data tuples
# by using the programmatic add operator
fig3 = go.Figure(data=functools.reduce(operator.add, [_.data for _ in all_figures]))
fig3.show()https://stackoverflow.com/questions/65124833
复制相似问题