Learn the basic steps to draw a line chart with matplotlib

WBOY
Release: 2024-01-17 11:07:05
Original
1185 people have browsed it

Learn the basic steps to draw a line chart with matplotlib

Matplotlib is one of the most famous and commonly used data visualization libraries in Python. Mastering the basic steps of drawing line charts with Matplotlib is very important for data analysis work. This article will start from scratch, introduce the basic steps of drawing a line chart with Matplotlib for beginners, and provide specific code examples.

  1. Import matplotlib library

To start using Matplotlib to draw graphics, you first need to import the Matplotlib library. You can use the following code to import:

import matplotlib.pyplot as plt
Copy after login
  1. Prepare data

Before you are ready to start drawing a line chart, you need to prepare the data to be drawn. Typically, data is stored in data files. Here, we will use the Numpy library to generate a set of random data as sample data, as follows:

import numpy as np

x = np.arange(0, 10, 1)
y = np.random.rand(10)
Copy after login
  1. Create a graph

To create a graph, you can use matplotlib'splt.figureFunction. This function can specify the graphic size and other properties. An example is as follows:

plt.figure(figsize=(8,6), dpi=80)
Copy after login
  1. Draw a line chart

After preparing the data and graphics, the next step is to draw the line chart. To plot a line graph in Matplotlib, we use the plt.plot() function. The first parameter of this function is the x-axis data, and the second parameter is the y-axis data. An example is as follows:

plt.plot(x,y, color="blue", linewidth=1.5, linestyle="-", label="Random Data")
Copy after login

Among them, the color parameter specifies the color of the line, the linewidth parameter specifies the width of the line, and the linestyle parameter specifies the line style. , labelThe parameter specifies the label of the line chart line.

  1. Add legend

After drawing the line chart, we can add a legend to it to make it easier to read. A legend can be added using the plt.legend function. An example is as follows:

plt.legend(loc="upper left")
Copy after login

Among them, the loc parameter specifies the location of the legend. Here, we use "upper left" to place the legend in the upper left corner of the graph.

  1. Add Axis Labels and Titles

Axis labels and titles can make the graph more explicit. We can add X-axis labels, Y-axis labels and figure titles using the plt.xlabel, plt.ylabel and plt.title functions as follows:

plt.xlabel("x axis")
plt.ylabel("y axis")
plt.title("A Random Line Graph")
Copy after login
  1. Display graphics

Finally, we need to use the plt.show() function to display graphics, the example is as follows:

plt.show()
Copy after login

The complete code example is as follows:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 10, 1)
y = np.random.rand(10)

plt.figure(figsize=(8,6), dpi=80)
plt.plot(x,y, color="blue", linewidth=1.5, linestyle="-", label="Random Data")
plt.legend(loc="upper left")
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.title("A Random Line Graph")
plt.show()
Copy after login

Through this step, we have now mastered the basic steps of drawing a line chart with Matplotlib. I hope this sample code can help beginners understand more easily how to use Matplotlib for data visualization and graph drawing.

The above is the detailed content of Learn the basic steps to draw a line chart with matplotlib. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!