Line charts

Python Courses

Line chart
You can use Matplotlib to create a line chart with Python. Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension numpy.  This takes only a few lines:

import numpy as np
import matplotlib.pyplot as plt

x = [2,3,4,5,7,9,13,15,17]

In the first two lines we include the numpy and matplotlib library. This contains logical functions to create line charts amongst others. We define a list named x with a few random numbers and we set the x and y label. Finally we call the function show() which will display the line chart. If you do not call the show() function, nothing will be shown on the screen.


by changing the plot function call to:

plt.plot(x, 'ro-')

we create a red graph with dots, where the r in ‘ro-‘ indicates red. Changing this to ‘bo-‘ would create a blue dotted line. If you only want to display the dots, use ‘ro’ or ‘bo’ instead.

Command line arguments

9 thoughts on “Line charts

  1. Reply
    Harinezumi - May 17, 2016

    I have a recommendation: it would be nice if you stated what version of python you are referring to in this tutorial, as well as tell users about installing libraries. This example uses numpy and matplotlib, which (at least in my case) were not part of python installation by default

  2. Reply
    augusto - June 8, 2016

    Can you please describe where the alias np start his action or say what does it (np) do in this code lines ? Thanks a lot !!

    1. Reply
      admin - July 22, 2016

      This line is not needed, it imports a module.

  3. Reply
    harsha - June 10, 2016

    what is use of import numpy as np in code?

    1. Reply
      admin - July 22, 2016

      That line can be removed

  4. Reply
    Ayush khanduri - August 2, 2016

    it gives me an error
    import matplotlib.pyplot as plt
    ImportError: No module named matplotlib.pyplot

    would it be because i have multiple versions of python installed in my linux.?

    1. Reply
      admin - August 6, 2016

      Multiple python versions is not a problem, install matplotlib: pip install matplotlib

  5. Reply
    Lena Khanpour - August 20, 2016

    how can I install matplotlib? I have the same problem as Ayush!

    1. Reply
      admin - August 20, 2016

      sudo apt-get install python-matplotlib

      for other systems there is an install guide here:

Leave a Reply

Your email address will not be published. Required fields are marked *