Skip to content

Commit d83b1b0

Browse files
Update histograms.md
Added `Accessing the y-axis values` section
1 parent fcab197 commit d83b1b0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Diff for: doc/python/histograms.md

+26
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,32 @@ fig = px.histogram(df, x="total_bill", nbins=20)
6969
fig.show()
7070
```
7171

72+
#### Accessing the y-axis values
73+
74+
JavaScript calculates the y-axis (count) values on the fly in the browser, so it's not accessible in the `fig`. You can manually calculate it using `pandas.cut` or `np.digitize`.
75+
76+
```python
77+
import plotly.express as px
78+
import pandas as pd
79+
80+
df = px.data.tips()
81+
82+
# create the bins; use the `range` to get the same bin size as in the histogram in the previous section.
83+
df["total_bill_bins"] = pd.cut(df.total_bill, bins=range(0, 60, 5), right=False)
84+
85+
# calculate counts
86+
df_counts = df.pivot_table(index="total_bill_bins", values="size", aggfunc='count').reset_index()
87+
df_counts.rename(columns={"size": "count"}, inplace=True)
88+
89+
# sort, then convert to string
90+
df_counts = df_counts.sort_values(by="total_bill_bins")
91+
df_counts["total_bill_bins"] = df_counts["total_bill_bins"].astype(str)
92+
93+
# display calculated counts on the bar chart
94+
fig = px.bar(df_counts, x="total_bill_bins", y="count")
95+
fig.show()
96+
```
97+
7298
#### Type of normalization
7399

74100
The default mode is to represent the count of samples in each bin. With the `histnorm` argument, it is also possible to represent the percentage or fraction of samples in each bin (`histnorm='percent'` or `probability`), or a density histogram (the sum of bars is equal to 100, `density`), or a probability density histogram (sum equal to 1, `probability density`).

0 commit comments

Comments
 (0)