Matplotlib · NumPy · Pandas · Plotting in Python

Read the data and plotting with multiple markers

Just assume that we have an excel data and we want to plot it on a line chart with different markers. Why markers? just imagine, we have plotted a line chart with multiple lines inside by different colour, but we only have black and white ink, after printing the image to the paper, all lines will be in black colour. That’s why we need markers.


The data is about algorithms performance vs. the number of k. I want to plot this data in one figure of a line chart.  We already have the previous experiment, how to plot the line chart with multiple lines and multiple styles. However, in that previous experiment, we still use the static declaration of each line. It will be hard if we have to declare one by one for each line. Let’s see…

The first step is to load that Excel data to the DataFrame in pandas.

import pandas as pd
import numpy as np

import matplotlib as mpl

xl = pd.ExcelFile("Experiment_results.xlsx")

df = xl.parse("Sheet2", header=1, index_col=0)

The data can be seen in the picture above, it very easy to load the Excel data to DataFrame, we can use some parameters which very useful such as sheet name, header, an index column. In this experiment, I use “Sheet2″ due to my data in the Sheet2, and I use ”1″ as the header parameter which means I want to load the header to the DataFrame, and if you don’t want to load it just fill it with ”0″. In here, I also use index_col equal to “0”, which means I want to make the first column in my Excel dataset as the index in DataFrame.

The second step is about the markers. As I said in the previous experiment that matplotlib supports a lot of markers. Of course, I don’t want to define one by one manually. Let see the code below:

# create valid markers from mpl.markers
valid_markers = ([item[0] for item in mpl.markers.MarkerStyle.markers.items() if
item[1] is not 'nothing' and not item[1].startswith('tick') and not item[1].startswith('caret')])

# valid_markers = mpl.markers.MarkerStyle.filled_markers

markers = np.random.choice(valid_markers, df.shape[1], replace=False)

Now, we have a list of markers inside the ‘markers’ variable. We need to select the markers randomly and the number of markers that we want to select is defined by df.shape[1], which means the number of columns. Let start to plot the data.

ax = df.plot(kind='line')
for i, line in enumerate(ax.get_lines()):

# adding legend
ax.legend(ax.get_lines(), df.columns, loc='best')

Taraaaa!!!!, it’s easy, right?




One thought on “Read the data and plotting with multiple markers

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s