英文:
When adding traces to fig=px.scatter() does it have to be added with fig.add_traces(go.Scatter())?
问题
当向 fig=px.scatter() 添加 traces 时,是否必须使用 fig.add_traces(go.Scatter())?
我问这个问题是因为在 px.scatter 和 go.scatter 之间存在微妙的差异,对我来说很难记住(令人讨厌)。另外,使用其中一个是否更好/更全面?
我觉得我应该能够以创建原始 traces 的相同方式添加 traces?即,fig.add_traces(px.Scatter())。这样可以减少记住所有输入的工作。我漏掉了什么吗?
英文:
When adding traces to fig=px.scatter() does it have to be added with fig.add_traces(go.Scatter())?
I ask this because it gets very confusing with the subtle differences, that are a PIA to remember (for me), between px.scatter and go.scatter. Also, is it better/more comprehensive to use one or the other?
I feel like I should be able to add the traces the same way the original ones were created? i.e., fig.add_traces(px.Scatter()). That would cut down on remembering all the inputs. Am I missing something?
here is my sample code:
df = pd.DataFrame({'x':[1,2,3,4],'y':[1,1,1,1],'z':[10,10,10,10],'custom':[30,30,30,30]})
df2 = pd.DataFrame({'x':[1,2,3,4],'y':[2,2,2,2],'z':[15,15,15,15],'custom':[40,40,40,40]})
fig = px.scatter(df,
x='x',
y='y',
size='z',
size_max=10,
custom_data=['custom'],
color_discrete_sequence=['red'],
)
fig.update_traces(
hovertemplate="<br>".join([
"x: %{x}",
"some custom data: %{custom_data[0]}",
])
)
fig.add_traces(go.Scatter( x=df2['x'],
y=df2['y'],
mode='markers',
customdata=df2['custom'],
marker=dict(size=7,
symbol='circle',
color='blue',
)))
fig.update_traces(
hovertemplate="<br>".join([
"x: %{x}",
"some custom data: %{customdata}",
])
)
fig.show()
Thanks!
答案1
得分: 2
如果您在fig.add_traces(go.Scatter())
中感到困惑,您可以改用以下方式:
fig.add_scatter
或者您可以使用通过运行dir(go.Figure())
显示的其他任何方法,比如:
add_bar
add_barpolar
add_box
add_candlestick
完整的代码:
import plotly.graph_objects as go
fig = go.Figure()
dir(fig)
英文:
If it's the go.Scatter()
part that you find confusing in fig.add_traces(go.Scatter())
then you can use this instead:
fig.add_scatter
Or you can use any of the other methods that will reveal themselves by running dir(go.Figure())
, such as:
add_bar
add_barpolar
add_box
add_candlestick
Complete code:
import plotly.graph_objects as go
fig = go.Figure()
dir(fig)
答案2
得分: 0
这是一个仅使用Plotly Express的解决方案。
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'x':[1,2,3,4],'y':[1,1,1,1],'z':[10,10,10,10],'custom':[30,30,30,30]})
df2 = pd.DataFrame({'x':[1,2,3,4],'y':[2,2,2,2],'z':[15,15,15,15],'custom':[40,40,40,40]})
fig = px.scatter(df,
x='x',
y='y',
size='z',
size_max=10,
hover_data=['custom'],
color_discrete_sequence=['red'],
)
fig2 = px.scatter(df2,
x='x',
y='y',
hover_data=['custom'],
color_discrete_sequence=['blue'],
)
fig2.update_traces(marker=dict(
size=7,
symbol='circle',
color='blue',
))
# 将fig2的所有轨迹添加到fig中
fig.add_traces(fig2.data)
fig.show()
英文:
Here is a plotly express only solution.
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'x':[1,2,3,4],'y':[1,1,1,1],'z':[10,10,10,10],'custom':[30,30,30,30]})
df2 = pd.DataFrame({'x':[1,2,3,4],'y':[2,2,2,2],'z':[15,15,15,15],'custom':[40,40,40,40]})
fig = px.scatter(df,
x='x',
y='y',
size='z',
size_max=10,
hover_data=['custom'],
color_discrete_sequence=['red'],
)
fig2 = px.scatter(df2,
x='x',
y='y',
hover_data=['custom'],
color_discrete_sequence=['blue'],
)
fig2.update_traces(marker=dict(
size=7,
symbol='circle',
color='blue',
))
# Add all the traces from fig2 to fig
fig.add_traces(fig2.data)
fig.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论