Data Analysis · Matplotlib · Plotting in Python

Plot multiple lines in one chart with different style Python matplotlib

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

We start with the simple one, only one line:

import matplotlib.pyplot as plt

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


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^')

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

import matplotlib.pyplot as plt
import numpy as np



ax.plot(x,x+1,c='g',marker=(8,2,0),ls='--',label='Greedy Heuristic')


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

These are the resources from matplotlib that may useful:

  1. Marker types of matplotlib
  2. Line styles matplotlib
  3. Matplotlib marker explanation


Experiment case using real Excel dataset and plot it to line chart with different markers automatically without define one by one for each line can be seen on this post

*Some part of the codes, I took from StackOverflow

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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s