Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Line Plot | Creating Commonly Used Plots
Ultimate Visualization with Python

Swipe to show menu

book
Line Plot

Applications

Line plot is used to depict the relationship between two variables (e.g. x, y) using straight lines. More formally, it shows the relationship between continuous or ordinal variables in a continuous data point manner. Moreover, it can show how a certain variable changes with time.

Creating a Line Plot

Use the familiar plot() function from pyplot to create line plots. Here's an example that displays a quadratic relationship between two variables:

123456789
import matplotlib.pyplot as plt import numpy as np data_linear = np.arange(0, 6) data_squared = data_linear ** 2 # Creating a line plot with specifying x and y plt.plot(data_linear, data_squared, '-o') plt.show()
copy

In fact, this code can even be further simplified. Have a look at another example:

123456
import matplotlib.pyplot as plt import numpy as np data_squared = np.arange(0, 6) ** 2 # Creating a line plot using only one array plt.plot(data_squared, '-o') plt.show()
copy

When only one array (or pandas.Series()) like data_squared is passed to the plot() function, matplotlib uses the array values for the y-axis. The x-axis is automatically generated using the element indices β€” in this case, the numbers from 0 to 5.

Task

Swipe to start coding

  1. Use the correct function for creating a line plot.
  2. Pass in the correct order x_data (x-axis) and y_data (y-axis) as the first two arguments.
  3. Pass the rightmost argument such that the plot will have 'o' markers and dashed lines.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
We're sorry to hear that something went wrong. What happened?

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

book
Line Plot

Applications

Line plot is used to depict the relationship between two variables (e.g. x, y) using straight lines. More formally, it shows the relationship between continuous or ordinal variables in a continuous data point manner. Moreover, it can show how a certain variable changes with time.

Creating a Line Plot

Use the familiar plot() function from pyplot to create line plots. Here's an example that displays a quadratic relationship between two variables:

123456789
import matplotlib.pyplot as plt import numpy as np data_linear = np.arange(0, 6) data_squared = data_linear ** 2 # Creating a line plot with specifying x and y plt.plot(data_linear, data_squared, '-o') plt.show()
copy

In fact, this code can even be further simplified. Have a look at another example:

123456
import matplotlib.pyplot as plt import numpy as np data_squared = np.arange(0, 6) ** 2 # Creating a line plot using only one array plt.plot(data_squared, '-o') plt.show()
copy

When only one array (or pandas.Series()) like data_squared is passed to the plot() function, matplotlib uses the array values for the y-axis. The x-axis is automatically generated using the element indices β€” in this case, the numbers from 0 to 5.

Task

Swipe to start coding

  1. Use the correct function for creating a line plot.
  2. Pass in the correct order x_data (x-axis) and y_data (y-axis) as the first two arguments.
  3. Pass the rightmost argument such that the plot will have 'o' markers and dashed lines.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt