# Plot multiple lines on one chart with different style Python matplotlib

Sometimes we need to plot multiple lines on one chart using different styles such as dot, line, dash, or maybe with different colour as well. It is quite easy to do that in basic python plotting using matplotlib library.

```
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])

# when you want to give a label
plt.xlabel('This is X label')
plt.ylabel('This is Y label')
plt.show()

``` Let’s go to the next step, several lines with different colour and different styles.

```
import numpy as np
import matplotlib.pyplot as plt

# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)

# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()

``` If only three lines, it seems still easy, how if there are many lines, e.g: six lines.

```
import matplotlib.pyplot as plt
import numpy as np

x=np.arange(6)

fig=plt.figure()

ax.plot(x,x,c='b',marker="^",ls='--',label='Greedy',fillstyle='none')
ax.plot(x,x+1,c='g',marker=(8,2,0),ls='--',label='Greedy Heuristic')
ax.plot(x,(x+1)**2,c='k',ls='-',label='Random')
ax.plot(x,(x-1)**2,c='r',marker="v",ls='-',label='GMC')
ax.plot(x,x**2-1,c='m',marker="o",ls='--',label='KSTW',fillstyle='none')
ax.plot(x,x-1,c='k',marker="+",ls=':',label='DGYC')

plt.legend(loc=2)
plt.show()

``` Now, we can plot multiple lines with multiple styles on one chart.

These are some resources from matplotlib documentation that may useful:

1. Marker types of matplotlib https://matplotlib.org/examples/lines_bars_and_markers/marker_reference.html
2. Line styles matplotlib https://matplotlib.org/1.3.1/examples/pylab_examples/line_styles.html
3. Matplotlib marker explanation https://matplotlib.org/api/markers_api.html

In this experiment, we define each line manually while it can be hard if we want to generate line chart from dataset. In the next experiment, we use real an Excel dataset and plot the data to line chart with different markers without defining one by one for each line -> just check it here https://pydatascience.org/2017/12/05/read-the-data-and-plotting-with-multiple-markers/

*Some part of the codes, I took from StackOverflow

## One thought on “Plot multiple lines on one chart with different style Python matplotlib”

This site uses Akismet to reduce spam. Learn how your comment data is processed.