The Pandas DataFrame/Series has several methods related to plotting.
Preparation
Before any data manipulation can occur, four (4) new libraries will require installation.
- The Pandas library enables access to/from a DataFrame.
- The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.
- The Matplotlib library displays a visual graph of a plotted dataset.
- The Scipy library allows users to manipulate and visualize the data.
To install these libraries, navigate to an IDE terminal. At the command prompt ($
), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($
). Your terminal prompt may be different.
$ pip install pandas
Hit the <Enter> key on the keyboard to start the installation process.
$ pip install numpy
Hit the <Enter> key on the keyboard to start the installation process.
ip install matplotlib
Hit the <Enter> key on the keyboard to start the installation process.
$ pip install scipy
Hit the <Enter> key on the keyboard to start the installation process.
If the installations were successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required libraries.
- How to install Pandas on PyCharm
- How to install NumPy on PyCharm
- How to install Matplotlib on PyCharm
- How to install Scipy on PyCharm
Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.
import pandas as pd import numpy as np import matplotlib.pyplot as plt import scipy
DataFrame Plot Hexbin
The dataframe.plot.hexbin()
method establishes a relationship between two (2) numeric values. This occurs when there is a large number of data points. With no overlaps, the chart splits into different hexbins
.
π‘ Note: The darker the color hue, the more concentrated the points.
The syntax for this method is as follows:
DataFrame.plot.hexbin(x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs)
Parameter | Description |
---|---|
x | This parameter is a column label/position for x-points. |
y | This parameter is a column label/position for y-points. |
c | A column integer/string representing the value of an (x, y) point. |
reduce_c_function | This function reduces multiple values in a bin to a single value. |
gridsize | The number of hexagons in the x-direction. Grid size can also be a tuple with two (2) elements indicating x-y numbers. |
**kwargs | Keywords documented in DataFrame.plot() . |
For this example, we have a CSV file containing the Sacramento, California, real-estate sales transactions over a five (5) day span. In addition, a Hexbin chart displays the square footage and house prices.
df = pd.read_csv('real-estate.csv', usecols=['sq__ft', 'price']) ax = plot.gca() ax = df.plot.hexbin(x='sq__ft', y='price', gridsize=20, ax=ax) plot.show()
- Line [1] reads in two (2) columns from a comma-delimited CSV file and saves it to
df
. - Line [2] gets the current axes (
gca()
) and saves it toax
. - Line [3] does the following:
- plots the Hexbin chart based on square footage and house prices
- sets the grid size to 20
- sets the ax variable created above
- Line [4] displays the Hexbin chart on-screen.
Output
The buttons on the bottom left can be used to further manipulate the chart.
π‘ Note: Another way to create this chart is with the plot()
method and the kind parameter set to the 'hexbin'
option.
This example uses the NumPy library to plot random numbers using Hexbin.
n = 900 x = np.random.uniform(-3, 3, size=n) y = np.random.uniform(20, 80, size=n) ob = np.random.randint(1, 5, size=n) df = pd.DataFrame({'x': x, 'y': y, 'ob': ob)}) ax = df.plot.hexbin(x='x', y='y', reduce_C_function=np.sum, gridsize=10, cmap="plasma") plot.show()
- Line [1] sets the size (range) to 900 and saves to
n
. - Line [2-3] uses
np.random.uniform
to evenly distribute numbers between a specified range. - Line [4] uses
np.random.randint
returns random integers between the specified range. - Line [5] creates a DataFrame based on the variables created above and saves it to df.
- Line [6] does the following:
- plots the Hexbin chart based on the variables x, and y
- reduces the plot size by adding up the numbers
- sets the grid size to 10
- sets the colormap (cmap) to plasma
- Line [7] displays the Hexbin chart on-screen.
Output
The buttons on the bottom left can be used to further manipulate the chart.
π‘ Note: Another way to create this chart is with the plot()
method and the kind parameter set to the 'hexbin'
option.
DataFrame Plot Hist
The dataframe.plot.hist()
(histogram) method plots the number of times different values appear in a dataset.
The syntax for this method is as follows:
DataFrame.plot.hist(by=None, bins=10, **kwargs)
Parameter | Description |
---|---|
by | This parameter is the column in the DataFrame to group by. |
none | This parameter denotes the number of histogram bins to use. |
**kwargs | Keywords document in DataFrame.plot() . |
For this example, this code selects a random number between 0 and 36. This number is the total number of slots on a Roulette wheel (0-36 outside the US). A histogram indicates that some numbers appear more than others.
slots = np.random.randint(0, 36, 250) df = pd.DataFrame(slots, columns=['slots']) df['random'] = df['slots'] + slots ax = df.plot.hist(bins=12, alpha=0.5) plt.show()
- Line [1] creates a variable containing 250 random integers between the specified range.
- Line [2] creates a DataFrame from the slots variable, sets the columns to the same, and saves it to
df
. - Line [3] creates a new DataFrame column based on the existing slots column plus the
slots
variable. - Line [4] does the following:
- sets the plot type to Hist
- the bin size to 12 (bars)
- the alpha (transparency) to 0.5.
- Line [5] displays the Hist chart on-screen.
Output
The buttons on the bottom left can be used to further manipulate the chart.
π‘ Note: Another way to create this chart is with the plot()
method and the kind
parameter set to the 'hist'
option.
DataFrame Plot Pie
The dataframe.plot.pie()
method generates a Pie Chart based on a proportional representation of the numeric values in a column.
The syntax for this method is as follows:
DataFrame.plot.pie(**kwargs)
Parameter | Description |
---|---|
y | This parameter is the label/position of the column to plot. |
**kwargs | Keywords documented in DataFrame.plot() . |
For this example, Rivers Clothing plots its Quarterly Sales on a Pie Chart.
rivers_dict = {'Months': ['Jan','Aor','Jul','Oct'], 'Sales': [28744, 32600, 45700, 55900]} df = pd.DataFrame(rivers_dict) qtitle = 'Rivers Clothing Quarterly Sales' qlabels = ['Q1','Q2','Q3','Q4'] qcolors = ['#9932CC', '#8B008B', '#E6E6FA', '#9370DB'] qexplode = (0,0,0,0.2) df.plot.pie(title=qtitle, y='Sales', figsize=(6,5), fontsize=9, labels=qlabels, colors=qcolors, explode=qexplode, legend=False) plt.show()
- Line [1] creates a dictionary of lists with quarterly sale details. This output saves to
rivers_dict
. - Line [2] creates a DataFrame from the dictionary created above.
- Line [3] saves the title for the Pie chart to
qtitle
. - Line [4] saves the labels for the Pie chart to
qlabels
. - Line [5] saves the slices of the Pie chart to
qcolors
. - Line [6] saves the explode value (away from the main chart) to
qexplode
. - Line [7] creates a Pie chart using the parameters saved above.
- Line [8] displays the Pie chart on-screen.
Output
The buttons on the bottom left can be used to further manipulate the chart.
π‘ Note: Another way to create this chart is with the plot()
method and the kind parameter set to the 'pie'
option.
Further Learning Resources
This is Part 20 of the DataFrame method series.
Also, have a look at the Pandas DataFrame methods cheat sheet!