亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Plotly:如何向直方圖添加文本標簽?

Plotly:如何向直方圖添加文本標簽?

楊__羊羊 2023-08-15 18:52:52
有沒有辦法在 Plotly.Express 直方圖中顯示直方圖聚合的計數值?px.histogram(pd.DataFrame({"A":[1,1,1,2,2,3,3,3,4,4,4,5]}),x="A")如果我使用常規直方圖,我可以指定text直接指向包含要顯示的值的列的參數。px.bar(pd.DataFrame({"val":[1,2,3,4,5], "height": [3,2,3,3,1]}), x="val", y="height", text="height")但對于直方圖,該值是計算出來的,它甚至不是fig.to_dict(). 有沒有辦法將文本標簽添加到直方圖中?使用下面的答案,我將這一發現總結為一篇文章 - https://towardsdatascience.com/histograms-with-plotly-express-complete-guide-d483656c5ad7
查看完整描述

3 回答

?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

text_auto設置為的參數將True執行您想要的操作。

以您的示例代碼為例,這就是我得到的:

fig?=?px.histogram(pd.DataFrame({"A":[1,1,1,2,2,3,3,3,4,4,4,5]}),x="A",?
text_auto=True)
fig.show()


查看完整回答
反對 回復 2023-08-15
?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

據我所知,繪圖直方圖沒有文本屬性。事實證明,如果可能的話,檢索應用的 x 和 y 值并將它們放入適當的注釋中也是很復雜的。您最好的選擇似乎是使用numpy.histogram處理分箱并使用go.Bar.?下面的代碼片段將產生以下圖:


https://img2.sycdn.imooc.com/64db595500017e1f05800366.jpg

完整代碼:

import numpy as np

import plotly.express as px

import plotly.graph_objects as go


# sample data

df = px.data.tips()


# create bins

bins = [0, 10, 20, 30, 40, 50]

counts, bins = np.histogram(df.total_bill, bins=bins)

#bins2 = 0.5 * (bins1[:-1] + bins2[1:])


fig = go.Figure(go.Bar(x=bins, y=counts))

fig.data[0].text = counts

fig.update_traces(textposition='inside', textfont_size=8)

fig.update_layout(bargap=0)



fig.update_traces(marker_color='blue', marker_line_color='blue',

? ? ? ? ? ? ? ? ? marker_line_width=1, opacity=0.4)


fig.show()


查看完整回答
反對 回復 2023-08-15
?
MM們

TA貢獻1886條經驗 獲得超2個贊

今天早上我在嘗試繪制 TDD 百分比直方圖時遇到了同樣的問題。我想使用plotly 進行標準化(histnorm:“百分比”),這樣我就可以看到每月 TDD 值的百分比而不是計數。我通過簡單地執行print(tdd_hist)找到了這個解決方案


首先,我將直方圖打印到控制臺并看到這個輸出......


Figure({

'data': [{'alignmentgroup': 'True',

          'bingroup': 'x',

          'histnorm': 'percent',

          'hovertemplate': 'Total Demand Distortion TDD %=%{x}<br>count=%{y}<extra></extra>',

          'legendgroup': '',

          'marker': {'color': '#636efa'},

          'name': '',

          'offsetgroup': '',

          'orientation': 'v',

          'showlegend': False,

          'type': 'histogram',

          'x': array([0.67, 0.68, 0.68, ..., 2.41, 2.48, 2.01]),

          'xaxis': 'x',

          'yaxis': 'y'}],

'layout': {'barmode': 'relative',

           'legend': {'tracegroupgap': 0},

           'template': '...',

           'title': {'text': 'Percent Histogram of TDD%'},

           'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'Total Demand Distortion TDD %'}},

           'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'count'}, 'type': 'log'}}

現在我可以清楚地看到,為了改變這一點,我做了一個


tdd_hist.layout.yaxis.title.text = 'Percent'


查看完整回答
反對 回復 2023-08-15
  • 3 回答
  • 0 關注
  • 296 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號