简体   繁体   English

Plotly:如何使用 Plotly Express 组合散点图和线图?

[英]Plotly: How to combine scatter and line plots using Plotly Express?

Plotly Express has an intuitive way to provide pre-formatted plotly plots with minimal lines of code; Plotly Express 有一种直观的方式来提供预格式化的 plotly 绘图,并且代码行数最少; sort of how Seaborn does it for matplotlib. Seaborn 是如何为 matplotlib 做的。

It is possible to add traces of plots on Plotly to get a scatter plot on an existing line plot.可以在 Plotly 上添加绘图轨迹,以在现有行 plot 上获得散点 plot。 However, I couldn't find such a functionality in Plotly Express.但是,我在 Plotly Express 中找不到这样的功能。

Is it possible to combine a scatter and line graph in Plotly Express?是否可以在 Plotly Express 中组合散点图和折线图?

You can use:您可以使用:

fig3 = go.Figure(data=fig1.data + fig2.data)

Where fig1 and fig2 are built using px.line() and px.scatter() , respectively.其中fig1fig2分别使用px.line()px.scatter() And fig3 is, as you can see, built using plotly.graph_objects .如您所见,图 3 是使用fig3 plotly.graph_objects

Some details:一些细节:

One approach that I use alot is building two figures fig1 and fig2 using plotly.express and then combine them using their data attributes together with a go.Figure / plotly.graph_objects object like this: One approach that I use alot is building two figures fig1 and fig2 using plotly.express and then combine them using their data attributes together with a go.Figure / plotly.graph_objects object like this:

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()

Plot: Plot:

在此处输入图像描述

If you want to scale the apporach如果你想扩展方法

fig3 = go.Figure(data=fig1.data + fig2.data)

as stated in the other answer, here are some hints.如另一个答案所述,这里有一些提示。

fig1.data and fig2.data are common tuples that hold all the info needed for a plot and the + just concatenates them. fig1.datafig2.data是常见的元组,它们包含 plot 所需的所有信息, +只是将它们连接起来。

# 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()

EDIT: fixed typo.编辑:修正错字。

This works great and is even more useful with flipSTAR's clarification regarding adding a global layout to the combined fig.这很好用,而且对于 FlipSTAR 关于向组合图中添加全局布局的说明更加有用。 However, sometimes a global layout doesn't cover everything.但是,有时全局布局并不能涵盖所有内容。 For example, in my case (a stacked bar and two single scatter plot lines), my global layout caused me to lose my scatter plot legends.例如,在我的例子中(一个堆叠条和两个单散点 plot 线),我的全局布局导致我丢失了散点 plot 图例。 Fortunately, you can add additional arguments to the combined fig by targeting the specific figures.幸运的是,您可以通过针对特定数字将额外的 arguments 添加到组合图中。 eg, given a hypothetical:例如,给定一个假设:

fig1 = px.bar(...)
fig2 = px.line(...)
fig3 = px.line(...)
all_fig = go.Figure(data=fig1.data + fig2.data + fig3.data, layout = fig1.layout)

Which is a bar chart and two scatter plots each with a single line, you can add the legends for each line with:这是一个条形图和两个散点图,每个散点图都有一条线,您可以为每条线添加图例:

all_fig['data'][1]['showlegend']=True
all_fig['data'][1]['name']='Line 1 Name'
all_fig['data'][1]['hovertemplate']='Line 1 Name<br>x=%{x}<br>y=%{y}<extra></extra>'
all_fig['data'][2]['showlegend']=True
all_fig['data'][2]['name']='Line 2 Name'
all_fig['data'][2]['hovertemplate']='Line 2 Name<br>x=%{x}<br>y=%{y}<extra></extra>'

(the bar is all_fig['data'][0]). (条形图为 all_fig['data'][0])。

For some reason, the name won't show up on hover unless you explicitly add it to 'hovertemplate.'由于某种原因,该名称不会出现在 hover 上,除非您明确将其添加到“悬停模板”中。

Anyone have an answer for what to do when one of those figures is animated and the other isn't?任何人都知道当其中一个人物是动画而另一个不是时该怎么办?

timeResolution = 10
xPos = np.linspace(0,np.pi*2,resolution)
freq = np.linspace(1,5,timeResolution)[:,np.newaxis]

xID = np.tile(xPos,timeResolution) * np.tile(freq,resolution).flatten()
yVal = np.sin(xID)

fig1 = px.line(x= np.tile(xPos,timeResolution) ,y=yVal, animation_frame= np.tile(np.arange(0,timeResolution,1.)[:,np.newaxis],resolution).flatten(), animation_group= np.tile(xPos,timeResolution))
fig2 = px.line(x= np.linspace(0,np.pi*2,resolution) ,y= np.sin(np.linspace(0,np.pi*2,resolution)))
fig3 = go.Figure(data=fig1.data + fig2.data)
fig3.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Plotly:如何手动设置 plotly express 散点图中点的颜色? - Plotly: How to manually set the color of points in plotly express scatter plots? 如何使用 plotly express 为 plot 中的线设置动画? - How to animate line in scatter plot using plotly express? Plotly:如何 plot 使用 plotly 和 Z26C592956DC26CA26AEF5DBACC204A03 回归线? - Plotly: How to plot a regression line using plotly and plotly express? python 中的 Plotly 分类散点图/线图 - Plotly categorical scatter/line plots in python Plotly:使用 plotly 快递线路时如何在 hoverinfo 中格式化日期? - Plotly: How to format date in hoverinfo when using plotly express line? 如何使用 plotly express 突出显示散点图 plot 上的单个数据点 - How to highlight a single data point on a scatter plot using plotly express Plotly Express 散点图不绘制完整范围的 x 变量 - Plotly Express Scatter plots does not plot full range of x variables Plotly:如何在不使用 plotly express 时为散点图设置标题颜色? - Plotly: How to set title color for scatter traces when not using plotly express? 如何使用 plotly-express 将两个 Dataframes 和 plot 数据组合为一行 - How to combine two Dataframes and plot data as one line using plotly-express Plotly:如何使用 DASH 回调将多项式拟合线添加到 plotly go.scatter 图? - Plotly: How to add polynomial fit line to plotly go.scatter figure using a DASH callback?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM