如何使用懸停標簽中的懸停資料添加 fig.add_scatter?
最小代碼不起作用。
我需要使用與第一個相同的懸停模板添加另一組資料。
非常感謝
import numpy as np
import pandas as pd
a, b, c = [1, 2], [1, 5], [5, 6]
d, e, f = [5, 5], [4, 4], [5, 5]
s1 = ['A', 'F']
s2 = ['V', 'T']
d = {'a': a, 'b': b, 'c': c, 's1':s1}
df = pd.DataFrame(data=d)
d2 = {'d': d, 'e': e, 'f': f, 's2':s2}
df2 = pd.DataFrame(data=d2)
fig = px.scatter(df, x='a', y='b', hover_data=['c', 's1'], color='s1', color_discrete_sequence=["green", "navy"])
fig.add_scatter(x=df2['d'], y=df2['e'], customdata=['f', 's2'], mode="markers", marker=dict(size=10,color='Purple'), name = 'A') # ------> these custom data are not in label, there is just %{customdata[1]}
fig.update_traces(
hovertemplate="<br>".join([
"<b>G:</b> %{x:.3f}",
"<b>R:</b> %{y:.6f}<extra></extra>",
"<b>D:</b> %{customdata[1]}",
"<b>E:</b> %{customdata[0]}",
])
)
fig.update_xaxes(title_font_family="Trebuchet")
fig.update_traces(marker=dict(size=9),
selector=dict(mode='markers'))
fig.show()
uj5u.com熱心網友回復:
創建時有錯誤df2。假設了你想要達到的目標。下面使hovertext作業。
import numpy as np
import pandas as pd
a, b, c = [1, 2], [1, 5], [5, 6]
d, e, f = [5, 5], [4, 4], [5, 5]
s1 = ["A", "F"]
s2 = ["V", "T"]
d = {"a": a, "b": b, "c": c, "s1": s1}
df = pd.DataFrame(data=d)
d2 = {"d": d, "e": e, "f": f, "s2": s2}
# SO question invalid !!!
# df2 = pd.DataFrame(data=d2)
# try this
df2 = pd.DataFrame(d).join(pd.DataFrame({k:v for k,v in d2.items() if k!="d"}))
fig = px.scatter(
df,
x="a",
y="b",
hover_data=["c", "s1"],
color="s1",
color_discrete_sequence=["green", "navy"],
)
fig.add_scatter(
x=df2["a"],
y=df2["e"],
customdata=df2.loc[:,["f", "s2"]].values.reshape([len(df2),2]),
mode="markers",
marker=dict(size=10, color="Purple"),
name="A",
) # ------> these custom data are not in label, there is just %{customdata[1]}
fig.update_traces(
hovertemplate="<br>".join(
[
"<b>G:</b> %{x:.3f}",
"<b>R:</b> %{y:.6f}<extra></extra>",
"<b>D:</b> %{customdata[1]}",
"<b>E:</b> %{customdata[0]}",
]
)
)
fig.update_xaxes(title_font_family="Trebuchet")
fig.update_traces(marker=dict(size=9), selector=dict(mode="markers"))
fig.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/445434.html
標籤:python-3.x 情节地
下一篇:使用python-binance時,heroku[regex._regex_core.error:badescape\datposition7]出錯
