Creating a histogram
We want to see how we can get the distribution of a sample of data and get an idea of where values are concentrated, as well as how much variability/spread it has. We will do this by creating a histogram.
As always, we'll start with the simplest possible example:
- We open the
poverty
DataFrame and create a subset of it, containing only countries and data from the year 2015:import pandas as pd poverty = pd.read_csv('data/poverty.csv') df = poverty[poverty['is_country'] & poverty['year'].eq(2015)]
- Import Plotly Express and run the
histogram
function withdf
as the argument to thedata_frame
parameter and the indicator of our choice for thex
parameter:import plotly.express as px gini = 'GINI index (World Bank estimate)' px.histogram(data_frame=df, x=gini)
As a result, we get the histogram that you can see in Figure 8.1: