Feeds:
Posts

## Programming in Linux – Python Library Matplotlib 01 – Introduction to Plotting

Matplotlib is a plotting library for python. Matplotlib was originally written by John D Hunter during his post-doctoral research in neurobiology to visualize electrocorticography (ECoG) data of epilepsy patients. Then Michael Droettboom lead the development project after John Hunter passed away in August 2012.
Matplotlib version 2.0.x support python version 2.7 – 3.6.
Starting 2020, Matplotlib does not support python 2 anymore.

Using Matplotlib with python is easy.

I will show you how to visualize a simple graph.
In this tutorial, I use IDLE as python editor.
If you don’t have it, you can install using command below from your Linux Terminal.
\$ sudo apt-get install idle

Run the idle. From menu, select File>New File to display the Editor.
Type the code below and press F5 to run the code.

```import matplotlib.pyplot as plt
x=[1,2,3,4]
y=[5,6,7,8]
plt.plot(x,y)
plt.show()
```  You have your first graph with Matplotlib.
It’s easy, right?

Title, X label, Y label, Grid
Now, you can add more details like, graph title, label for X and Y and also Grid to your graph.

```import matplotlib.pyplot as plt
x=[1,2,3,4]
y=[5,6,7,8]
plt.plot(x,y)
plt.title('Simple Graph')
plt.xlabel('x series')
plt.ylabel('y series')
plt.grid()
plt.show()```  Legend
Type the code below and run it.

```import matplotlib.pyplot as plt
x=[1,2,3,4]
y=[5,6,7,8]
x2=[1,2,3,4]
y2=[8,7,6,5]
plt.plot(x,y)
plt.plot(x2,y2)
plt.title('Simple Graph')
plt.xlabel('x series')
plt.ylabel('y series')
plt.grid()
plt.show()```  If you have 2 plot in 1 graph, it’s difficult to justify which one represent which data.
That’s why you need ‘Legend’.

Type the code below and run.

```import matplotlib.pyplot as plt
x=[1,2,3,4]
y=[5,6,7,8]
x2=[1,2,3,4]
y2=[8,7,6,5]
plt.plot(x,y)
plt.plot(x2,y2)
plt.title('Simple Graph')
plt.xlabel('x series')
plt.ylabel('y series')
plt.grid()
plt.legend(['y=x','y2=x2'])
plt.show()```  Now, the graph is easy to read.

The legend data should follow the sequence which plot that you will display first.
Since the plt.plot(x,y) is displayed first then in legend should be mentioned first, plt.legend([‘y=x’,’y2=x2′]).

How about if we change the sequence and we missed to revised the legend then the graph will be wrong.

```plt.plot(x2,y2)
plt.plot(x,y)
...
...
plt.legend(['y=x','y2=x2'])```

To avoid that happen, better we put the legend data directly on the plot function.
Type the code below and run.

```import matplotlib.pyplot as plt
x=[1,2,3,4]
y=[5,6,7,8]
x2=[1,2,3,4]
y2=[8,7,6,5]
plt.plot(x2,y2,label='y2=x2')
plt.plot(x,y,label='y=x')
plt.title('Simple Graph')
plt.xlabel('x series')
plt.ylabel('y series')
plt.grid()
plt.legend()
plt.show()```  Now as you can see, although the y2=x2 is display first and the color is changed, but the legend is still correct.

Format String
You can change the marker, line and color of the plot using Format String.
Format = ‘[marker][line][color]’
For example, if you to change your plot to circle marker, line dash style and magenta color, the syntax will be:

`‘o--b’`

Let’s try.

```import matplotlib.pyplot as plt
x=[1,2,3,4]
y=[5,6,7,8]
x2=[1,2,3,4]
y2=[8,7,6,5]
plt.plot(x,y,'o--m',label='y=x')
plt.plot(x2,y2,label='y2=x2')
plt.title('Simple Graph')
plt.xlabel('x series')
plt.ylabel('y series')
plt.grid()
plt.legend()
plt.show()```  If you want your code to be more easier to read, you can use property like below:

```import matplotlib.pyplot as plt
x=[1,2,3,4]
y=[5,6,7,8]
x2=[1,2,3,4]
y2=[8,7,6,5]
plt.plot(x,y,color='m',linestyle='--',marker='o',label='y=x')
plt.plot(x2,y2,label='y2=x2')
plt.title('Simple Graph')
plt.xlabel('x series')
plt.ylabel('y series')
plt.grid()
plt.legend()
plt.show()```

The result is the same, but code it’s easier to understand.  There are available styles ready that you can use for your graph.
Type the code below and run

```import matplotlib.pyplot as plt
plt.style.use('dark_background')
x=[1,2,3,4]
y=[5,6,7,8]
x2=[1,2,3,4]
y2=[8,7,6,5]
plt.plot(x,y,label='y=x')
plt.plot(x2,y2,label='y2=x2')
plt.title('Simple Graph')
plt.xlabel('x series')
plt.ylabel('y series')
plt.grid()
plt.legend()
plt.show()```  You list down the available styles from matplotlib, so you can try which one that is fulfill your need.

```>>> import matplotlib.pyplot as plt
>>> for item in plt.style.available:
print(item)
seaborn-muted
seaborn-deep
grayscale
ggplot
seaborn-dark-palette
seaborn-white
Solarize_Light2
fivethirtyeight
_classic_test
fast
bmh
seaborn
seaborn-bright
classic
seaborn-darkgrid
seaborn-dark
seaborn-ticks
seaborn-paper
seaborn-notebook
seaborn-poster
seaborn-pastel
seaborn-talk
seaborn-whitegrid
dark_background
tableau-colorblind10
seaborn-colorblind
>>>```

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

Line Styles

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

Colors

The supported color abbreviations are the single letter codes

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