赞
踩
1.1 subplot(m,n,k) :创建一个m*n的子图,接下来的图样绘制在第k块中
1.2 plot(x,y,color='blue',linewidth=2.5,marker = '^',linestyle='-',label = ' '):绘制曲线
color:线条颜色:
颜色字符 | 说明 | 颜色字符 | 说明 |
'b' | 蓝色 | ‘m’ | 洋红色 |
'g' | 绿色 | 'y' | 黄色 |
'r' | 红色 | 'k' | 黑色 |
'c' | 青绿色 | 'w' | 白色 |
'#008000' | RGB某颜色 | '0.8' | 灰色值字符串 |
marker:标记风格
标记字符 | 说明 |
'.' | 点标记 |
'o' | 实心圆标记 |
'v' | 倒三角标记 |
'^' | 上三角标记 |
'>' | 右三角标记 |
'<' | 左三角标记 |
'*' | 星形标记 |
‘+’ | 十字标记 |
‘|’ | 垂直线标记 |
‘D’ | 菱形标记 |
linestyle:线条样式
风格字符 | 说明 |
'-' | 实线 |
'--' | 破折线 |
‘-.’ | 点划线 |
':' | 虚线 |
label:图例名称
1.3 xlim(-2.0,2.0) :设置横轴的上下限
1.4 xticks(np.linspace(-4, 4, 9, endpoint=True), fontproperties='Times New Roman', size=20)
yticks(np.linspace(-1, 1, 5, endpoint=True)):设置横轴和纵轴记号(范围,字体,字号)
1.5 xlabel('X axis',font) ylabel('Y axis',font): 横轴标签,纵轴标签
#设置横纵坐标的名称以及对应字体格式 font = {'family' : 'Times New Roman', 'weight' : 'normal', 'size' : 20, }
1.6 title('Demo Figure',font):设置图像标题
1.7 show():在屏幕上显示
1.8 legend(loc = 'upper left'):显示图例并固定位置
实例:
- from pylab import *
- import numpy as np
- import matplotlib.pylab as plt
-
- # 创建一个8*6点(point)的图,并设置分辨率为80
- figure(figsize=(8, 6), dpi=80)
-
- # 创建一个新的1*1的子图,接下来的图样绘制在其中的第一块中
- subplot(2, 2, 1)
-
- # 得到坐标点(x,y)坐标
- 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=2.5, marker = '^',linestyle='-',label = 'cosine')
-
- # 绘制正弦曲线,使用绿色的、连续的、宽度为1的线条
- plot(X, S, color='green', linewidth=2.0, linestyle='-',label='sine')
-
- legend(loc = 'upper left')
- # 设置横轴的上下限
- xlim(-4.0, 4.0)
-
- # 设置横轴记号
- xticks(np.linspace(-4, 4, 9, endpoint=True), fontproperties='Times New Roman', size=20)
-
- # 设置纵轴记号
- yticks(np.linspace(-1, 1, 5, endpoint=True))
-
- #设置横纵坐标的名称以及对应字体格式
- font = {'family' : 'Times New Roman',
- 'weight' : 'normal',
- 'size' : 20,
- }
-
- # 设置横轴标签
- plt.xlabel('X axis', font)
-
- # 设置纵轴标签
- plt.ylabel('Y axis', font)
-
- # 设置图像标题
- plt.title('Demo Figure', font)
-
- # 以分辨率72来保存图片
- savefig('demo.png', dpi=72)
-
-
- subplot(2,2,2)
- plot(X, -C, color='m', linewidth=2.5, marker = '^',linestyle='-.')
-
- # 绘制正弦曲线,使用绿色的、连续的、宽度为1的线条
- plot(X, -S, color='r', linewidth=2.0, linestyle='--')
-
- # 在屏幕上显示
- show()
效果图:
若横轴纵轴标记为:
- xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
- [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
-
- yticks([-1, 0, +1],
- [r'$-1$', r'$0$', r'$+1$'])
效果图:
plt.fill_between(x, y1, y2, where, color, alpha)
参数:
x: x轴坐标值,为一个list
y1: 第一条曲线对应的函数值,为x对应的函数值list
y2: 第二条曲线对应的函数值,为x对应的函数值list
where: 条件表达式,用于判断某个区间内是否进行填充,如果判断为True,则进行填充,否则不填充
color: 填充区域的颜色
alpha: 填充区域的透明度,1表示不透明,0表示完全透明
- import numpy as np
- import matplotlib.pyplot as plt
-
- n = 256
- X = np.linspace(-np.pi,np.pi,n,endpoint=True)
- Y = np.cos(2*X)
-
- plt.axes([0.025,0.025,0.95,0.95])
-
- plt.plot (X, Y+1, color='g', alpha=1.00)
- plt.fill_between(X, 1, Y+1, color='g', alpha=.25)
-
- plt.plot (X, Y-1, color='m', alpha=1.00)
- plt.fill_between(X, -1, Y-1, (Y-1) > -1, color='m', alpha=.25)
- plt.fill_between(X, -1, Y-1, (Y-1) < -1, color='b', alpha=.25)
-
- plt.xlim(-np.pi,np.pi), plt.xticks([])
- plt.ylim(-2.5,2.5), plt.yticks([])
-
- plt.show()
效果图:
- import numpy as np
- import matplotlib.pyplot as plt
-
- n = 1024
- X = np.random.normal(0,1,n)
- Y = np.random.normal(0,1,n)
- T = np.sin(Y) # T中包含了数据点的颜色到当前colormap的映射值
- # print(T.shape)
- plt.axes([0.025,0.025,0.95,0.95])
- plt.scatter(X,Y, s=75, c=T, alpha=.5)
-
- plt.xlim(-1.5,1.5), plt.xticks([])
- plt.ylim(-1.5,1.5), plt.yticks([])
- plt.show()
-
效果图:
bar()
参数:
- from pylab import *
- n= 10
- X = np.arange(5)
- Y1 = np.array([2,3,4,5,6])
- Y2 = np.array([-3,-6,-9,-1,-2])
- bar(X,Y1,facecolor='#fd9999', edgecolor='white')
- bar(X,Y2,facecolor='#9dd999', edgecolor='white')
- for x,y in zip(X,Y1):
- text(x, y+0.05, '%.2f' % y, ha='center', va= 'bottom')
- for x, y in zip(X, Y2):
- text(x, y-0.85, '%.2f'% y, ha='center', va='bottom')
-
- ylim(-10,10)
- show()
两种直方图对比:
- import matplotlib.pyplot as plt
- import matplotlib
-
- # 设置中文字体和负号正常显示
- matplotlib.rcParams['font.sans-serif'] = ['SimHei']
- matplotlib.rcParams['axes.unicode_minus'] = False
-
- label_list = ['2020', '2021', '2022', '2023'] # 横坐标刻度显示值
- num_list1 = [10, 30, 15, 35] # 纵坐标值1
- num_list2 = [10, 20, 40, 20] # 纵坐标值2
- x = range(len(num_list1))
-
- # 绘制条形图
- rects1 = plt.bar(x, height=num_list1, width=0.4, alpha=0.5, color='#2188fe', label='部门一')
- rects2 = plt.bar([i+0.4 for i in x], height=num_list2, width=0.4, color='#fe2188', label='部门二')
-
- # 设置y轴属性
- plt.ylim(0, 50)
- plt.ylabel('数量')
-
- # 设置x轴属性
- plt.xticks([index+0.2 for index in x], label_list)
- plt.xlabel("年份")
- plt.title('某某学校')
- plt.legend()
-
- # 显示文本
- for rect in rects1:
- height = rect.get_height()
- plt.text(rect.get_x() + rect.get_width() / 2, height + 1, str(height), ha='center', va='bottom')
- for rect in rects2:
- height = rect.get_height()
- plt.text(rect.get_x() + rect.get_width() / 2, height + 1, str(height), ha='center', va='bottom')
-
- plt.show()
- from pylab import *
-
- def f(x,y):
- return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)
-
- n = 256
- x = np.linspace(-3,3,n)
- y = np.linspace(-3,3,n)
- X,Y = np.meshgrid(x,y)
- #涂色区域
- plt.contourf(X, Y, f(X,Y), 12, alpha=.75, cmap='jet')
- #等高线
- C = plt.contour(X, Y, f(X,Y), 12, colors='black')
- show()
- from pylab import *
- x = [5, 10, 15, 20, 25, 30, 35, 40]
- y = [17, 24, 29, 36, 38, 47, 59, 80]
-
- plt.plot(x, y, 'm*-', markersize=20)
- show()
- import matplotlib.pyplot as plt
- import matplotlib.colors as col
- import matplotlib.cm as cm
- import numpy as np
-
- points = np.arange(-5, 5, 0.01)
- # print(points)
- xs, ys = np.meshgrid(points, points)
-
- z = np.sqrt(xs**2 + ys**2)
- # print(z)
- # 自定义colormap
- start_color = 'r'
- end_color = 'w'
-
- cmap_1 = col.LinearSegmentedColormap.from_list('cmap1', [start_color, end_color])
-
-
- plt.imshow(z, cmap=cmap_1, alpha=0.3)
-
- plt.show()
本文参考:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。