3 回答

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()

TA貢獻1883條經驗 獲得超3個贊
據我所知,繪圖直方圖沒有文本屬性。事實證明,如果可能的話,檢索應用的 x 和 y 值并將它們放入適當的注釋中也是很復雜的。您最好的選擇似乎是使用numpy.histogram處理分箱并使用go.Bar
.?下面的代碼片段將產生以下圖:
完整代碼:
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()

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'
添加回答
舉報