Learn how to draw using matplotlib from scratch

PHPz
Release: 2024-01-10 08:29:53
Original
647 people have browsed it

Learn how to draw using matplotlib from scratch

Learn how to draw using Matplotlib from scratch

Matplotlib is a powerful Python data visualization library that can be used to create various types of graphs and charts. It is widely used in the fields of data science and machine learning, as well as other jobs that require displaying data. This article will introduce how to learn to use Matplotlib to draw pictures from scratch, and provide specific code examples.

Install Matplotlib
First, we need to install the Matplotlib library. You can use the pip command to install:

pip install matplotlib
Copy after login

Import Matplotlib
After the installation is complete, use the following code in the Python program to import the Matplotlib library:

import matplotlib.pyplot as plt
Copy after login

Create simple graphics
Let’s follow You will learn how to draw using Matplotlib through some simple examples. First, we can create a simple line chart using the plot() function of the matplotlib.pyplot module. Here is an example:

import matplotlib.pyplot as plt

# 创建x和y的数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# 使用plot()函数创建折线图
plt.plot(x, y)

# 显示图形
plt.show()
Copy after login

After running the code, you will see a simple line chart displayed on the screen. In this example, we create two lists as the data for x and y, pass these data to Matplotlib using the plot() function, and then display the graph using the show() function.

Custom graphics
In Matplotlib, we can customize graphics in various ways, including adding titles, axis labels, legends, etc. Here is an example showing how to customize the graph:

import matplotlib.pyplot as plt

# 创建x和y的数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# 使用plot()函数创建折线图,并设置线条颜色和类型
plt.plot(x, y, color='red', linestyle='dashed')

# 添加标题
plt.title('折线图')

# 添加x和y轴标签
plt.xlabel('x轴')
plt.ylabel('y轴')

# 显示图例
plt.legend(['y = 2x'])

# 显示图形
plt.show()
Copy after login

In this example, we use some additional functions to customize the graph. For example, use the title() function to set the title of the graph, use the xlabel() and ylabel() functions to add x- and y-axis labels, and use the legend() function to display the legend.

Different types of graphics
In addition to line charts, Matplotlib also supports many other types of graphics, such as scatter plots, bar charts, pie charts, etc. Here is some sample code to show how to draw different types of graphs:

import matplotlib.pyplot as plt

# 散点图
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.scatter(x, y)

# 柱状图
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.bar(x, y)

# 饼图
sizes = [15, 30, 45, 10]
labels = ['A', 'B', 'C', 'D']
plt.pie(sizes, labels=labels)

plt.show()
Copy after login

In these examples, we use the corresponding functions of the matplotlib.pyplot module to create scatter plots, bar charts, and pie charts. You can choose the appropriate function to draw different types of graphics according to your needs.

Summary
This article introduces how to learn to use Matplotlib to draw from scratch. We first installed the Matplotlib library, then learned how to draw a line chart through some simple examples, and customized the graphics. Finally, we also show how to draw different types of graphs. I hope these examples will help you become more comfortable using Matplotlib.

The above is the detailed content of Learn how to draw using matplotlib from scratch. 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!