From 2a2ee8b287309284a84d47a3b4d385f9e99c3c65 Mon Sep 17 00:00:00 2001 From: Ong Chin Hwee Date: Fri, 2 Oct 2020 20:36:34 +0800 Subject: [PATCH] DOC: Add histogram category order example (#2779) --- doc/python/histograms.md | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/doc/python/histograms.md b/doc/python/histograms.md index 3d72758005b..8327bc91f7c 100644 --- a/doc/python/histograms.md +++ b/doc/python/histograms.md @@ -20,7 +20,7 @@ jupyter: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.7.3 + version: 3.8.5 plotly: description: How to make Histograms in Python with Plotly. display_as: statistical @@ -30,9 +30,9 @@ jupyter: order: 3 page_type: example_index permalink: python/histograms/ - redirect_from: - - /python/histogram-tutorial/ - - /python/histogram/ + redirect_from: + - /python/histogram-tutorial/ + - /python/histogram/ thumbnail: thumbnail/histogram.jpg --- @@ -392,6 +392,21 @@ fig.update_layout( fig.show() ``` +### Sort Histogram by Category Order + +Histogram bars can also be sorted based on the ordering logic of the categorical values using the [categoryorder](https://plotly.com/python/reference/layout/xaxis/#layout-xaxis-categoryorder) attribute of the x-axis. Sorting of histogram bars using `categoryorder` also works with multiple traces on the same x-axis. In the following examples, the histogram bars are sorted based on the total numerical values. + +```python +import plotly.express as px +import pandas as pd + +df = px.data.tips() +fig1 = px.histogram(df, x="day").update_xaxes(categoryorder='total ascending') +fig2 = px.histogram(df, x="day", color="smoker").update_xaxes(categoryorder='total descending') +fig1.show() +fig2.show() +``` + #### Reference See https://plotly.com/python/reference/histogram/ for more information and chart attribute options!