Home » Machine Learning » Plotting » Scatterplots, Pie Charts, Histograms

Scatterplots, Pie Charts, Histograms

It’s possible to create many different types of plot with Matplotlib and Python.

Here we’ll look at three common types of plot.

Scatterplots

Scatterplots are particularly useful when you’re trying to figure out how two sets of values are related. To create a scatterplot, supply an iterator of x- and corresponding y-values to the scatter method.

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(100)
y = np.random.randn(len(x))

fig, ax = plt.subplots()

ax.set_title("y")
ax.set_xlabel("x")
ax.set_ylabel("y")

"""
s sets the size of the points
00FF0055 = 0 red, maximum green, zero blue, transparency 0x80 (50%)
"""
ax.scatter(x, y, color='#00FF0080', linewidths=1, edgecolors='#0000FF44', s=100)

plt.show()

Pie Charts

The pie method accepts a single list of values which must be non-negative. The relative proportions of these values are then displayed in a circular chart. Using the autopct parameter you can automatically add percentages to the chart, assuming all values to represent 100%.

import matplotlib.pyplot as plt
import numpy as np

labels = ['frog', 'heron', 'dragonfly', 'cat']
x = np.random.rand(len(labels))

fig, ax = plt.subplots()

ax.set_title("Numbers of Animals in a Swamp")

# Autopct specifies how percentages should be formatted
ax.pie(x, labels=labels, autopct='%.2f %%')

plt.show()

Histograms

Histograms are bar charts where the height of each bar represents the number of data points between a range.

The edges of the ranges (bins) can be manually specified, or you can simply specify how many equally-spaced bins you want.

The hist method plots a histogram and also returns information about how many samples are in each bin, where the edges of the bins lie, and how each bar was drawn.

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(10000)

n, bins, patches = plt.hist(x, bins=5, edgecolor='white')
plt.show()

# Show some stats
print(n, bins, patches)

for patch in patches:
    print(patch)
[ 135. 2520. 5747. 1559.   39.] [-3.93685947 -2.29039969 -0.64393991  1.00251987  2.64897965  4.29543943] <BarContainer object of 5 artists>
Rectangle(xy=(-3.93686, 0), width=1.64646, height=135, angle=0)
Rectangle(xy=(-2.2904, 0), width=1.64646, height=2520, angle=0)
Rectangle(xy=(-0.64394, 0), width=1.64646, height=5747, angle=0)
Rectangle(xy=(1.00252, 0), width=1.64646, height=1559, angle=0)
Rectangle(xy=(2.64898, 0), width=1.64646, height=39, angle=0)

Leave a Reply

Blog at WordPress.com.

%d