avatarBetter Everything

Summary

The webpage provides a comprehensive guide on how to plot lines and functions using the Python package matplotlib.

Abstract

The article titled "Plot Lines and Functions with Python" is a tutorial aimed at teaching readers how to visualize data by plotting lines on graphs using the Python library matplotlib. It begins by explaining the installation process for matplotlib and then delves into the use of the pyplot module for plotting lines between two points, connecting multiple points, and graphing mathematical functions like y = x². The tutorial also covers customization options for plotted lines, such as changing colors, adding markers, and altering line styles. Additionally, it demonstrates how to plot multiple lines on a single graph and provides tips for enhancing the aesthetics and clarity of graphs. The author encourages readers to join Medium for full access to the article and other content, and also promotes their other work related to data visualization in Python.

Opinions

  • The author believes that matplotlib is a powerful tool for data visualization in Python.
  • They suggest that by following the article, readers will gain the ability to create their own line graphs.
  • The article implies that customizing plots with colors, markers, and line styles can make data visualizations more informative and appealing.
  • The author emphasizes the importance of clear and attractive graph design for better data communication.
  • They endorse the idea that joining Medium is beneficial for readers interested in accessing more in-depth content on Python and data visualization.

Plot Lines and Functions with Python

The Python package matplotlib is great for visualizing data. A common task is plotting a line on a graph, that is what we are going to look at in this article.

By the end of this article you should be able to plot lines on graphs like this:

2 Examples of lines on graphs that you will be able to make yourself by following this article. Source: own images.

Installing and import matplotlib

To be able to use the matplotlib package you have to install it, which can be done with the command: pip install matplotlib.

To learn more about Python packages and installing them you can read this article of mine.

After installing you can import the matplotlib collection of functions called pyplot as follows: import matplotlib.pyplot as plt.

How lines are plotted

A line is a connection between 2 points. If you define 2 points, a line can be drawn by coloring in the pixels between them.

The plot function

The function that we will use is the plot function that comes with the pyplot collection of functions.

The plot function can be used to plot single points on a graph, but today we are going to plot lines by specifying at least 2 points.

A line between 2 points

Suppose we have 2 points:

  1. Point 1 has X-axis (horizontal) coordinate 0 and Y-axis (vertical) coordinate 0. The coordinate would be (0,0).
  2. Point 2 has X-axis coordinate 10 and Y-axis coordinate 100. The coordinate would be (10,100).

We can put this data in Python like this:

point_1_x = 0
point_1_y = 0

point_2_x = 10
point_2_y = 100

And then we can draw a line between these points by passing a list with x-coordinates and a list with y-coordinates to the plot function like this:

import matplotlib.pyplot as plt

point_1_x = 0
point_1_y = 0

point_2_x = 10
point_2_y = 100

plt.plot([point_1_x,point_2_x],[point_1_y,point_2_y])

plt.show()

This is the graph with the line that the code makes:

A line between 2 points made with matplotlib’s pyplot function: plot. Source: own image.

A line that connects multiple points

We just passed 2 lists with a length of 2 to the plot function, but these lists can also be longer.

If we use lists with 10 items in them we are basically connecting 10 points with a line.

import matplotlib.pyplot as plt

x_coordinates = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y_coordinates = [0, 2, 4, 8, 16, 32, 64, 128, 256, 512]

plt.plot(x_coordinates,y_coordinates)

plt.show()

The above code makes this graph:

A line that connects multiple points made with matplotlib’s pyplot function: plot. Source: own image.

Graphing a line based on a function in Python

If we have a mathematical function like y = x², we can also easily plot that with the plot function.

First we define along which range of x values we want to plot the function. We can use a range object for that, to which we pass 2 values. The start value and the stop value. The stepsize is by default 1. But you can overwrite that by specifying a different stepsize as the third argument.

When working with ranges, if you take stop value 100 for instance, it stops 1 step before that, not at 100 itself.

So if we want to plot a function from x=0 to x=100, we can specify the range of x values as follows: x_range = range(0,101). That will generate 101 values from 0 to 100.

We can then calculate the y value corresponding to each x value in a list comprehension: y_values = [x**2 for x in x_range].

x**2 is the Python way to note x². To learn more about list comprehensions you can read this short article of mine:

The list y_values will hold 101 y values. To get an idea, here are the first 10: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81].

We can pass x_range and y_values to the plot function, like we have been doing in the examples before, to plot the mathematical function:

import matplotlib.pyplot as plt

x_range = range(0,101)
y_values = [x**2 for x in x_range]

plt.plot(x_range,y_values)

plt.show()

The above code makes this graph:

A function plotted with matplotlib’s pyplot function: plot. Source: own image.

Extra options for plotting lines with the plot function

The plot function allows you to customize the lines that it plots by adding arguments to the function call.

Here are 3 of those options.

  1. Colors, with the color argument you can pass a color name like 'red' or a hex color code like '#FF0000' to give the line a color.
  2. Markers, with the marker argument you can make markers appear at the points that your line connects. Some examples are: 'o' for circles, 's' for squares and '*' for stars.
  3. Linestyle, with the linestyle argument you can make dotted (':') or dashed ('--') lines among other styles, instead of the default solid line.

Here is an example of applying the 3 options to a plotted line:

import matplotlib.pyplot as plt

x_range = range(0,100,10)
y_values = [x**2 for x in x_range]

plt.plot(x_range, y_values, color='red', marker='o', linestyle='--')
plt.show()

The code makes the following graph:

A line with markers, color and linestyle plotted with matplotlib pyplot function: plot. Source: own image.

Plotting multiple lines

Plotting multiple lines can be easily done by just calling the plot function multiple times. Here is an example:

import matplotlib.pyplot as plt

x_range = range(0,10,1)

plt.plot(x_range, [5*x for x in x_range], color='red')
plt.plot(x_range, [x**2 for x in x_range], color='blue')
plt.plot(x_range, [x+25 for x in x_range], color='black')

plt.show()

The graph that the code makes:

One matplotlib graph with 3 lines plotted on it. Source: own image.

Making a graph better looking and more understandable

2 Graphs that display the same data, but one is more descriptive and better looking. Source: own images.

If you want to learn how to make a simple graph more understandable and better looking by:

  • adding a title
  • adding labels
  • setting ticks at specific values and displaying specific tick labels
  • changing background color
  • changing the limits of the X- and Y-axis

and more, consider reading this article of mine:

Thank you for reading!

You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:

More charts and graphs in Python:

Data Science
Programming
Data Visualization
Matplotlib
Python
Recommended from ReadMedium