The complete code of Matplotlib basic graphics using line chart-histogram-scatter plot-pie chart with examples

Table of contents

The use of Matplotlib basic line chart

1. Import the matplotlib library

2. Prepare data

3. Draw a line chart

4. Add tags and titles

5. Custom styles

6. Complete code

​edit

 

Draw a scatterplot

Import matplotlib library and numpy library

prepare data

Draw a scatterplot

​edit

draw pie chart

Import matplotlib library and numpy library

prepare data

draw pie chart

Code for all graphics:


The use of Matplotlib basic line chart

hello! Friends, let’s talk about matplotlib (true and universal Python drawing library) today. Let’s start with the basic line chart, step by step, and make sure you can get started easily!

1. Import the matplotlib library

The first library we are going to use is matplotlib, so we have to import it first!

import matplotlib.pyplot as plt

2. Prepare data

Next, we need to prepare some data. Let's take Xiaoming's test scores as an example to see the changing trends of his several subjects.

x = [2015, 2016, 2017, 2018, 2019, 2020] # 年份
y = [85, 89, 93, 90, 87, 92] # 分数

3. Draw a line chart

Now that we have our data, let's start drawing a line chart!

plt.plot(x, y)
plt.show()

Hey, look, these simple lines of code complete a line chart! Is it very simple?

4. Add tags and titles

Seeing this, we may need to add some tags and titles to make the image more beautiful. We can do it like this.

plt.plot(x, y)
plt.xlabel('年份')
plt.ylabel('分数')
plt.title('小明的考试成绩')
plt.show()

5. Custom styles

Not only that, we can also customize the color of the polyline, the shape of the line and the shape of the point, and so on. See the example below.

plt.plot(x, y, color='r', linestyle='--', marker='o')
plt.xlabel('年份')
plt.ylabel('分数')
plt.title('小明的考试成绩')
plt.show()

6. Complete code

import matplotlib.pyplot as plt

# 准备数据
x = [2015, 2016, 2017, 2018, 2019, 2020] # 年份
y = [85, 89, 93, 90, 87, 92] # 分数

# 绘制折线图
plt.plot(x, y)

# 加上标签和标题
plt.xlabel('年份')
plt.ylabel('分数')
plt.title('小明的考试成绩')

# 自定义样式
plt.plot(x, y, color='r', linestyle='--', marker='o')

# 展示图像
plt.show()

Xiao Ming's grades fluctuated a bit

 

Draw a scatterplot

Import matplotlib library and numpy library

import matplotlib.pyplot as plt
import numpy as np

prepare data

A scatterplot needs to provide x-axis and y-axis data, as well as the size and color of each point.

x = np.random.rand(100)
y = np.random.rand(100)
colors = np.random.rand(100)
area = (30 * np.random.rand(100))**2  # 点的大小

Draw a scatterplot

plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Scatter Plot')
plt.show()

 

draw pie chart

Import matplotlib library and numpy library

import matplotlib.pyplot as plt
import numpy as np

prepare data

Pie charts need to provide scale and labels for each part.

labels = ['A', 'B', 'C', 'D', 'E']
sizes = [15, 30, 45, 10, 5]

draw pie chart

plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
plt.title('Pie Chart')
plt.show()

Code for all graphics:

import matplotlib.pyplot as plt
import numpy as np

# 准备数据
x = [2015, 2016, 2017, 2018, 2019, 2020] # 年份
y = [85, 89, 93, 90, 87, 92] # 分数

# 绘制折线图
plt.plot(x, y)

# 加上标签和标题
plt.xlabel('年份')
plt.ylabel('分数')
plt.title('小明的考试成绩')

# 自定义样式
plt.plot(x, y, color='r', linestyle='--', marker='o')

# 展示图像
plt.show()

# 绘制柱状图

# 准备数据
x = ['A', 'B', 'C', 'D', 'E']
y = [10, 24, 36, 40, 59]

# 绘制柱状图
plt.bar(x, y)
plt.xlabel('Category')
plt.ylabel('Value')
plt.title('Bar Chart')
plt.show()

# 绘制散点图

# 准备数据
x = np.random.rand(100)
y = np.random.rand(100)
colors = np.random.rand(100)
area = (30 * np.random.rand(100))**2  # 点的大小

# 绘制散点图
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Scatter Plot')
plt.show()

# 绘制饼图

# 准备数据
labels = ['A', 'B', 'C', 'D', 'E']
sizes = [15, 30, 45, 10, 5]

# 绘制饼图
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
plt.title('Pie Chart')
plt.show()

 

Guess you like

Origin blog.csdn.net/alike_u/article/details/130034146