matplotlib [Python] common library of learning

matplotlib

Role: to generate publication-quality graphics level

Chinese document: https: //www.matplotlib.org.cn/

The official document: https: //matplotlib.org/users/index.html


FIG generate the pie

import matplotlib.pyplot as plt

# data to plot
labels = ('Python', 'C++', 'Ruby', 'Java',)
sizes = (215, 130, 245, 210,)

colors = ('gold', 'yellowgreen', 'lightcoral', 'lightskyblue',)

plt.pie(
    sizes,
    labels=labels,
    colors=colors,
    autopct='%1.1f%%',
)

plt.title("Programming Languages")

plt.show()

# 将图表保存成文件
plt.savefig('foo.png')
plt.savefig('foo.pdf')

Here Insert Picture Description

Generating a cosine curve

# 导入 matplotlib 的所有内容(nympy 可以用 np 这个名字来使用)
from pylab import *

# 创建一个 8 * 6 点(point)的图,并设置分辨率为 80
figure(figsize=(8,6), dpi=80)

# 创建一个新的 1 * 1 的子图,接下来的图样绘制在其中的第 1 块(也是唯一的一块)
subplot(1,1,1)

X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X)

# 绘制余弦曲线,使用蓝色的、连续的、宽度为 1 (像素)的线条
plot(X, C, color="blue", linewidth=1.0, linestyle="-")

# 绘制正弦曲线,使用绿色的、连续的、宽度为 1 (像素)的线条
plot(X, S, color="green", linewidth=1.0, linestyle="-")

# 设置横轴的上下限
xlim(-4.0,4.0)

# 设置横轴记号
xticks(np.linspace(-4,4,9,endpoint=True))

# 设置纵轴的上下限
ylim(-1.0,1.0)

# 设置纵轴记号
yticks(np.linspace(-1,1,5,endpoint=True))

# 以分辨率 72 来保存图片
# savefig("exercice_2.png",dpi=72)

# 在屏幕上显示
show()

Here Insert Picture Description


Here Insert Picture Description

Published 149 original articles · won praise 2572 · Views 570,000 +

Guess you like

Origin blog.csdn.net/qq_43901693/article/details/104916659