Plotting a basic line graph.

In [1]:
# only necessary for iPython Notebook to place the graph in the HTML page, ignore
%matplotlib inline

First, we need to import two modules, Matplotlib's PyPlot and Numpy.

In [2]:
import numpy as np
In [3]:
import matplotlib.pyplot as plt

Now, let's create a set of random data to plot using Numpy's Random. Within Random, there is a basic random function, numpy.random.random.

In [4]:
data = np.random.random(100) # 100 is the number of random points we will be plotting

The data looks something like this:

In [5]:
print(data)
[  5.29320947e-01   3.51632044e-01   1.95760145e-01   5.57832803e-01
   3.20881940e-01   7.52050764e-01   4.99573701e-01   9.98125261e-01
   7.22189940e-01   2.43706680e-01   8.82315741e-01   6.26541946e-01
   2.85815555e-01   7.90577851e-02   2.13862349e-01   2.30589133e-01
   5.83819574e-01   6.92446528e-01   3.62670956e-01   8.64835217e-01
   1.53643595e-01   8.92548003e-01   8.61344118e-01   4.88307785e-01
   8.52331067e-01   5.52326200e-01   3.33885793e-01   5.79426242e-01
   9.49844602e-01   2.82730606e-01   7.95123714e-01   4.29002458e-01
   7.47537196e-01   7.04689635e-01   4.12038084e-01   8.43929185e-03
   8.81859100e-01   2.11204039e-01   4.76182609e-02   6.40114089e-01
   6.04189265e-01   6.33368192e-01   4.55686480e-01   4.53116252e-01
   2.98215152e-01   6.60013248e-01   4.95465403e-01   2.73254613e-01
   3.95894387e-01   8.92656910e-02   4.20161961e-02   9.63127194e-01
   7.64205983e-01   1.01219360e-01   2.25167052e-01   1.18437429e-01
   9.63415004e-01   3.19635863e-01   5.31380262e-01   7.61067154e-01
   9.57190683e-01   6.33914723e-01   8.83612753e-01   9.40388415e-02
   2.45706916e-04   2.02895375e-01   3.28732981e-01   9.52428119e-01
   2.26572103e-01   1.90704827e-01   3.86729263e-01   4.59366616e-01
   9.59553192e-01   8.36707733e-01   8.69924212e-01   8.05535078e-01
   8.45503152e-01   4.06232277e-01   4.07087094e-01   5.69832454e-01
   1.89060912e-02   6.66035053e-01   8.48944866e-01   9.74002537e-01
   2.26445952e-01   1.71666191e-01   7.72469768e-01   3.54048050e-01
   1.50972919e-02   9.37655911e-01   8.65420489e-01   8.02760163e-01
   6.27861833e-03   1.30834235e-01   2.08843537e-02   6.98879893e-01
   2.92590679e-01   8.35077461e-01   6.06995526e-01   7.00617479e-01]

Next, let's plot the data in a plain line graph

In [6]:
plt.plot(data)
Out[6]:
[<matplotlib.lines.Line2D at 0x106286110>]

Now, let's make the plot a little more customized.

In [7]:
plt.plot(data, color='black', linestyle='dashed', marker='o',
     markerfacecolor='red', markersize=6)
Out[7]:
[<matplotlib.lines.Line2D at 0x1063b67d0>]

Notice you can easily specify the color of the line and markers, the style of the line and markers (even hiding either one), and customize any other aspect of the line plot not shown here. Python allows you to choose colors in a number of ways, I recommend using the names of HTML colors as strings. I use this link: http://www.w3schools.com/html/html_colornames.asp as a reference for many of the availabe colors.

Let's just go back to a simpler plot:

In [8]:
plt.plot(data,color='red',linewidth=2)
Out[8]:
[<matplotlib.lines.Line2D at 0x1065d9110>]

And now, let's add labels, a legend, and a title.

In [9]:
plt.plot(data,color='red',linewidth=2,label='Data')
plt.title('Random Data Plot')
plt.xlabel('Datapoint #')
plt.ylabel('Random Value')
plt.legend()
Out[9]:
<matplotlib.legend.Legend at 0x1066df7d0>
In []:
plt.show() # use this command to show the plot in an X-Window
plt.savefig('filename.png') # use this command to save the image as a .png or .pdf or .svg (depending on what the filename is)

That's all there is to making a basic line plot. For more information on Matplotlib, including all of the plot types and the ways you can customize the plots, go to http://www.matplotlib.org.

Back to Examples