[英]Two or three colorbars for one plot in Plotly
我有一個 Scattergl plot,我希望它有兩個或三個顏色條。 每個顏色條都有與不同數據特征相關的不同色標。 plot 中的點被着色為兩個或三個色標 colors 的組合。我已經有了 colors 部分的組合,但我不知道如何 plot 顏色條......我不確定它是否是允許 plot 超過一個。 你知道怎么做嗎?
我會感謝你的幫助。 提前致謝。 親切的問候。
很高興看到更多使用 plotly 的 sansanos,為此目的,標記的顏色條中有一個屬性trace.marker.colorbar.x
,它需要一個范圍從 -2 到 3 的值,0 是繪圖的左極限,1作為正確的限制,您通常必須通過反復試驗來獲得它。
請記住,提供代碼示例總是有用的,因此我們可以根據您正在處理的范式提供更正。 這是一個例子
from plotly import graph_objects as go
data_x = [1, 2, 3, 4, 5, 6]
data_y = [1, 4, 9, 16, 25, 36]
figure = go.FigureWidget()
# First
first_scatter = go.Scattergl()
first_scatter.mode = 'markers'
first_scatter.x = data_x
first_scatter.y = data_y
first_scatter.marker.colorscale = 'Viridis'
first_scatter.marker.color = data_y
first_scatter.marker.size = 20
first_scatter.marker.showscale = True
first_scatter.showlegend = False
figure.add_trace(first_scatter)
# Second
second_scatter = go.Scattergl()
second_scatter.mode = 'markers'
second_scatter.x = data_x
second_scatter.y = data_y[::-1]
second_scatter.marker.colorscale = 'Electric'
second_scatter.marker.color = data_y
second_scatter.marker.size = 20
second_scatter.marker.showscale = True
second_scatter.marker.colorbar.x = 1.15 # Here
second_scatter.showlegend = False
figure.add_trace(second_scatter)
訪問 plotly 的布局。通過fig.layout
覆蓋 plot 的屬性很方便
用於移動顏色條的目的:為了分離 2 個顏色條,在創建第一個軌跡/圖形之后,我們可以訪問其當前參考 position 並在添加另一個軌跡之前用 new_value 覆蓋
fig.layout.coloraxis.colorbar.x = 1.5
=> 這會將欄向右移動(默認為 1)
fig.layout.coloraxis.colorbar.x = -0.5
=> 這會將條形圖向左移動
同樣的想法也適用於y
值和colorscale
。
為了更友好的使用目的,您可以改用更新方法:
fig.update_coloraxes(colorbar_x=<VALUE>)
有關更多詳細信息,您可以在此處查看
這是一個示例,我在其中更改了顏色條及其顏色參考。
#creating a figure and add the first trace
fig = px.density_contour(df[(df["label"]=='capture')&(df["bie"]=='bie')], x="x", y="y")
# change reference params
fig.layout.coloraxis.colorbar.x = 1.1
fig.update_coloraxes(colorscale="YlGnBu")
fig.update_traces(contours_coloring="fill")
# add new traces with new params
fig.add_traces(
list(px.scatter(df[df["label"]=='capture'], x="x", y="y",color="state", opacity=0.8).select_traces()))
fig.update_layout(width=1000,height=500,legend=dict(
yanchor="bottom"))
fig.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.