Back to all posts

Matplotlib Pyplot for Visualisation

Pyplot Most of the Matplotlib utilities lies under the  pyplot  submodule, and are usually imported under the  plt  alias: import matpl…

Pyplot

Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported under the plt alias:

JavaScript
import matplotlib.pyplot as plt

Color Reference
'r' Red, 'g' Green, 'b' Blue, 'c' Cyan, 'm' Magenta, 'y' Yellow, 'k' Black, 'w' White

Line Reference
'-' Solid line
':' Dotted line
'--' Dashed line
'-.' Dashed/dotted line

Marker Reference
'o' Circle
'*' Star
'.' Point
',' Pixel
'x' X
'X' X (filled)
'+' Plus
'P' Plus (filled)
's' Square
'D' Diamond
'd' Diamond (thin)
'p' Pentagon
'H' Hexagon
'h' Hexagon
'v' Triangle Down
'^' Triangle Up
'<' Triangle Left '>' Triangle Right
'1' Tri Down
'2' Tri Up
'3' Tri Left
'4' Tri Right
'|' Vline
'_' Hline

Shortcut string notation parameter to specify the marker.

marker|line|color

PHP
import matplotlib.pyplot as plt
import numpy as np

font1 = {'family':'serif','color':'blue','size':20}
font2 = {'family':'serif','color':'darkred','size':15}

x1 = np.array(&#091;0, 1, 2, 3])
y1 = np.array(&#091;3, 8, 1, 10])
x2 = np.array(&#091;0, 1, 2, 3])
y2 = np.array(&#091;6, 2, 7, 11])

plt.plot(x1,y1,x2,y2,"*--r" )
#plt.grid()
plt.grid(axis = 'x')
plt.title("Sports Watch Data",fontdict=font1)
plt.xlabel("Average Pulse",fontdict=font2)
plt.ylabel("Calorie Burnage",fontdict=font2)
plt.show()

General Plot Properties (Common to All Plots)

  • title(label, fontdict=None, loc='center', pad=None): Set title of the plot.
    • label: Title text.
    • fontdict: Font properties (e.g., size, weight).
    • loc: Location ('left', 'center', 'right').
    • pad: Padding between title and plot.
  • xlabel(label, fontdict=None, labelpad=None): Set label for the x-axis.
    • label: X-axis label text.
    • fontdict: Font properties.
    • labelpad: Spacing between label and axis.
  • ylabel(label, fontdict=None, labelpad=None): Set label for the y-axis.
    • label: Y-axis label text.
    • fontdict: Font properties.
    • labelpad: Spacing between label and axis.
  • legend(loc='best', bbox_to_anchor=None, fontsize=None, ncol=1, title=None, fancybox=None, shadow=None): Add a legend to the plot.
    • loc: Location of the legend ('best', 'upper right', etc.).
    • bbox_to_anchor: A tuple for positioning the legend.
    • fontsize: Size of the text in the legend.
    • ncol: Number of columns in the legend.
    • title: Title for the legend box.
    • fancybox: Use a rounded box for the legend.
    • shadow: Add shadow to the legend.
  • grid(visible=True, which='both', axis='both', color='black', linestyle='-', linewidth=1): Display a grid.
    • visible: Display gridlines or not.
    • which: Which gridlines to apply ('major', 'minor', 'both').
    • axis: Axis on which gridlines appear ('x', 'y', 'both').
    • color: Color of the gridlines.
    • linestyle: Line style of the gridlines.
    • linewidth: Width of the gridlines.
  • xlim(left=None, right=None): Set limits for the x-axis.
    • left: Lower limit.
    • right: Upper limit.
  • ylim(bottom=None, top=None): Set limits for the y-axis.
    • bottom: Lower limit.
    • top: Upper limit.
  • xticks(ticks=None, labels=None, rotation=None): Set the ticks on the x-axis.
    • ticks: Positions of ticks.
    • labels: Labels for ticks.
    • rotation: Rotation of tick labels.
  • yticks(ticks=None, labels=None, rotation=None): Set the ticks on the y-axis.
    • ticks: Positions of ticks.
    • labels: Labels for ticks.
    • rotation: Rotation of tick labels.

Keep building your data skillset

Explore more SQL, Python, analytics, and engineering tutorials.