{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyM3N6YHhBILi8HPaV3jYDeV"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# Plotting Categorical Data\n","\n","For our purposes, categorical data is defined as qualitative, nominal, or ordinal data that is discrete, or non-continuous. Categorical data contrasts with numerical data that is continuous. The axis type determines how the data is plotted in the resulting figure.\n","\n","Axis types recognized in `plotly`:\n","- `linear`\n","- `log`\n","- `date`\n","- `category`\n","- `multicategory`\n","\n","The axis type is auto-detected by `plotly` based on the data linked to the specific axis.\n","- If `plotly` does not recognize the data as `multicategory`, `date`, or `category` (it checks sequentially in that order), it defaults to `linear`.\n","- When testing for `multicategory` data, `plotly` looks to see if there is a nested array.\n","- When testing for `date` or `category`, `plotly` requires more than twice as many distinct date or category strings as distinct numbers in order to choose one of these axis types.\n","\n","We can imagine scenarios in which we are working with categorical data that would not be accurately auto-detected by `plotly`. We can instruct `plotly` to recognize an axis as having categorical data through the `xaxis_type` and `yaxis_type` attributes."],"metadata":{"id":"6WsGiPhHgR-w"}},{"cell_type":"markdown","source":["## Example #1\n","\n","An example of categorical data represented as a bar chart."],"metadata":{"id":"9vA7jmE_gX1r"}},{"cell_type":"code","execution_count":2,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":542},"id":"4CvfdT6XgPLV","executionInfo":{"status":"ok","timestamp":1706150731899,"user_tz":300,"elapsed":211,"user":{"displayName":"Katherine Walden","userId":"17094108395123900917"}},"outputId":"07340568-c110-4ad9-de30-1a7aed2435d5"},"outputs":[{"output_type":"display_data","data":{"text/html":["\n","\n","\n","
\n","
\n","\n",""]},"metadata":{}}],"source":["import plotly.express as px # import statement\n","fig = px.bar(x=[\"a\", \"b\", \"c\", \"d\"], y = [1,2,3,4]) # create figure\n","fig.update_xaxes(type='category') # update axis type\n","fig # show output"]},{"cell_type":"markdown","source":["In this example, the auto-detected `X` axis type would be `linear`. By using `.update_xaxes(type='category')`, we force the `X` axis to be categorical."],"metadata":{"id":"zpaSF4p1gdRC"}},{"cell_type":"markdown","source":["## Example #2\n","\n","We can also control the category order by passing a dictionary to the `category_orders` parameter. An example with side-by-side bar charts of categorical data for the restaurant and tip data."],"metadata":{"id":"qq9iKADxgoaP"}},{"cell_type":"code","source":["df = px.data.tips() # load data\n","\n","# create figure\n","fig = px.bar(df, x=\"day\", y=\"total_bill\", color=\"smoker\", barmode=\"group\", facet_col=\"sex\",\n"," category_orders={\"day\": [\"Thur\", \"Fri\", \"Sat\", \"Sun\"],\n"," \"smoker\": [\"Yes\", \"No\"],\n"," \"sex\": [\"Male\", \"Female\"]})\n","fig.show() # show figure"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":542},"id":"X-cpiSM9gsLD","executionInfo":{"status":"ok","timestamp":1706150785250,"user_tz":300,"elapsed":247,"user":{"displayName":"Katherine Walden","userId":"17094108395123900917"}},"outputId":"c0d222de-22fc-435d-87e1-8b432785441a"},"execution_count":3,"outputs":[{"output_type":"display_data","data":{"text/html":["\n","\n","\n","
\n","
\n","\n",""]},"metadata":{}}]},{"cell_type":"markdown","source":["In addition to setting `color`, `barmode`, and `facet_col` parameters, we pass a dictionary to `category_orders` to determine the order for each category in the plot."],"metadata":{"id":"kXHnXo5QgwgQ"}},{"cell_type":"markdown","source":["## Ordering Categories\n","\n","We can also automatically sort categories by name or total value by using `.update_xaxes()` or `.update_yaxes()` in combination with the `categoryorder` parameter.\n","- Setting `categoryorder` to `category ascending` or `category descending` sorts categories alphabetically.\n","- Setting `categoryorder` to `total ascending` or `total descending` sorts categories numerically by total value."],"metadata":{"id":"_79LDFg0gyA0"}},{"cell_type":"markdown","source":["## Additional Resources\n","\n","- [`plotly`, Categorical Axes in Python](https://plotly.com/python/categorical-axes/)\n","- [`plotly`, Axes in Python](https://plotly.com/python/axes/)\n","- [`plotly`, Python Figure Reference: `layout.xaxis`](https://plotly.com/python/reference/layout/xaxis/)"],"metadata":{"id":"9sHEqdX5g0tV"}}]}