文章目录 [隐藏]
https://matplotlib.org/2.1.2/contents.html
https://matplotlib.org/3.3.3/contents.html
安装和中文设置
安装
1 2 3 4 5 6 7 8 9 10 11 12 |
$ pip3 install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple $ pip3 show matplotlib Name: matplotlib Version: 3.3.3 Location: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages Requires: pyparsing, numpy, python-dateutil, pillow, kiwisolver, cycler # 同时安装了这些引用库 $ pip3 show numpy Name: numpy Version: 1.19.4 Location: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages Required-by: scipy, matplotlib, dtw-python # 被已安装的这些库引用 $ |
中文相关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# 图形标题和轴名称中文报错: # /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py:201: # RuntimeWarning: Glyph 28857 missing from current font. # font.set_text(s, 0, flags=flags) # 解决步骤: # 1.[不确定有没有用]将字体放到 /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf/ # 2.[不确定有没有用]编辑 /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc # 约x行 # 249 font.family : 打开 sans-serif # 257 font.sans-serif : 添加 SimHei # 400 axes.unicode_minus : 打开 False # 3.终端,获取缓存路径 $ python3 # >>> import matplotlib # >>> matplotlib.get_cachedir() # '/Users/moonmen/.matplotlib' # >>> exit() # 清除缓存 # $ rm -rf /Users/moonmen/.matplotlib # $ rm -rf ~/.cache/matplotlib # 4.代码中支持中文【不知道做了什么,不加也无妨】 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams["font.family"] = "sans-serif" plt.rcParams['axes.unicode_minus'] = False |
或者这样,在/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/matplotlib/pyplot.py中添加上面3项配置:
引子
1 2 3 4 5 6 |
import matplotlib.pyplot as plt arr = [2, 3, 5, 7, 6] plt.plot(arr) # 传入1个参数时默认作为y轴坐标。x轴自动从0开始累加对应 plt.show() |
1.线条图plot
1 2 3 4 |
x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 6] plt.plot(x, y) # 第1个参数为x轴坐标。第2个为y轴。两个轴点的数量必须相等 plt.show() |
2.标记的样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 6] plt.plot(x, y, "--") # 使用预定义的图形绘制 # 标记 描述 # '.' 点 # ',' 像素【看起来没东西】 # '+' 加号 # '*' 星号 # '|' 竖线 # '_' 下划线 # 'o' 圆圈 # 'D' 菱形 # 'd' 小菱形 # 's' 正方形 # 'p' 五边形、五角星 # 'H' 六边形 # 'h' 六边形 # '8' 八边形 # 'v' 一角朝下的三角形 # '<' 一角朝左的三角形 # '>' 一角朝右的三角形 # '^' 一角朝上的三角形 # '1' 朝下的小花 # '2' 朝上的小花 # '3' 朝左的小花 # '4' 朝右的小花 # 'x' X # 'None', '', ' ' 无图形显示 # ############# 以上仅标注点,下面4个是连线的形式 ########### # '-' 实线。减号。【默认方式】 # '--' 虚线。两个减号。 # '-.' 虚点线。减号和点。 # ':' 虚线。连线全是点。 plt.show() |
3.标记/线条的颜色
1 2 3 4 5 6 7 8 9 10 11 12 |
x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 6] plt.plot(x, y, "m-") # 使用plot预定义的颜色绘制线条 # b 蓝色 # g 绿色 # r 红色 # y 黄色 # c 青色 # k 黑色 # m 洋红色 # w 白色 plt.show() |
4.自定义标记/线条的颜色
1 2 3 4 5 6 7 |
x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 6] # plt.plot(x, y, color='g') # 使用预定义的颜色绘制线条 # plt.plot(x, y, color='red') # 使用HTML定义的颜色绘制线条 # plt.plot(x, y, color='#99382f') # 使用自定义的颜色绘制线条。16进制rgb plt.plot(x, y, color=(0.93, 0.43, 0.14)) # 使用自定义的颜色绘制线条。[0,1]的RGB元祖 plt.show() |
5.图形的图例和标记的大小
1 2 3 4 5 |
x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 6] plt.plot(x, y, "*", label='label对线条进行说明', linewidth=1.0) # label 图例说明,linewidth 线条粗细 plt.legend() # 不加此句,图例-label不显示 plt.show() |
6.多组标记(多根线条)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
x1 = [1, 2, 3, 4, 5] y1 = [2, 3, 5, 7, 6] x2 = [2, 2, 3, 4, 4] y2 = [1, 3, 4, 6, 7] # plt.plot(x1, y1, "b-", label='蓝色的线') # plt.plot(x1, y2, "r-", label='红色的线') # plt.legend() # plt.show() x1 = [1, 2, 3, 4, 5] y1 = [2, 3, 5, 7, 6] x2 = [2, 2, 3, 4, 4] y2 = [1, 3, 4, 6, 7] plt.plot(x1, y1, "b-", x1, y2, "r-") plt.legend(['蓝色的线', '红色的线']) plt.show() |
7.图形的背景色、区域着色、刻度线文字大小及颜色
1 2 3 4 5 6 7 8 9 |
x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 6] plt.plot(x, y) gcf= plt.gcf() # 图形的画布 gcf.set_facecolor('k') # 画布颜色 gca = plt.gca() # 图形 gca.set_facecolor("#dfee9a") # 图形的底色 gca.tick_params(labelsize=25, labelcolor="w") # 刻度条文字的字号和颜色 plt.show() |
区域着色:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import numpy as np import matplotlib.pyplot as plt x = np.array([0, 1, 2, 3, 4, 5, 6]) # <class 'numpy.ndarray'> y1 = np.array([3, 2.5, 2, 1, 1.5, 2, 3]) y2 = np.array([0, 1, 0.5, 0, -1, -0.5, 0]) plt.figure() plt.plot(x, y1, lw=3, label='y1') plt.plot(x, y2, lw=3, label='y2') plt.fill_between( # 水平填充 x, # x方向的一段位置 y1, # 顶部的边缘位置 y2, # 底部边缘位置。默认0,坐标系y=0的位置,而不是图形的最底刻度。 where=(x >= 3) & (x < 6), # x轴填充条件。此处的'x'列表须为numpy的ndarry color='blue', alpha=0.5, # label='y2-y1') y = np.array([-1, 0.5, 1, 1.4, 2.7, 3]) x1 = np.array([0, 1.3, 1.6, 1.1, 1.5, 2.2]) x2 = np.array([1.6, 2.1, 2.5, 3.4, 2.3, 3]) plt.plot(x1, y, lw=2, label='y3') plt.plot(x2, y, lw=2, label='y4') plt.fill_betweenx( # 垂直填充 y, # y方向的一段位置 x1, # 顶部的边缘位置 x2, # 底部边缘位置。默认0,坐标系y=0的位置,而不是图形的最底刻度。 where=(y >= 0.5) & (y < 2), # x轴填充条件 color='red', alpha=0.3, edgecolor='g', lw=5, # label='x2-x1') plt.fill_betweenx( # 垂直填充 y, # y方向的一段位置 x1, # 顶部的边缘位置 x2, # 底部边缘位置。默认0,坐标系y=0的位置,而不是图形的最底刻度。 where=(y > 2.5), # x轴填充条件 color='red', alpha=0.3, edgecolor='none', # label='x2-x1') plt.legend(loc='upper left', frameon='True') plt.show() |
8.刻度条的间隔和不同文字颜色
1 2 3 4 5 6 7 8 |
x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 6] from matplotlib.pyplot import MultipleLocator plt.plot(x, y, "*", markersize=20) # , markersize设置全部标记点图形大小像素 plt.gca().get_xticklabels()[3].set_color("red") # 图形的某个刻度标注颜色 plt.gca().get_yticklabels()[4].set_color("red") plt.gca().xaxis.set_major_locator(MultipleLocator(1)) # 刻度间隔为1。影响get_yticklabels的数量 plt.show() |
9.点图scatter-标记大小和标记的不同颜色大小
1 2 3 4 5 6 7 8 9 10 |
x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 6] s = [5, 20, 35, 50, 65] c = ['red', 'blue', 'm', '#99008f', '#ae998f'] plt.figure(figsize=(4, 3.5)) # 图片的宽高像素,单位是100。即 w = 4*100,h=3.5*100 plt.scatter(x, y, s=s, c=c) # scatter只绘制点,而不是默认的连线。 # s 为每个点分别设置大小,c 为每个点分别设置颜色 plt.xlim(0, 6) # 刻度数字区间 plt.ylim(0, 9) plt.show() |
10.图形的标题和轴名称
1 2 3 4 5 6 7 |
x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 6] plt.scatter(x, y) # 只绘制点,而不是默认的连线 plt.title('不同大小的标注点') # 绘图标题 plt.xlabel('x轴') # x轴显示的名称 plt.ylabel('y轴') plt.show() |
- end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/2937.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设