Python——Numpy、Pandas、Matplotlib
Matplotlib<day01>
今日思维导图整理
今日思维导图整理
绘制图如下
-
折线图
-
# -*- coding:utf-8 -*- # ====#====#====#====#====#====#====#====#==== # @Time : 2020/4/4 11:30 # @Author : Alex_Dong # @Email : 1220274707@qq.com # @HomePage:https://www.cnblogs.com/xied/ # @File : 演示文件.py # @Software: PyCharm # ====#====#====#====#====#====#====#====#==== # import matplotlib # 绘制一条直线 import matplotlib.pyplot as plt x = range(2, 27, 2) y = [15, 13, 14, 5, 17, 20, 25, 26, 27, 25, 22, 18, 15] # plt.plot(x,y) # plt.show() # plt.title('这是折线图的标题') # 设置大小,高宽及像素dpi # fig=plt.figure(figsize=(20,8),dpi=100) # figsize接收一个元组,表示图片的高和宽,单位是英寸 # DPI分辨率,代表了每一英寸有多少像素,默认80 # plt.plot(x,y) # plt.show() # 保存图片 # fig.savefig('./pic/test_png.png') # 可以保存为SVG这种矢量图格式,放大以后不会有锯齿 # fig.savefig("./pic/test_svg.svg") # X轴和Y轴的调整和设置中文 # plt.plot(x,y) # x轴的刻(度) # plt.xticks(x) # plt.yticks(y) # plt.show() # import random # import matplotlib as mpl # # 设置中文格式'仿宋' # mpl.rcParams['font.sans-serif'] = ['FangSong'] # mpl.rcParams['font.size'] = 16 # # y = [random.randint(15, 35) for i in range(10)] # x = list(range(10)) # fig = plt.figure(figsize=(20, 8)) # plt.plot(x, y) # xlable = ['10点{}分'.format(i) for i in range(60)] # xlable += ['11点{}分'.format(i) for i in range(60)] # # #添加描述 # plt.xlabel('时间',color='red',fontdict={'fontsize':20}) # plt.ylabel('温度') # #设置标题 # plt.title('某日10点到12点间的温度变化情况') # #添加网格 # plt.grid(alpha=0.1) # # plt.xticks(x[::1], xlable[::12], rotation=45) # plt.yticks(y) # plt.show() # fig.savefig('./pic/fansong_text_png.png') import numpy as np import matplotlib.pyplot as plt plt.figure(1) # 创建图表1 plt.figure(2) # 创建图表2 ax1 = plt.subplot(211) # 在图表2中创建子图1 ax2 = plt.subplot(212) # 在图表2中创建子图2 x = np.linspace(0, 3, 100) for i in range(5): plt.figure(1) # ❶ # 选择图表1 plt.plot(x, np.exp(i * x / 3)) plt.sca(ax1) # ❷ # 选择图表2的子图1 plt.plot(x, np.sin(i * x)) plt.sca(ax2) # 选择图表2的子图2 plt.plot(x, np.cos(i * x)) plt.show()
View Code
-
散点图
-
# -*- coding:utf-8 -*- # ====#====#====#====#====#====#====#====#==== # @Time : 2020/4/4 14:05 # @Author : Alex_Dong # @Email : 1220274707@qq.com # @HomePage:https://www.cnblogs.com/xied/ # @File : 散点图.py # @Software: PyCharm # ====#====#====#====#====#====#====#====#==== import matplotlib.pyplot as plt import matplotlib as mpl # 设置中文说明'仿宋'字体 mpl.rcParams['font.sans-serif'] = ['FangSong'] # 用来正常显示中文标签 mpl.rcParams['font.size'] = 16 x_3 = list(range(1, 32)) y_3 = [10, 16, 17, 14, 12, 10, 12, 6, 6, 7, 8, 9, 12, 15, 15, 17, 18, 21, 16, 16, 20, 13, 15, 15, 15, 18, 20, 22, 22, 22, 24] x_10 = [i + 50 for i in x_3] y_10 = [26, 26, 28, 19, 21, 17, 16, 19, 18, 20, 20, 19, 22, 23, 17, 20, 21, 20, 22, 15, 11, 15, 5, 13, 17, 10, 11, 13, 12, 13, 6] fig = plt.figure(figsize=(15, 8)) plt.scatter(x_3, y_3, label='三月份') plt.scatter(x_10, y_10, label='十月份') # 设置轴刻度 # 集合 y = set(y_3 + y_10) min_y = min(y) max_y = max(y) plt.yticks(range(min_y, max_y + 1)) # x轴 x = x_3 + x_10 x_lables = ['3月{}日'.format(i) for i in range(1, 32)] + ['10月{}日'.format(i) for i in range(1, 32)] plt.xticks(x[::2], x_lables[::2], rotation=45) plt.xlabel('日期') plt.ylabel('温度(C)') plt.title('武汉市2019年3月份到10月份的气温变化趋势图') #添加网格 plt.grid(alpha=0.3) # 设置图例 plt.legend() plt.show() fig.savefig('./pic/散点图.png')
-
条形图
-
# -*- coding:utf-8 -*- # ====#====#====#====#====#====#====#====#==== # @Time : 2020/4/4 19:52 # @Author : Alex_Dong # @Email : 1220274707@qq.com # @HomePage:https://www.cnblogs.com/xied/ # @File : 条形图.py # @Software: PyCharm # ====#====#====#====#====#====#====#====#==== import matplotlib.pyplot as plt import matplotlib as mpl # 设置中文 mpl.rcParams['font.sans-serif'] = ['FangSong'] # 用来正常显示中文标签 mpl.rcParams['font.size'] = 16 # 设置字体大小 # 构建坐标 movies = x = ['哪吒之魔童降世', '流浪地球', '复仇者联盟4:终局之战', '疯狂的外星人', '飞驰人生', '烈火英雄', '速度与激情:特别行动', '蜘蛛侠:英雄远征', '扫毒2天地对决', '大黄蜂', '惊奇队长', '比悲伤更悲伤的故事', '哥斯拉2:怪兽之王', '阿丽塔:战斗天使', '银河补习班', '狮子王', '反贪风暴4 ', '熊出没·原始时代', '使徒行者2:谍影行动', '大侦探皮卡丘'] y = [49.04, 46.18, 42.05, 21.83, 17.03, 16.74, 14.16, 14.01, 12.85, 11.38, 10.25, 9.46, 9.27, 8.88, 8.64, 8.23, 7.88, 7.09, 6.92, 6.34] x = range(len(movies)) # 设置容器 fig = plt.figure(figsize=(20, 8), dpi=100) # 绘图 plt.barh(x, y, ) # 设置轴刻度 plt.xticks(x, rotation=90) plt.xlabel('电影名称') plt.ylabel('电影票房') plt.title('2019年电影的票房') # 设置网格 plt.grid(alpha=0.3) # 设置图例 # plt.legend() # 显示 plt.show() # 储存 fig.savefig('./pic/条形图.png')
-
多合一
-
# -*- coding:utf-8 -*- # ====#====#====#====#====#====#====#====#==== # @Time : 2020/4/4 20:47 # @Author : Alex_Dong # @Email : 1220274707@qq.com # @HomePage:https://www.cnblogs.com/xied/ # @File : 绘制多图表.py # @Software: PyCharm # ====#====#====#====#====#====#====#====#==== import numpy as np import matplotlib.pyplot as plt fig = plt.figure(2,figsize=(20, 8), dpi=100) # fig = plt.figure(5,figsize=(20, 8), dpi=100) # fig_1 = plt.figure(1) # 创建图表1 # fig_2 = plt.figure(2) # 创建图表2 # ax1 = plt.subplot(211) # 在图表2中创建子图1 # ax2 = plt.subplot(212) # 在图表2中创建子图2 ax1 = fig.add_subplot(211) ax2 = fig.add_subplot(212) x = np.linspace(0, 3, 100) for i in range(5): plt.figure(1) # ❶ # 选择图表1 plt.plot(x, np.exp(i * x / 3)) plt.sca(ax1) # ❷ # 选择图表2的子图1 plt.plot(x, np.sin(i * x)) plt.sca(ax2) # 选择图表2的子图2 plt.plot(x, np.cos(i * x)) # plt.show() fig.show() # fig.savefig('./pic/绘制多图表.png') # fig.savefig("./pic/test.pdf")
# -*- coding:utf-8 -*- # ====#====#====#====#====#====#====#====#==== # @Time : 2020/4/4 13:59 # @Author : Alex_Dong # @Email : 1220274707@qq.com # @HomePage:https://www.cnblogs.com/xied/ # @File : 一个图中画多个图.py # @Software: PyCharm # ====#====#====#====#====#====#====#====#==== import matplotlib.pyplot as plt import matplotlib as mpl # 设置中文 mpl.rcParams['font.sans-serif'] = ['FangSong'] # 用来正常显示中文标签 mpl.rcParams['font.size'] = 16 # 设置字体大小 # 构建坐标 # x轴表示年龄,Y轴表示个数 x = range(11, 31) y_self = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1] y_d = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1] # 创建容器 fig = plt.figure(figsize=(20, 8)) # 画图 plt.plot(x, y_self, label='自己', color='black', linestyle='-.') plt.plot(x, y_d, label='同桌') # 设置刻度 x_lables = ['{}岁'.format(i) for i in x] plt.xticks(x, x_lables) # 设置标签 plt.xlabel('年龄') plt.ylabel('个数') plt.title('我和同桌历年的个数对比') # 设置了图例一定要加上这句话 plt.legend() plt.grid(alpha=0.3) # 标记点 plt.annotate('最高点', xy=(23, 6), xytext=(24, 6), arrowprops={'arrowstyle': '<->'}) plt.show() fig.savefig('./pic/一张图画多个.png')
# -*- coding:utf-8 -*- # ====#====#====#====#====#====#====#====#==== # @Time : 2020/4/4 21:29 # @Author : Alex_Dong # @Email : 1220274707@qq.com # @HomePage:https://www.cnblogs.com/xied/ # @File : pylib.py # @Software: PyCharm # ====#====#====#====#====#====#====#====#==== import numpy as np import pylab as pl x = [1, 2, 3, 4, 5] # Make an array of x values y = [1, 4, 9, 16, 25] # Make an array of y values for each x value # pl.plot(x, y) # use pylab to plot x and y 直接画是线段 # pl.plot(x, y, 'o') #变成散点 pl.plot(x, y, 'or') #散点变为红色 pl.show() # show the plot on the screen