目录
- 一、基本图的绘制
- 1.1直线的绘制
- 1.2折线的绘制
- 1.3散点图的绘制
- 1.4绘制不同颜色的散点图
- 1.5绘制不同颜色的线
- 1.6柱状图
- 1.7饼状图
- 1.8直方图
- 1.9三维图
- 1.10等高线图
- 二、样式
- 三、函数
一、基本图的绘制
1.1直线的绘制
import matplotlib.pyplot as plt
plt.plot([0, 4], [3, 5])
plt.ylabel("y")
plt.xlabel("X")
plt.savefig("F:/Z/Python/project/project2/matl/直线.jpg")
plt.show()
运行效果

1.2折线的绘制
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
X = [1, 5, 7, 9, 5]
Y = [9, 6, 7, 3, 6]
Z = [5, 6, 9, 3, 4]
figure = plt.figure()
ax = Axes3D(figure)
ax.plot_trisurf(X, Y, Z)
plt.show()
运行效果

1.3散点图的绘制
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
sin_y = np.sin(x)
plt.scatter(x, sin_y)
plt.show()
"""
注意:由上可知,使用plot绘制和使用scatter绘制出来的图形是一样的
但是,如果画一堆点,点的形式没有差别就建议使用plot,因为plot的绘图速度优于scatter
如果点的形式有差别(点的大小和颜色不同),就必须使用scatter
"""
运行效果

1.4绘制不同颜色的散点图
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)
x = np.random.rand(100)
y = np.random.rand(100)
size = np.random.rand(100)*1000
color = np.random.rand(100)
plt.scatter(x, y, s=[size], c=color,alpha=0.9)
plt.show()
"""
注意:
大小、颜色的个数和x,y的个数应该保持一致 要不然会出现类似下面的报错
'c' argument has 10 elements, which is inconsistent with 'x' and 'y' with size 100.
s must be a scalar, or the same size as x and y
"""
运行效果

1.5绘制不同颜色的线
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
plt.plot(x, x + 0, "--g", label="--g")
plt.plot(x, x + 1, ",b", label=",b")
plt.plot(x, x + 2, "or", label="or")
plt.plot(x, x + 3, ":c", label=":c")
plt.plot(x, x + 4, "vy", label="vy")
plt.plot(x, x + 5, "--m", label="--m")
plt.legend(loc="lower right", fancybox=True, framealpha=1, shadow=True, borderpad=1)
plt.show()

1.6柱状图
import matplotlib.pyplot as plt
x = [2016, 2017, 2018, 2019, 2020]
x_label = ["2016年", "2017年", "2018年", "2019年", "2020年"]
y = [-500000000000, 60000000000, 70000000000, 80000000000, 900000000000]
plt.rcParams['axes.unicode_minus'] = False
plt.bar(x, y, width=0.8)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.xticks(x, x_label)
plt.ylabel("利润")
plt.xlabel("年份")
plt.title("每年对应的利润图")
plt.show()
运行效果

1.7饼状图
import matplotlib.pyplot as plt
sheep = 666
dogs = 555
ducks = 888
sheep_percent = sheep/(sheep+ducks+dogs)
dogs_percent = dogs/(sheep+ducks+dogs)
ducks_percent = ducks/(sheep+ducks+dogs)
labels =["山羊","狗","鸭子"]
colors = ["blue","green","red"]
plt.rcParams['font.sans-serif'] = ['SimHei']
paches,texts,autotexts=plt.pie([sheep_percent,dogs_percent,ducks_percent],labels=labels,colors=colors,explode=(0,0,0.05),autopct="%0.1f%%")
for text in autotexts:
text.set_color("white")
for text in texts+autotexts:
text.set_fontsize(15)
plt.show()
运行效果

1.8直方图
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(1000)
plt.hist(x, bins=100)
plt.show()
运行效果

1.9三维图
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
X = [1, 5, 7, 9, 5]
Y = [9, 6, 7, 3, 6]
Z = [5, 6, 9, 3, 4]
figure = plt.figure()
ax = Axes3D(figure)
ax.plot_trisurf(X, Y, Z)
plt.show()
运行效果

注:鼠标点住图,可使其旋转
如果不能的解决办法:
- 在pycharm中点击“File—>setting”,打开设置窗口。
- 找到最后一个工具选项tools.
- 找到“Python Scientific”,去除Show plots in toolwindow框中的勾号
1.10等高线图
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)
X, Y = np.meshgrid(x, y)
Z = np.sqrt(X ** 2 + Y ** 2)
plt.contourf(X, Y, Z)
plt.show()
运行效果

二、样式
2.1基本样式
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5, 6]
y = [1, 2, 4, 8, 16, 32, 64]
plt.ylabel("y轴")
plt.xlabel("X轴")
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.plot(x, y)
plt.title("标题")
plt.savefig("F:/Z/Python/project/project2/matl/base_pattern.jpg")
plt.show()
运行效果

2.2画布分区的使用
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
sin_y = np.sin(x)
plt.subplot(2,2,1)
plt.xlim(-5,15)
plt.ylim(-2,1.5)
plt.plot(x,sin_y)
plt.subplot(2,2,2)
plt.plot(x,np.cos(x))
plt.show()
运行效果

三、函数
3.1简单的一元二次方程
import matplotlib.pyplot as plt
x = range(-100, 100)
y = [i ** 2 for i in x]
plt.plot(x, y)
plt.savefig("onetwofangcheng.jpg")
plt.show()
运行结果

3.2三角函数
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
sin_y = np.sin(x)
plt.plot(x, sin_y)
cos_y = np.cos(x)
plt.plot(x, cos_y)
plt.savefig("sincos.jpg")
plt.show()
运行效果

注意:cos类似