import os
import matplotlib.pyplot as plt
import numpy as np
# High DPI rendering for mac
%config InlineBackend.figure_format = 'retina'
%config InlineBackend.print_figure_kwargs={'facecolor' : "w"}
= {
plot_params 'font.size' : 22,
'axes.titlesize' : 24,
'axes.labelsize' : 20,
'axes.labelweight' : 'bold',
'xtick.labelsize' : 16,
'ytick.labelsize' : 16,
}
plt.rcParams.update(plot_params)
# Make dataset
= np.linspace(0,5,200)
X = 1.3*X + np.random.normal(0.01, size=X.shape) Y
Quick plotting
= plt.subplots(1,1, figsize=(8,8))
fig, ax
ax.scatter(X, Y)'X')
ax.set_xlabel('Y') ax.set_ylabel(
Text(0, 0.5, 'Y')
Make plots with equal aspect ratio and axes
= plt.subplots(1,1, figsize=(8,8))
fig, ax ='data')
ax.scatter(X, Y, label
# Find limits for each axes
= [np.min([ax.get_xlim(), ax.get_ylim()]), # min of both axes
lims max([ax.get_xlim(), ax.get_ylim()]), # max of both axes
np.
]
'k--', alpha=0.75, zorder=0, label='parity')
ax.plot(lims, lims, 'equal')
ax.set_aspect(
ax.set_xlim(lims)
ax.set_ylim(lims)
'X')
ax.set_xlabel('Y')
ax.set_ylabel(
= ax.get_legend_handles_labels()
handles, labels print(labels)
=handles, labels=labels, title="Legend") ax.legend(handles
['parity', 'data']
<matplotlib.legend.Legend at 0x11ad6a190>
Slightly fancier output with parity and linear fit plots
= plt.subplots(1,1, figsize=(8,8))
fig, ax =0.6, label='data')
ax.scatter(X, Y, alpha
= [np.min([ax.get_xlim(), ax.get_ylim()]), # min of both axes
lims max([ax.get_xlim(), ax.get_ylim()]), # max of both axes
np.
]
# Linear fit line
= np.polyfit(X, Y, deg=1)
reg 0] * np.array(lims) + reg[1], 'r--', linewidth=1.5, label='linear fit')
ax.plot(lims, reg[
# Parity plot
'k--', alpha=0.75, zorder=0, label='parity')
ax.plot(lims, lims, #ax.set_aspect('equal')
'X')
ax.set_xlabel('Y')
ax.set_ylabel(
= ax.get_legend_handles_labels()
handles, labels print(labels)
# Put a legend to the right of the current axis
=handles, labels=labels, title="Legend", loc='center left', bbox_to_anchor=(1, 0.5)) ax.legend(handles
['linear fit', 'parity', 'data']
<matplotlib.legend.Legend at 0x11af6afd0>