[英]Legend label Heatmap Plotly
我正在嘗試用 Plotly 中的熱圖構建華夫餅圖。代碼運行良好,但現在我不想顯示基於數字數據的圖例,而是想顯示文本(即“到目標的中等距離”而不是 2) . 知道如何前進嗎?謝謝!
m= 10
n= 10
z = np.ones((m, n))
z[2:, 6] = 2
z[:4, 7] = 2
z[4:, 7] = 3
z[:4, 8] = 3
z[4:, 8] = 4
z[:5, 9] = 5
z[5:, 9] = 6
#dictionary that maps the heatmap z-values to strings
d = {1: "Insufficient data",
2: "Moderate distance to target",
3: "Close to target",
4: "Far from target",
5: "Very far from target",
6: "Target met or almost met",
}
M = max([len(s) for s in d.values()])
customdata= np.empty((m,n), dtype=f'<U{M}') #supplementary information on each waffle cell
colorscale = [[0, "#adb5bd"],
[0.16, "#adb5bd"],
[0.16, "#ffc107"],
[0.33, "#ffc107"],
[0.33, "#28a745"],
[0.5, "#28a745"],
[0.5, "#d76406"],
[0.66, "#d76406"],
[0.66, "#dc3545"],
[0.83, "#dc3545" ],
[0.83, "#007bff" ],
[1, "#007bff"]]
ticktext = ["Insufficient data", "Moderate distance to target", "Close to target", "Far from target", "Very far from target","Target met or almost met"]
xlabels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
ylabels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
fig = go.Figure(go.Heatmap(x=xlabels, y = ylabels, z=z,
customdata=customdata, xgap=3, ygap=3,
colorscale=colorscale, showscale=True,
hovertemplate="(%{y}, %{x}): %{customdata})<extra></extra>"))
fig.update_layout(margin=dict(l=40, r=20, t=20, b=40),yaxis_autorange='reversed')
fig.update_yaxes(showticklabels=False)
fig.update_xaxes(showticklabels=False)
[enter image description here][1]fig.update_traces(colorbar_orientation='h', selector=dict(type='heatmap'))
根據您提供的代碼,您似乎正在使用 go.Heatmap 庫中的 883276440288 圖表類型創建熱圖。 您已經使用 z 參數(一個二維值數組)和 customdata 參數定義了熱圖的數據,customdata 參數是一個字符串數組,其中包含熱圖中每個單元格的補充信息。
要顯示 d 字典中的文本標簽而不是 z 中的數值,您可以使用 go.Heatmap 圖表類型的文本參數來指定每個單元格的文本標簽。
以下是如何修改代碼以實現此目的的示例:
import plotly.graph_objects as go
# Map the values in z to the corresponding text labels from the d dictionary
text = [[d[z[i][j]] for j in range(n)] for i in range(m)]
fig = go.Figure(go.Heatmap(x=xlabels, y = ylabels, z=z, text=text,
customdata=customdata, xgap=3, ygap=3,
colorscale=colorscale, showscale=True,
hovertemplate="(%{y}, %{x}): %{customdata})<extra></extra>"))
fig.update_layout(margin=dict(l=40, r=20, t=20, b=40),yaxis_autorange='reversed')
fig.update_yaxes(showticklabels=False)
fig.update_xaxes(showticklabels=False)
fig.update_traces(colorbar_orientation='h', selector=dict(type='heatmap'))
在此修改后的代碼中,文本參數設置為二維數組,其中包含 d 字典中的文本標簽,映射到 z 中的相應值。 這將導致顯示文本標簽而不是熱圖中的數值。
我希望這有幫助。 如果您有任何問題,請告訴我。
這是通過設置您希望設置的字符串和顏色條的值(您希望顯示的 position)來實現的。 只需了解您在此處設置的比例值是針對您的圖形和字符串長度的,因此如果您更改圖形大小或字符串,則需要手動修改它們。
fig = go.Figure(go.Heatmap(x=xlabels,
y=ylabels,
z=z,
customdata=customdata,
xgap=2,
ygap=2,
colorscale=colorscale,
showscale=True,
hovertemplate="(%{y}, %{x}): %{customdata})<extra></extra>",
)
)
fig.update_traces(colorbar_orientation='h',
colorbar_tickmode='array',
colorbar_ticktext=ticktext,
colorbar_tickvals=[1.30, 2.25, 3.0, 3.8, 4.75, 5.65],
colorbar_tickangle=0,
selector=dict(type='heatmap')
)
fig.update_yaxes(showticklabels=False)
fig.update_xaxes(showticklabels=False)
fig.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.