Matplotlib Settings for Jupyter Notebook

In [ ]:
# Default setting for whole notebook
import matplotlib as mpl
mpl.rc('lines', linewidth=2, color='r', markersize=10)
mpl.rc('axes', titlesize=24, labelsize=20)
mpl.rc('xtick', labelsize=16)
mpl.rc('ytick', labelsize=16)

font = {'family' : 'monospace',
        'weight' : 'bold',
        'size'   : 'larger'}
mpl.rc('font', **font)  # pass in the font dict as kwargs

## Plot Style
# Set the style to 'ggplot'
plt.style.use('ggplot')


#-------------------------------------------------------------------------------
## Add grid() call to show grid lines but depend on style also.
plt.grid(True)

#-------------------------------------------------------------------------------
## Use plt.tight_layout() to improve the spacing between subplots
plt.tight_layout()

## All of thea rc settings are stored in a dictionary-like variable called
## matplotlib.rcParams, which is global to the matplotlib package.
plt.rcParams['figure.figsize'] = (5.0, 4.0) # set default size of plots

## Color paletter
 ['#e6194b', '#3cb44b', '#ffe119', '#0082c8', '#f58231', 
  '#911eb4', '#46f0f0', '#f032e6', '#d2f53c', '#fabebe', 
  '#008080', '#e6beff', '#aa6e28', '#fffac8', '#800000', 
  '#aaffc3', '#808000', '#ffd8b1', '#000080', '#808080', 
  '#FFFFFF', '#000000']
    
#-------------------------------------------------------------------------------
## Show and clean up again. NOT REQUIRED with Jupyter notebook
plt.show()
plt.clf()

Various Plots

In [ ]:
## Basic Line Plot
plt.plot(x, y, color='b')

#-------------------------------------------------------------------------------
## Scatter Plot
plt.scatter(x = x, y = y, s = size_array, c = col_array, alpha = 0.8)
# Note: size and color array should be of same length or length = 1

#-------------------------------------------------------------------------------
## Histogram with 20 bins
plt.hist(x, bins=20)

#-------------------------------------------------------------------------------
## if any axis values range is too large from 1 to billions then we can take
## log of that axis values and scale it down.
plt.xscale('log') # plt.yscale('log')

Histogram in Numpy

In [ ]:
# Annotate a text for maximum value
# Creating a histogram table in numpy
hist_series = np.histogram(df['column'].dropna(), bins=50)

Horizontal Bar Plots with Values on bar

In [ ]:
plt.figure(figsize=(20, 7))
plt.subplot(1, 2,1)
# plt.figure(figsize=(13, 8))
count = df[~df['within_borough']]['pickup_borough'].value_counts()
count = count[:4]
sns.barplot(x=count.values, y=count.index, orient='h', palette=color[0:8])

for index, value in enumerate(count.values):
        s = str('{:,}'.format(value))+ " | " + str(np.round(100*value/count.sum(), 2)) + ' %'
        plt.text(1000, index, s, fontdict={'size': 16, 'fontweight':'bold', 'color':'black'})

Overlap Plots

In [ ]:
#-------------------------------------------------------------------------------
# Specify the label 'Computer Science'
plt.plot(x1, y1, color='red', label='Label1')

# Specify the label 'Physical Sciences'
plt.plot(x2, y2, color='blue', label='Label2')

# Add a legend at the lower center
plt.legend(loc='lower center')

Plot Customization

In [ ]:
#===============================================================================
#                               PLOT CUSTOMIZATION                             #
#===============================================================================
## Add Labels and title
plt.suptitle('Super Title', size=18, fontweight='bold')
plt.title('title', size=18)
plt.xlabel('X-label')
plt.ylabel('y-label')

#-------------------------------------------------------------------------------
# Set the axis range
plt.xlim([1990, 2010])
plt.ylim([0, 50])

# Set the x-axis and y-axis limits AT ONCE
plt.axis([1990, 2010, 0, 50])

# Angle and size of xticks
plt.xticks(size=14, rotation=45);
plt.yticks(size=14, rotation=45);
#-------------------------------------------------------------------------------
## Definition of tick_val and tick_lab
tick_val_x = [1000,10000,100000] # only show this values in axis
tick_lab_x = ['1k','10k','100k'] # change above ticks to these values. As you can think length of array should be same.

# Adapt the ticks on the x-axis
plt.xticks(tick_val_x, tick_lab_x)
plt.yticks(tick_val_y, tick_lab_y)

Annnotate Plots

Add text to plot

In [ ]:
#-------------------------------------------------------------------------------
## Add text to plot
plt.text(x_cordinate, y_cordinate, 'test-text')

Arrow and Text on Plot

In [ ]:
ax = sns.distplot(df['column'], bins=50, 
             kde_kws={'color':'r', "label": "KDE", "lw": 4}, 
             hist_kws={'color':color[7], 'alpha':1})

ax.annotate('Annotate Text', 
            xy=(13.5, 0.102), xytext=(20, 0.09),
            arrowprops=dict(facecolor='black', shrink=0.05), fontsize=18, color='red');

Add shapes in Plot

In [ ]:
rectangle = plt.Rectangle((4.2, 10), 2.3, 8.2, 
                          fill=None, edgecolor='red', 
                          linestyle='--', linewidth=3)

plt.gca().add_patch(rectangle)

Drawing Subplots

In [ ]:
#-------------------------------------------------------------------------------
## Multiple Subplots using subplot method
# Create a figure with 1x2 subplot and make the left subplot active
plt.subplot(1, 2, 1)
# Plot in blue the % of degrees awarded to women in the Physical Sciences
plt.plot(x1, y1, color='blue')
plt.title('Plot 1')

# Make the right subplot active in the current 1x2 subplot grid
plt.subplot(1, 2, 2)

# Plot in red the % of degrees awarded to women in Computer Science
plt.plot(x2, y2, color='red')
plt.title('Plot 2')

Subplot Settings

In [ ]:
#===============================================================================
#								 SUBPLOTS                                      #
#===============================================================================
## Using Axes to show multiple plots at once
plt.axes([0.05, 0.05, 0.425, 0.9])

# Plot in blue the % of degrees awarded to women in the Physical Sciences
plt.plot(year, physical_sciences, color='blue')

# Create plot axes for the second line plot
plt.axes([0.525, 0.05, 0.425, 0.9])

# Plot in red the % of degrees awarded to women in Computer Science
plt.plot(year, computer_science, color='red')

Saving plot on disk

In [ ]:
# Save the image as 'plot1.png'
plt.savefig('plot1.png')