python matplotlib - ghdrako/doc_snipets GitHub Wiki

Use the object-oriented interface!!!

Global Functions

  • plt.bar – creates a bar chart
  • plt.scatter – makes a scatter plot
  • plt.boxplot – makes a box and whisker plot
  • plt.hist – makes a histogram
  • plt.plot – creates a line plot
x = np.arange(5) # assume there are 5 students
y = (20, 35, 30, 35, 27) # their test scores
plt.bar(x,y) # Bar plot
plt.scatter(x,y) 
df = pd.read_csv(‘Data/iris.csv’)   # Read sample data
df.hist()    # Histogram
df.plot()    # Line Graph
df.boxplot() # Box plot
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
plt.style.available # list of matplotlib  styles available for rendering plots
plt.style.use('ggplot') # choose style

Simple plot

top_10.plot(kind='barh', y="Sales", x="Name") #plot the data using the standard pandas plotting function

Object Oriented - If you need customize use the object-oriented interface!!!

The basic matplotlib terminology

  • Figure - final image that may contain 1 or more axes.Top-level container for everything on a canvas
  • Axes - represent an individual plot. Container class for a specific plot
  • A figure may contain many Axes and/or Subplots. Subplots are laid out in a grid within the Figure. Axes can be placed anywhere on the Figure. We can use the subplots factory to get the Figure and all the desired Axes at once.
fig, ax = plt.subplots()
fig,(ax1,ax2,ax3) = plt.subplots(nrows=3, ncols=1, sharex=True, 
figsize=(8,4))
# Iterating the Axes within a Figure
for ax in fig.get_axes():
 pass # do something
fig, ax = plt.subplots()
top_10.plot(kind='barh', y="Sales", x="Name", ax=ax)  # get access to the axes and figures in matplotlib to farther customization
#coustomization
ax.set_xlim([-10000, 140000]) #set limit
ax.set_xlabel('Total Revenue')  |
ax.set_ylabel('Customer');      | ax.set(title='2014 Revenue', xlabel='Total Revenue', ylabel='Customer') #set labels

fig, ax = plt.subplots(figsize=(5, 6)) # adjust the size of this image  in inches
ax.legend().set_visible(False)         # remove the legend

def currency(x, pos):
    'The two args are the value and tick position'
    if x >= 1000000:
        return '${:1.1f}M'.format(x*1e-6)
    return '${:1.0f}K'.format(x*1e-3)
formatter = FuncFormatter(currency)     # create formatter function
ax.xaxis.set_major_formatter(formatter)	# apply it to the x axis

# Add a line for the average
avg = top_10['Sales'].mean()
ax.axvline(x=avg, color='b', label='Average', linestyle='--', linewidth=1)

# Annotate the new customers
for cust in [3, 5, 8]:
    ax.text(115000, cust, "New Customer")

Multiplot on the same figure

fig, (ax0, ax1) = plt.subplots(nrows=1, ncols=2, sharey=True, figsize=(7, 4)) # two plots ax and ax1, sharey=True so that the yaxis will share the same labels


# Get the figure and the axes
fig, (ax0, ax1) = plt.subplots(nrows=1,ncols=2, sharey=True, figsize=(7, 4))
top_10.plot(kind='barh', y="Sales", x="Name", ax=ax0)
ax0.set_xlim([-10000, 140000])
ax0.set(title='Revenue', xlabel='Total Revenue', ylabel='Customers')

# Plot the average as a vertical line
avg = top_10['Sales'].mean()
ax0.axvline(x=avg, color='b', label='Average', linestyle='--', linewidth=1)

# Repeat for the unit plot
top_10.plot(kind='barh', y="Purchases", x="Name", ax=ax1)
avg = top_10['Purchases'].mean()
ax1.set(title='Units', xlabel='Total Units', ylabel='')
ax1.axvline(x=avg, color='b', label='Average', linestyle='--', linewidth=1)

# Title the figure
fig.suptitle('2014 Sales Analysis', fontsize=14, fontweight='bold');

# Hide the legends
ax1.legend().set_visible(False)
ax0.legend().set_visible(False)
%matplotlib inline     #show figures inline Jupyter

fig.canvas.get_supported_filetypes() # list supported formats for saving files ex jpg,jpeg,pdf,eps,ps,png,svg,svgz,tif,tiff

fig.savefig('sales.png', transparent=False, dpi=80, bbox_inches="tight") # saves the plot as a png with opaque background,customizing dpi nd bbox_inches="tight" in order to minimize excess white space.