python_Matplotlib

Matplotlib

Matplotlib是Python中用于机器学习最流行和最古老的绘图库之一。在机器学习中,它有助于通过不同的可视化来理解数据,绘制最终的结果。

basic plot
import numpy as np
import matplotlib.pyplot as plt
# 需要X轴和Y轴
x = np.arange(-100, 101, 1)
print(x)
y = 0.5 * x ** 2 + 2*x
plt.plot(x, y)
plt.show()
# 也可以在一张图里面画多个函数
y1 = 0.5 * x ** 2 + 2*x
y2 = np.sin(x) * 2000
y3 = np.log(x) * 1000

plt.plot(x, y1)
plt.plot(x, y2)
plt.show()

修改样式

1 x = np.arange(-100, 101, 1)
2 print(x)
3 y = 0.5 * x ** 2 + 2*x
4 plt.plot(x, y, 'r') # 'b' 'r--' 'go' 'y-' 'y^'
5 plt.show()

subplots

1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 x = np.arange(0, 100, 1)
5 y1 = np.sin(x)
6 y2 = x**2 + 2*x
7
8 ax1 = plt.subplot(211) # rows,columns,index221
9 ax2 = plt.subplot(212) # 222
10
11 ax1.plot(x,y1)
12 ax2.plot(x,y2)
13 plt.show()

good layout

1 plt.tight_layout()

multiple windows

1 plt.figure(1)
2 ax1 = plt.subplot(211)
3 ax1.plot(x,y1,'g')
4 ax2 = plt.subplot(212)
5 ax2.plot(x,y2,'r')
6
7 plt.figure(2)
8 plt.plot(x, y1)
9
10 y3 = np.log(x)
11 plt.figure(3)
12 plt.plot(x,y3)
13
14 plt.show()

更多主题

1 import numpy as np
2 import matplotlib.pyplot as plt
3 from matplotlib import style
4
5 #style.use('ggplot') # 不同的主题
6 #style.use('fivethirtyeight')
7 #style.use('dark_background')
8 #style.use('grayscale')
9
10 x = np.arange(0, 200, 0.2)
11 y = np.sin(x)
12
13 plt.plot(x,y)
14 plt.show()

添加网格、标题、x轴y轴标题

1 plt.grid(True)
2
3 plt.title('sine wave')
4 plt.suptitle('bigger title')
5
6 plt.xlabel("number of students")
7 plt.ylabel("height of students")

添加图例

1 y1 = np.sin(x)
2 y2 = np.cos(x)
3
4 plt.plot(x, y1, label='sine')
5 plt.plot(x, y2, label='cosine')
6
7 plt.legend('loc='upper left) # lower right
8 plt.show()

柱状图

1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 python = (85, 67, 23, 98)
5 java = (50, 67, 89, 14)
6 networking = (60, 20 ,56, 22)
7 machine_learning = (88, 23, 40, 87)
8
9 people = ['Bob', 'Anna', 'John', 'Mark']
10 index = np.arange(4)
11
12 plt.bar(index, python, width=0.2,label='Python')
13 plt.bar(index + 0.2, java, width=0.2,label='Java')
14 plt.bar(index + 0.4, networking, width=0.2,label='Networking')
15 plt.bar(index + 0.6, machine_learning,width=0.2, label='Machine Learning')
16
17 plt.title('IT Skill Scores')
18 plt.xlabel('person')
19 plt.ylabel('scores')
20
21 plt.xticks(index+0.2, people)  # x轴上面坐标数字替换
22 plt.legend(loc='upper right')
23 plt.ylim(0, 120) # 控制y轴展示范围,让图例尽量不覆盖在图上
24
25 plt.show()

饼图

1 import matplotlib.pyplot as plt
2
3 nationalities = ['American', 'German','French', 'Other']
4 students = [60, 23, 21, 34]
5
6 # 排序
7 # pairs = zip(students, nationalities)
8 # pairs = sorted(list(pairs))
9 # students, nationalities = zip(*pairs)
10
11 plt.pie(students)
12 plt.pie(students, labels=nationalities)
13 plt.pie(students, labels=nationalities,autopct='%.2f%%')
14 plt.pie(students, labels=nationalities,autopct='%.2f%%', counterclock=False)
15 plt.pie(students, labels=nationalities,autopct='%.2f%%', counterclock=False,startangle=90)
16 plt.pie(students, labels=nationalities,autopct='%.2f%%', shadow=True)
17
18 explode = [0,0,0.1,0]  # 重点突出哪部分
19 plt.pie(students, labels=nationalities,autopct='%.2f%%', explode=explode)
20
21 plt.title("Student Nationalities")
22 plt.show()

频率直方图 histogram

1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 mu, sigma = 172, 8 # 身高cm
5 # 随机出来一组数据,让其服从正太分布
6 x = mu + sigma*np.random.randn(10000)
7
8 plt.hist(x, 100, facecolor='bule') # 可视化x轴上100个点
9 plt.hist(x, 100, facecolor='bule',density=True)
10
11 plt.xlabel('Heights')
12 plt.ylabel('Percentage of students')
13 plt.title('Heights of students')
14 plt.grid(True)
15 plt.rcParams["font.sans-serif"]=["SimHei"] #设置字体
16 plt.text(145, 0.04, "μ=172, σ=8")
17
18 plt.show()

散点图

1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 x = np.random.rand(50)
5 y = np.random.rand(50)
6
7 #x = [10,20,30,40,20]
8 #y = [5,7,3,2,6]
9
10 plt.scatter(x,y)
11 plt.scatter(x,y,c='red',marker='^', s=100,cmap='Spectral')
12
13 plt.show()

3D图

1 import numpy as np
2 import matplotlib.pyplot as plt
3 from mpl_toolkits import mplot3d
4
5 ax = plt.axes(projection='3d')
6
7 # 一个轴作为输入,另外两个轴是根据输入计算出来的
8 #z = np.linspace(0, 30, 100)
9 #x = np.sin(z)
10 #y = np.cos(z)
11 # ax.plot3D(x, y, z)
12
13 # 两个轴作为输入,第三个轴是根据前俩个轴计算出来的
14 def z_function(x,y):
15    return np.sin(np.sqrt(x**2 + y**2))
16
17 x = np.linspace(-5,5,100)
18 y = np.linspace(-5,5,100)
19
20 X, Y = np.meshgrid(x, y)
21 Z = z_function(X,Y)
22
23 ax.plot_surface(X,Y,Z)
24
25 plt.show()

np.meshgrid() 函数
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1687F

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值