Matplotlib in action: How to draw beautiful charts with code?

1. What is Matplotlib?

First install Matplotlib

pip install matplotlib

matplotlib is specially used to develop 2D charts and 3D charts
to realize data visualization in a progressive and interactive way.

2. Why learn Matplotlib

Visualization is a key auxiliary tool in the entire data mining, which can clearly understand the data and thereby adjust our analysis methods.

  • Able to visualize data and present it more intuitively
  • Make data more objective and convincing

3. Implement a simple Matplotlib drawing - take the line chart as an example

1. matplotlib.pyplot module, this module contains a series of drawing functions similar to matlab

import matplotlib.pyplot as plt 

2. Graphic drawing process

  • Create canvas - plt.figure()
plt.figure(figsize=(), dpi=)
# figsize: 指定图的长度
# dpi: 图像的清晰度
# 返回fig对象
  • Plot the image - plt.plot(x, y)

  • Display image - plt.show()
    3. Line chart drawing and display

import matplotlib.pyplot as plt
# 创建画布
plt.figure(figsize=(10, 10), dpi=100)
# 绘制折线图
plt.plot([1, 2, 3, 4, 5, 6, 7], [17, 17, 18, 15, 11, 11, 13])
#显示图像
plt.show()

Insert image description here

4. Basic drawing functions - take line chart as an example

1. Improve the original line chart - add auxiliary functions to the graph.
Draw a line chart of temperature changes per minute in a city from 11 o'clock to 12 o'clock. The temperature range is 15 degrees -18 degrees.
1.1 Prepare data and draw initial line chart

import matplotlib.pyplot as plt
import random

# 画出温度变化图

# 准备x, y坐标的数据
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
# 创建画布
plt.figure(figsize=(20, 8), dpi= 80)

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

# 显示图像
plt.show()

Insert image description here
1.2 Customize x, y scale
plt.xticks(x, **kwargs)
x: The scale value to be displayed
plt.yticks(y, **kwargs)
y: The scale value to be displayed

import matplotlib.pyplot as plt
import random
from pylab import mpl

# 设置显示中文字体
mpl.rcParams["font.sans-serif"] = ["SimHei"]

# 设置正常显示符号
mpl.rcParams["axes.unicode_minus"] = False

# 准备数据
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
# 创建画布
plt.figure(figsize=(20, 8), dpi= 80)

# 绘制图像
plt.plot(x, y_shanghai)

# 添加x,y轴刻度
# 构造x, y轴刻度标签
x_ticks_label = ["11点{}分".format(i) for i in x]
y_ticks = range(40)

# 刻度显示
plt.xticks(x[::5], x_ticks_label[::5])
plt.yticks(y_ticks)

# 添加网格显示
plt.grid(True, linestyle = "--", alpha=0.5)

# 添加描述信息
plt.xlabel("时间")
plt.ylabel("温度")
plt.title("中午11点-12点某城市温度变化图", fontsize = 20)

# 图像保存
plt.savefig("./test.png")


# 显示图像
plt.show()

Insert image description here
2. Draw multiple images in one coordinate system

# 增加北京的温度数据
y_beijing = [random.uniform(1, 3) for i in x]
# 绘制折线图
plt.plot(x, y_shanghai)
# 使用多次plot可以画多个折线
plt.plot(x, y_beijing, color='r', linestyle='--')

Insert image description here
3. Application scenarios of line charts

  • Presents the number of daily active users of the company's products (different regions)
  • Presents the number of app downloads per day
  • Presents the changes in the number of user clicks over time after the new product features are launched.
  • Expansion: draw images of various mathematical functions
import numpy as np
import matplotlib.pyplot as plt

# 准备数据
x = np.linspace(-10, 10, 1000)
y = np.sin(x)

# 创建画布
plt.figure(figsize=(20, 8), dpi= 100)

# 绘制函数图像
plt.plot(x, y)

#添加网格显示
plt.grid()

# 显示图像
plt.show()

Insert image description here
5. Summary

  • Add x,y axis scale
  • plt.xticks()
  • plt.yticks()
  • Note: The first parameter passed in must be a number, not a string. If it is a string, it needs to be replaced.
  • Add grid display
    • plt.grid(linestyle=“-”, alpha =0.5)
  • Add description
    • plt.xlabel()
    • plt.ylabel()
    • plt.title()
  • image save
    • plt.savefig("path")
  • multiple plots
    • Add directly
  • Show legend
    • plt.legend(loc=“best”)
  • Display of multiple coordinate systems
    • plt.subplots(nrows = , ncols=)
  • Application of line chart
    • Used to observe changes in data
    • Can draw some mathematical function images

Guess you like

Origin blog.csdn.net/qq_30353203/article/details/131217625