Matplotlib – Plot

Plot(): A pyplot.plot() function Plot horizontal versus vertical coordinates of the data points as lines and/or markers.

Parameters:

  • x,y  :  array-like or scalar.  x values are optional and default to range(len(y)).
  • fmt : str (Optional).  settings for line properties.  E.g ‘ro’ for red circle
  • data : indexable Object(Optional). An Object with labelled data.
  • Plot function also has other beautification parameters like a line label, line width, marker face color, etc.

 

fmt: Format string consists of a part for color, marker and line. Each of them is optional.

fmt = '[marker][line][color]' or '[color][marker][line]'
Markers

character description
'.' point marker
',' pixel marker
'o' circle marker
'v' triangle_down marker
'^' triangle_up marker
'<' triangle_left marker
'>' triangle_right marker
'1' tri_down marker
'2' tri_up marker
'3' tri_left marker
'4' tri_right marker
's' square marker
'p' pentagon marker
'*' star marker
'h' hexagon1 marker
'H' hexagon2 marker
'+' plus marker
'x' x marker
'D' diamond marker
'd' thin_diamond marker
'|' vline marker
'_' hline marker
Colors

character color
'b' blue
'g' green
'r' red
'c' cyan
'm' magenta
'y' yellow
'k' black
'w' white
Line Styles

character description
'-' solid line style
'--' dashed line style
'-.' dash-dot line style
':' dotted line style

Examples: 

import matplotlib.pyplot as plt
 
plt.plot([1,2,3,4,5], [1,4,9,16,25], 'go-', label='line 1', linewidth=2)
plt.plot([1,2,3,4,5], [2,6,10,20,30], 'rs--', label='line 2', linewidth=3)
plt.plot([10,20,30,40,50], 'y*-.', label='line 3')
 
plt.title("Example of Plot() ")
plt.xlabel("X")
plt.ylabel("Y")
plt.grid(True)
plt.legend()
plt.savefig("plot_example.jpg")

This produces the following result:

Leave a Reply

Your email address will not be published. Required fields are marked *

Matplotlib Tutorials

Matplotlib – Graph Decoration

Matplotlib – subplot2grid

Matplolib – Twin Axes

Matplotlib – Axes Class

Matplotlib – Pyplot API

Matplotlib – Violin plot

Matplotlib – Box Plot

Matplotlib – Histogram

Matplotlib – Pie Chart

Matplotlib – Bar Plot

Matplotlib – Scatter plot

Matplotlib – Figure

Matplotlib – Subplot

Matplotlib Introduction