Step 1: Prepare your Example DataFrame
Here’s the code to define a simple DataFrame for our example
import pandas as pd
area = ['R', 'R', 'Python', 'Javascript', 'Python', 'R', 'Python', 'Javascript', 'Python', 'R']
hired = [17,12, 17, 12, 15, 16, 15, 12, 16,18]
applications = [110, 120, 123, 111, 112, 113, 99, 87, 110, 140]
campaign = dict(area = area, hired = hired, apps = applications)
hr = pd.DataFrame(data=campaign)
hr.head()
Here’s our DataFrame header:
area | hired | apps | |
---|---|---|---|
0 | R | 17 | 110 |
1 | R | 12 | 120 |
2 | Python | 17 | 123 |
3 | Javascript | 12 | 111 |
4 | Python | 15 | 112 |
Step 2: Render an Histogram for a single column
We can use both the plot or the .hist methods of the Pandas DataFrame to draw our chart.
In the first example we will plot the chart only for the hired column. We will also set our chart color to green, remove the grid and set a bigger figure size for the histogram using the figsize parameter. We then set a title for the plot using set_title() function.
hist_chart = hr['hired'].hist(color = 'green', grid=False, figsize = (10,6))
hist_chart.set_title('Hired Frequency');
Here’s the chart:
Step 3: Plot an histogram for multiple columns
Our pandas DataFrame contains several numeric columns, so we can as well render a single histogram plots for the multiple columns:
hist_chart = hr.plot (kind = 'hist', grid=False, title ='Hired vs Applications');
This will rend the following chart:
Step 4: Multiple histograms from one DataFrame
Conversely, using the subplots = True parameter allows us to create a grid of charts out of our data as shown below:
hist_chart = hr.plot (kind = 'hist',grid=False,subplots = True, title =['Hired', 'Applications'], cmap = 'viridis')
This will render an array of charts:
Note that in this example we have applied the viridis color map to our plot using the cmap parameter of the plot method. We have also used a list to pass a couple of title values to our charts.
Additional Learning
How to chart one or multiple horizontal line in your Python plot?