Skip to content

Commit ec44961

Browse files
committed
histogram examples for px
1 parent 997aad3 commit ec44961

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

doc/python/2D-Histogram.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fig = px.density_heatmap(df, x="total_bill", y="tip", facet_row="sex", facet_col
8282
fig.show()
8383
```
8484

85-
You can add `z` as text to 2D Histogram points using `fig.update_traces(texttemplate="%{z}")`
85+
You can add the `z` values as text to 2D Histogram points using `fig.update_traces(texttemplate="%{z}")`
8686

8787
```python
8888
import plotly.express as px

doc/python/histograms.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,25 @@ fig = px.histogram(df, x="total_bill", color="sex", marginal="rug", # can be `bo
208208
fig.show()
209209
```
210210

211+
### Adding bar text
212+
213+
214+
You can add text to histogram bars using `fig.update_traces(texttemplate="%{variable}")`, where `variable` is the text to add. In this example, we add the y-axis values to the bars. We get these values following the **Accessing the counts (y-axis) values** example above.
215+
216+
```python
217+
import plotly.express as px
218+
import numpy as np
219+
220+
df = px.data.tips()
221+
222+
counts, bins = np.histogram(df.total_bill, bins=range(0, 60, 5))
223+
bins = 0.5 * (bins[:-1] + bins[1:])
224+
225+
fig = px.bar(x=bins, y=counts, labels={'x':'total_bill', 'y':'count'})
226+
fig.update_traces(texttemplate="%{y}")
227+
fig.show()
228+
```
229+
211230
## Histograms with go.Histogram
212231

213232
If Plotly Express does not provide a good starting point, it is also possible to use [the more generic `go.Histogram` class from `plotly.graph_objects`](/python/graph-objects/). All of the available histogram options are described in the histogram section of the reference page: https://plotly.com/python/reference#histogram.

0 commit comments

Comments
 (0)