Matplotlib provides two interfaces for plotting
The stateful interface makes its calls with plot() and other top-level pyplot functions.
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from IPython.display import Image
%matplotlib inline
plt.figure()
plt.plot([1, 2, 3, 4])
plt.show()
plt.plot([0, 1, 2, 3], [2, 4, 6, 8], 'o')
plt.show()
plt.plot([2, 4, 6, 8], 'o', [1, 5, 9, 13], '-s')
plt.show()
plt.plot([2, 4, 6, 8], color='blue', marker='o', linestyle='dashed', linewidth=2, markersize=12)
plt.show()
plt.figure()
plt.plot([1, 2, 4, 5], [1, 3, 4, 3], '-o')
plt.savefig('lineplot.png', dpi=300, bbox_inches='tight')
#bbox_inches='tight' removes the outer white margins
Image("lineplot.png")
def myplot():
X = np.linspace(-2*np.pi, 2*np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X)
plt.figure(figsize=(10,6), dpi=80)
plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-",label="cosine")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-",label="sine")
# Set x limits
plt.xlim(-3*np.pi/2,3*np.pi/2)
# Set y limits
plt.ylim(-2.0,2.0)
# Set x and y ticks with ticklabels
plt.xticks([-3*np.pi/2, -np.pi, -np.pi/2, 0, np.pi/2, np.pi, 3*np.pi/2],
[r'$-3\pi/2$', r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$-\pi$', r'$3\pi/2$'])
plt.yticks([-2, -1, 0, +1, +2],
[r'$-2$', r'$-1$', r'$0$', r'$+1$', r'$+2$'])
plt.xlabel('x')
plt.ylabel('y')
plt.title(r'Plot of $sin(x)$ and $\cos(x)$')
myplot()
plt.text(0,1.5,"Some text at (0,1.5)")
plt.grid(True)
plt.show()
def myplotannotate():
myplot()
t = 2*np.pi/3
plt.plot([t,t],[0,np.sin(t)], color ='red', linewidth=1.5, linestyle="--")
plt.plot([t,t],[0,np.cos(t)], color ='blue', linewidth=1.5, linestyle="--")
plt.annotate(r'$\sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',
xy=(t, np.sin(t)), xycoords='data',
xytext=(+10, +30), textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.annotate(r'$\cos(\frac{2\pi}{3})=-\frac{1}{2}$',
xy=(t, np.cos(t)), xycoords='data',
xytext=(-90, -50), textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
myplotannotate()
plt.grid(True)
plt.show()
myplotannotate()
plt.grid(True)
plt.legend(loc='upper left')
plt.show()
myplotannotate()
plt.grid(True)
plt.legend(loc='upper left')
ax = plt.gca()
ax.spines['left'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('center')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.grid(False)
plt.show()
plt.bar(['A', 'B', 'C', 'D'], [20, 25, 40, 10])
plt.show()
import numpy as np
labels = ['A', 'B', 'C', 'D']
x = np.arange(len(labels))
width = 0.4
plt.bar(x - width / 2, [20, 25, 40, 10], width=width)
plt.bar(x + width / 2, [30, 15, 30, 20], width=width)
# Ticks and tick labels must be set manually
plt.xticks(x)
ax = plt.gca()
ax.set_xticklabels(labels)
plt.show()
import numpy as np
labels = ['A', 'B', 'C', 'D']
x = np.arange(len(labels))
bar1 = np.linspace(10,20,4)
bar2 = np.linspace(5,20,4)
bar3 = np.linspace(2,10,4)
plt.bar(x, bar1)
plt.bar(x, bar2, bottom=bar1)
plt.bar(x, bar3, bottom=np.add(bar1, bar2))
# Ticks and tick labels must be set manually
plt.xticks([0,1,2,3])
ax = plt.gca()
ax.set_xticklabels(labels)
plt.show()
plt.pie([0.4, 0.3, 0.2, 0.1], explode=(0.1, 0, 0, 0), labels=['A', 'B', 'C', 'D'], autopct='%.2f')
plt.show()
n = 20
Z = np.ones(n)
Z[-1] *= 2
plt.axes([0.025,0.025,0.95,0.95])
plt.pie(Z, explode=Z*.05, colors = ['%f' % (i/float(n)) for i in range(n)])
plt.gca().set_aspect('equal')
plt.xticks([]), plt.yticks([])
plt.show()
plt.stackplot([1, 2, 3, 4], [2, 4, 5, 8], [1, 5, 4, 2])
plt.show()
# load datasets
sales = pd.read_csv('./data/smartphone_sales.csv')
# Create figure
plt.figure(figsize=(6, 4), dpi=100)
# Create stacked area chart
labels = sales.columns[1:]
plt.stackplot('Quarter', 'Apple', 'Samsung', 'Huawei', 'Xiaomi', 'OPPO', data=sales, labels=labels)
# Add legend
plt.legend()
# Add labels and title
plt.xlabel('Quarters')
plt.ylabel('Sales units in thousands')
plt.title('Smartphone sales units')
# Show plot
plt.show()
np.random.seed(19680801)
mu = 100 # mean of distribution
sigma = 15 # standard deviation of distribution
x = mu + sigma * np.random.randn(437)
bins = 50
plt.hist(x, bins=30, density=True)
# add a 'best fit' line
y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
np.exp(-0.5 * (1 / sigma * (bins - mu))**2))
plt.plot(bins, y, '--')
plt.xlabel('Smarts')
plt.ylabel('Probability density')
plt.title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')
plt.show()
# normal distribution center at x=0 and y=5
x = np.random.randn(10000)
y = np.random.randn(10000) + 5
plt.hist2d(x, y, bins=40)
plt.colorbar()
plt.show()
# IQ samples
iq_scores = [126, 89, 90, 101, 102, 74, 93, 101, 66, 120, 108, 97, 98,
105, 119, 92, 113, 81, 104, 108, 83, 102, 105, 111, 102, 107,
103, 89, 89, 110, 71, 110, 120, 85, 111, 83, 122, 120, 102,
84, 118, 100, 100, 114, 81, 109, 69, 97, 95, 106, 116, 109,
114, 98, 90, 92, 98, 91, 81, 85, 86, 102, 93, 112, 76,
89, 110, 75, 100, 90, 96, 94, 107, 108, 95, 96, 96, 114,
93, 95, 117, 141, 115, 95, 86, 100, 121, 103, 66, 99, 96,
111, 110, 105, 110, 91, 112, 102, 112, 75]
# Create figure
plt.figure(figsize=(6, 4), dpi=100)
# Create histogram
plt.boxplot(iq_scores)
# Add labels and title
ax = plt.gca()
ax.set_xticklabels(['Test group'])
plt.ylabel('IQ score')
plt.title('IQ scores for a test group of a hundred adults')
# Show plot
plt.show()
group_a = [118, 103, 125, 107, 111, 96, 104, 97, 96, 114, 96, 75, 114,
107, 87, 117, 117, 114, 117, 112, 107, 133, 94, 91, 118, 110,
117, 86, 143, 83, 106, 86, 98, 126, 109, 91, 112, 120, 108,
111, 107, 98, 89, 113, 117, 81, 113, 112, 84, 115, 96, 93,
128, 115, 138, 121, 87, 112, 110, 79, 100, 84, 115, 93, 108,
130, 107, 106, 106, 101, 117, 93, 94, 103, 112, 98, 103, 70,
139, 94, 110, 105, 122, 94, 94, 105, 129, 110, 112, 97, 109,
121, 106, 118, 131, 88, 122, 125, 93, 78]
group_b = [126, 89, 90, 101, 102, 74, 93, 101, 66, 120, 108, 97, 98,
105, 119, 92, 113, 81, 104, 108, 83, 102, 105, 111, 102, 107,
103, 89, 89, 110, 71, 110, 120, 85, 111, 83, 122, 120, 102,
84, 118, 100, 100, 114, 81, 109, 69, 97, 95, 106, 116, 109,
114, 98, 90, 92, 98, 91, 81, 85, 86, 102, 93, 112, 76,
89, 110, 75, 100, 90, 96, 94, 107, 108, 95, 96, 96, 114,
93, 95, 117, 141, 115, 95, 86, 100, 121, 103, 66, 99, 96,
111, 110, 105, 110, 91, 112, 102, 112, 75]
group_c = [108, 89, 114, 116, 126, 104, 113, 96, 69, 121, 109, 102, 107,
122, 104, 107, 108, 137, 107, 116, 98, 132, 108, 114, 82, 93,
89, 90, 86, 91, 99, 98, 83, 93, 114, 96, 95, 113, 103,
81, 107, 85, 116, 85, 107, 125, 126, 123, 122, 124, 115, 114,
93, 93, 114, 107, 107, 84, 131, 91, 108, 127, 112, 106, 115,
82, 90, 117, 108, 115, 113, 108, 104, 103, 90, 110, 114, 92,
101, 72, 109, 94, 122, 90, 102, 86, 119, 103, 110, 96, 90,
110, 96, 69, 85, 102, 69, 96, 101, 90]
group_d = [ 93, 99, 91, 110, 80, 113, 111, 115, 98, 74, 96, 80, 83,
102, 60, 91, 82, 90, 97, 101, 89, 89, 117, 91, 104, 104,
102, 128, 106, 111, 79, 92, 97, 101, 106, 110, 93, 93, 106,
108, 85, 83, 108, 94, 79, 87, 113, 112, 111, 111, 79, 116,
104, 84, 116, 111, 103, 103, 112, 68, 54, 80, 86, 119, 81,
84, 91, 96, 116, 125, 99, 58, 102, 77, 98, 100, 90, 106,
109, 114, 102, 102, 112, 103, 98, 96, 85, 97, 110, 131, 92,
79, 115, 122, 95, 105, 74, 85, 85, 95]
# Create figure
plt.figure(figsize=(6, 4), dpi=100)
# Create histogram
plt.boxplot([group_a, group_b, group_c, group_d])
# Add labels and title
ax = plt.gca()
ax.set_xticklabels(['Group A', 'Group B', 'Group C', 'Group D'])
plt.ylabel('IQ score')
plt.title('IQ scores for different test groups')
# Show plot
plt.show()
# Create figure
plt.figure(figsize=(4, 3))
# Create histogram
plt.violinplot([group_a, group_b, group_c, group_d])
# Add labels and title
ax = plt.gca()
ax.set_xticks([1,2,3,4])
ax.set_xticklabels(['Group A', 'Group B', 'Group C', 'Group D'])
plt.ylabel('IQ score')
plt.title('IQ scores for different test groups')
# Show plot
plt.show()
# Load dataset
data = pd.read_csv('./data/anage_data.csv')
# Preprocessing
longevity = 'Maximum longevity (yrs)'
mass = 'Body mass (g)'
data = data[np.isfinite(data[longevity]) & np.isfinite(data[mass])]
# Sort according to class
amphibia = data[data['Class'] == 'Amphibia']
aves = data[data['Class'] == 'Aves']
mammalia = data[data['Class'] == 'Mammalia']
reptilia = data[data['Class'] == 'Reptilia']
# Create figure
plt.figure(figsize=(6,4))
# Create scatter plot
plt.scatter(amphibia[mass], amphibia[longevity], label='Amphibia')
plt.scatter(aves[mass], aves[longevity], label='Aves')
plt.scatter(mammalia[mass], mammalia[longevity], label='Mammalia')
plt.scatter(reptilia[mass], reptilia[longevity], label='Reptilia')
# Add legend
plt.legend()
# Log scale
ax = plt.gca()
ax.set_xscale('log')
ax.set_yscale('log')
# Add labels
plt.xlabel('Body mass in grams')
plt.ylabel('Maximum longevity in years')
# Show plot
plt.show()
# Fixing random state for reproducibility
np.random.seed(19680801)
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
area = (30 * np.random.rand(N))**2 # 0 to 15 point radii
colors = np.random.rand(N)
area = (30 * np.random.rand(N))**2 # 0 to 15 point radii
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.colorbar()
plt.show()
plt.subplot(2,1,1)
plt.xticks([]), plt.yticks([])
plt.text(0.5,0.5, 'subplot(2,1,1)',ha='center',va='center',size=24,alpha=.5)
plt.subplot(2,1,2)
plt.xticks([]), plt.yticks([])
plt.text(0.5,0.5, 'subplot(2,1,2)',ha='center',va='center',size=24,alpha=.5)
plt.show()
plt.subplot(2,2,1)
plt.xticks([]), plt.yticks([])
plt.text(0.5,0.5, 'subplot(2,2,1)',ha='center',va='center',size=20,alpha=.5)
plt.subplot(2,2,2)
plt.xticks([]), plt.yticks([])
plt.text(0.5,0.5, 'subplot(2,2,2)',ha='center',va='center',size=20,alpha=.5)
plt.subplot(2,2,3)
plt.xticks([]), plt.yticks([])
plt.text(0.5,0.5, 'subplot(2,2,3)',ha='center',va='center',size=20,alpha=.5)
plt.subplot(2,2,4)
plt.xticks([]), plt.yticks([])
plt.text(0.5,0.5, 'subplot(2,2,4)',ha='center',va='center',size=20,alpha=.5)
plt.show()
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'o-')
plt.title('A tale of 2 subplots')
plt.ylabel('Damped oscillation')
plt.subplot(2, 1, 2)
plt.plot(x2, y2, '.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')
plt.show()
series = np.random.rand(100,4)
def mysubplot():
fig, axes = plt.subplots(2, 2)
axes = axes.ravel()
for i, ax in enumerate(axes):
ax.plot(series[:,i])
ax.set_title('Subplot ' + str(i))
mysubplot()
plt.show()
mysubplot()
plt.tight_layout()
plt.show()
plt.axes([0.1,0.1,.8,.8])
plt.xticks([]), plt.yticks([])
plt.text(0.6,0.6, 'axes([0.1,0.1,.8,.8])',ha='center',va='center',size=20,alpha=.5)
plt.axes([0.2,0.2,.3,.3])
plt.xticks([]), plt.yticks([])
plt.text(0.5,0.5, 'axes([0.2,0.2,.3,.3])',ha='center',va='center',size=16,alpha=.5)
plt.show()
# create some data to use for the plot
dt = 0.001
t = np.arange(0.0, 10.0, dt)
r = np.exp(-t[:1000] / 0.05) # impulse response
x = np.random.randn(len(t))
s = np.convolve(x, r)[:len(x)] * dt # colored noise
# the main axes is subplot(111) by default
plt.plot(t, s)
plt.axis([0, 1, 1.1 * np.min(s), 2 * np.max(s)])
plt.xlabel('time (s)')
plt.ylabel('current (nA)')
plt.title('Gaussian colored noise')
# this is an inset axes over the main axes
a = plt.axes([.65, .6, .2, .2], facecolor='k')
n, bins, patches = plt.hist(s, 400, density=True)
plt.title('Probability')
plt.xticks([])
plt.yticks([])
# this is another inset axes over the main axes
a = plt.axes([0.2, 0.6, .2, .2], facecolor='k')
plt.plot(t[:len(r)], r)
plt.title('Impulse response')
plt.xlim(0, 0.2)
plt.xticks([])
plt.yticks([])
plt.show()
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(3, 4)
ax1 = plt.subplot(gs[:3, :3])
ax2 = plt.subplot(gs[0, 3])
ax3 = plt.subplot(gs[1, 3])
ax4 = plt.subplot(gs[2, 3])
ax1.plot(series[:,0])
ax2.plot(series[:,1])
ax3.plot(series[:,2])
ax4.plot(series[:,3])
plt.tight_layout()
import pandas as pd
# Load dataset
data = pd.read_csv('./data/anage_data.csv')
# Preprocessing
longevity = 'Maximum longevity (yrs)'
mass = 'Body mass (g)'
data = data[np.isfinite(data[longevity]) & np.isfinite(data[mass])]
# Sort according to class
aves = data[data['Class'] == 'Aves']
aves = data[data[mass] < 20000]
# Create figure
fig = plt.figure(constrained_layout=True)
# Create gridspec
gs = fig.add_gridspec(4, 4)
# Specify subplots
histx_ax = fig.add_subplot(gs[0, :-1])
histy_ax = fig.add_subplot(gs[1:, -1])
scatter_ax = fig.add_subplot(gs[1:, :-1])
# Create plots
scatter_ax.scatter(aves[mass], aves[longevity])
histx_ax.hist(aves[mass], bins=20, density=True)
histx_ax.set_xticks([])
histy_ax.hist(aves[longevity], bins=20, density=True, orientation='horizontal')
histy_ax.set_yticks([])
# Add labels and title
plt.xlabel('Body mass in grams')
plt.ylabel('Maximum longevity in years')
fig.suptitle('Scatter plot with marginal histograms')
# Show plot
plt.show()
Changing the scale of an axis is easy:
plt.xscale('log')
from matplotlib.ticker import NullFormatter # useful for `logit` scale
# Fixing random state for reproducibility
np.random.seed(19680801)
# make up some data in the interval ]0, 1[
y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))
# plot with various axes scales
plt.figure(1)
# linear
plt.subplot(221)
plt.plot(x, y)
plt.yscale('linear')
plt.title('linear')
plt.grid(True)
# log
plt.subplot(222)
plt.plot(x, y)
plt.yscale('log')
plt.title('log')
plt.grid(True)
# symmetric log
plt.subplot(223)
plt.plot(x, y - y.mean())
plt.yscale('symlog', linthreshy=0.01)
plt.title('symlog')
plt.grid(True)
# logit
plt.subplot(224)
plt.plot(x, y)
plt.yscale('logit')
plt.title('logit')
plt.grid(True)
# Format the minor tick labels of the y-axis into empty strings with
# `NullFormatter`, to avoid cumbering the axis with too many labels.
plt.gca().yaxis.set_minor_formatter(NullFormatter())
# Adjust the subplot layout, because the logit one may take more space
# than usual, due to y-tick labels like "1 - 10^{-3}"
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25,
wspace=0.35)
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
data = [[ 66386, 174296, 75131, 577908, 32015],
[ 58230, 381139, 78045, 99308, 160454],
[ 89135, 80552, 152558, 497981, 603535],
[ 78415, 81858, 150656, 193263, 69638],
[139361, 331509, 343164, 781380, 52269]]
columns = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail')
rows = ['%d year' % x for x in (100, 50, 20, 10, 5)]
values = np.arange(0, 2500, 500)
value_increment = 1000
# Get some pastel shades for the colors
colors = plt.cm.BuPu(np.linspace(0, 0.5, len(rows)))
n_rows = len(data)
index = np.arange(len(columns)) + 0.3
bar_width = 0.4
# Initialize the vertical-offset for the stacked bar chart.
y_offset = np.zeros(len(columns))
# Plot bars and create text labels for the table
cell_text = []
for row in range(n_rows):
plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row])
y_offset = y_offset + data[row]
cell_text.append(['%1.1f' % (x / 1000.0) for x in y_offset])
# Reverse colors and text labels to display the last value at the top.
colors = colors[::-1]
cell_text.reverse()
# Add a table at the bottom of the axes
the_table = plt.table(cellText=cell_text,
rowLabels=rows,
rowColours=colors,
colLabels=columns,
loc='bottom')
# Adjust layout to make room for the table:
plt.subplots_adjust(left=0.2, bottom=0.2)
plt.ylabel("Loss in ${0}'s".format(value_increment))
plt.yticks(values * value_increment, ['%d' % val for val in values])
plt.xticks([])
plt.title('Loss by Disaster')
plt.show()
fig, ax = plt.subplots()
# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks')
ax.grid()
fig.savefig("test.png")
plt.show()
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
fig, (ax1,ax2) = plt.subplots(nrows=2,ncols=1)
ax1.plot(x1, y1, 'o-')
ax1.set_title('A tale of 2 subplots')
ax1.set_ylabel('Damped oscillation')
ax2.plot(x2, y2, '.-')
ax2.set_xlabel('time (s)')
ax2.set_ylabel('Undamped')
plt.show()
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.colors import BoundaryNorm
from matplotlib.ticker import MaxNLocator
import numpy as np
# make these smaller to increase the resolution
dx, dy = 0.05, 0.05
# generate 2 2d grids for the x & y bounds
y, x = np.mgrid[slice(1, 5 + dy, dy),
slice(1, 5 + dx, dx)]
z = np.sin(x)**10 + np.cos(10 + y*x) * np.cos(x)
# x and y are bounds, so z should be the value *inside* those bounds.
# Therefore, remove the last value from the z array.
z = z[:-1, :-1]
levels = MaxNLocator(nbins=15).tick_values(z.min(), z.max())
# pick the desired colormap, sensible levels, and define a normalization
# instance which takes data values and translates those into levels.
cmap = plt.get_cmap('PiYG')
norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)
fig, (ax0, ax1) = plt.subplots(nrows=2)
im = ax0.pcolormesh(x, y, z, cmap=cmap, norm=norm)
fig.colorbar(im, ax=ax0)
ax0.set_title('pcolormesh with levels')
# contours are *point* based plots, so convert our bound into point
# centers
cf = ax1.contourf(x[:-1, :-1] + dx/2.,
y[:-1, :-1] + dy/2., z, levels=levels,
cmap=cmap)
fig.colorbar(cf, ax=ax1)
ax1.set_title('contourf with levels')
# adjust spacing between subplots so `ax1` title and `ax0` tick labels
# don't overlap
fig.tight_layout()
plt.show()
# This import registers the 3D projection, but is otherwise unused.
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.hot)
ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.cm.hot)
ax.set_zlim(-2,2)
plt.show()
import matplotlib.animation as animation
# New figure with white background
fig = plt.figure(figsize=(6,6), facecolor='white')
# New axis over the whole figure, no frame and a 1:1 aspect ratio
ax = fig.add_axes([0,0,1,1], frameon=False, aspect=1)
# Number of ring
n = 50
size_min = 50
size_max = 50*50
# Ring position
P = np.random.uniform(0,1,(n,2))
# Ring colors
C = np.ones((n,4)) * (0,0,0,1)
# Alpha color channel goes from 0 (transparent) to 1 (opaque)
C[:,3] = np.linspace(0,1,n)
# Ring sizes
S = np.linspace(size_min, size_max, n)
# Scatter plot
scat = ax.scatter(P[:,0], P[:,1], s=S, lw = 0.5,
edgecolors = C, facecolors='None')
# Ensure limits are [0,1] and remove ticks
ax.set_xlim(0,1), ax.set_xticks([])
ax.set_ylim(0,1), ax.set_yticks([])
def update(frame):
global P, C, S
# Every ring is made more transparent
C[:,3] = np.maximum(0, C[:,3] - 1.0/n)
# Each ring is made larger
S += (size_max - size_min) / n
# Reset ring specific ring (relative to frame number)
i = frame % 50
P[i] = np.random.uniform(0,1,2)
S[i] = size_min
C[i,3] = 1
# Update scatter object
scat.set_edgecolors(C)
scat.set_sizes(S)
scat.set_offsets(P)
# Return the modified object
return scat,
animation = animation.FuncAnimation(fig, update, interval=10, blit=True, frames=200)
animation.save('rain.gif', writer='imagemagick', fps=30, dpi=40)
#plt.show()
import pandas as pd # Pandas
import numpy as np # Numpy
import matplotlib.pyplot as plt # Matplotlibrary
import seaborn as sns # Seaborn Library
%matplotlib inline
# https://medium.com/@mukul.mschauhan/data-visualisation-using-seaborn-464b7c0e5122
# Load the Dataset in Python
tips = sns.load_dataset("tips")
tips.head()
total_bill | tip | sex | smoker | day | time | size | |
---|---|---|---|---|---|---|---|
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
sns.relplot(x="total_bill", y="tip",
hue="smoker", style="smoker", size="size",
data=tips);
sns.relplot(x="total_bill", y="tip", col="time",
hue="smoker", style="smoker", size="size",
data=tips);
sns.relplot(x="total_bill", y="tip", hue="day",
col="time", row="sex", data=tips);
dots = sns.load_dataset("dots")
sns.relplot(x="time", y="firing_rate", col="align",
hue="choice", size="coherence", style="choice",
facet_kws=dict(sharex=False),
kind="line", legend="full", data=dots);
fmri = sns.load_dataset("fmri")
sns.relplot(x="timepoint", y="signal", col="region", hue="event", style="event", kind="line", data=fmri);
/Users/apacheco/anaconda3/lib/python3.7/site-packages/scipy/stats/stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result. return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval
# Barplot
f, axes = plt.subplots(1, 3, figsize=(15,5))
sns.barplot(x ="sex" , y ="total_bill", data=tips, ax=axes[0]);
# Inference - Total Bill Amount for males is more than Females.
# Lets Plot Smoker Vs Total Bill :: The purpose is to find out if
# Smokers pay more bill than Non Smokers
sns.barplot(x = "smoker", y = "total_bill", data =tips, ax=axes[1]);
# Inference - More Bill for Smokers
# Lets Find If There is more Bill In Weekend or Weekdays
sns.barplot(x = "day", y = "total_bill", data=tips, ax=axes[2]);
# People tend to visit more on weekends
f, axes = plt.subplots(1, 3, figsize=(15,5))
# Boxplot
sns.boxplot(x = "day", y = "total_bill", data=tips, ax=axes[0]);
# Add hue to split the barplot. Making it more fancier
sns.boxplot(x = "day", y = "total_bill", data=tips, hue = "smoker", ax=axes[1]);
# On Friday people have more bill if they are a Non smoker vs smoker
# Violin Plots
sns.violinplot(x = "day", y = "total_bill", data = tips, ax=axes[2]);
distplot(): take a quick look at a univariate distribution.
jointplot(): visualize a bivariate distribution of two variables.
f, axes = plt.subplots(1, 3, figsize=(15,5))
sns.distplot(tips["total_bill"], bins=16, color="purple", ax=axes[0]);
sns.distplot(tips["total_bill"], bins=16, color="purple", kde=False, ax=axes[1]);
sns.distplot(tips["total_bill"], bins=16, color="purple", hist=False, ax=axes[2]);
# Jointplot - Scatterplot and Histogram
sns.jointplot(x = "total_bill", y = "tip", data = tips, color="purple")
<seaborn.axisgrid.JointGrid at 0x1244f06a0>
# Jointplot - Scatterplot and Histogram
sns.jointplot(x = tips["total_bill"], y = tips["tip"],kind = "kde",
color="purple") # contour plot
<seaborn.axisgrid.JointGrid at 0x1283aec88>
# Pairplot of Tips
sns.pairplot(tips, hue = "sex", palette="Set2")
# this will color the plot gender wise
<seaborn.axisgrid.PairGrid at 0x1213f4f98>
# LM PLot
sns.regplot(x = "total_bill", y = "tip", data = tips);
sns.lmplot(x="total_bill", y="tip", hue="smoker", col='time', data=tips);
# If using LUApps
#!pip install --user --upgrade pip
#!pip install --user --upgrade plotly-express nodejs
#https://www.plotly.express/
#https://medium.com/@plotlygraphs/introducing-plotly-express-808df010143d
#jupyter labextension install @jupyterlab/plotly-extension
import plotly_express as px
gapminder = px.data.gapminder()
gapminder2007 = gapminder.query("year==2007")
px.scatter(gapminder2007,x="gdpPercap", y="lifeExp")
px.scatter(gapminder2007,x="gdpPercap", y="lifeExp", color="continent")
px.scatter(gapminder2007,x="gdpPercap", y="lifeExp", color="continent", size="pop", size_max=60)
px.scatter(gapminder2007,x="gdpPercap", y="lifeExp", color="continent", size="pop", size_max=60, hover_name="country")
px.scatter(gapminder2007,x="gdpPercap", y="lifeExp", color="continent", size="pop", size_max=60,
hover_name="country", facet_col="continent", log_x = True, range_x=[200,100000])
px.scatter(gapminder,x="gdpPercap", y="lifeExp", color="continent", size="pop", size_max=60,
hover_name="country", animation_frame="year", animation_group="country",
range_x=[200,100000], range_y=[25,90], log_x = True)
px.scatter_geo(gapminder, locations="iso_alpha", color="continent", hover_name="country", size="pop",
animation_frame="year", projection="natural earth")
px.choropleth(gapminder, locations="iso_alpha", color="lifeExp", hover_name="country", animation_frame="year",
color_continuous_scale=px.colors.sequential.Plasma)