matplotlib

  • 图形可视化,主要用来画图
  • 别问,问就是看不懂

折线图

  1. # 一般使用下面的这个语句,设置字体编码
  2. plt.rcParams['font.sans-serif'] = ['SimHei']
  3. plt.rcParams['axes.unicode_minus'] = False
  1. # cmd命令行用ipython也可以执行这些代码
  2. x = [10,2,3]
  3. y = [11,23,10]
  4. plt.title('标题', fontsize=20, color='red')
  5. plt.ylabel('y轴', fontsize=20, color='green')
  6. plt.xlabel('x轴', fontsize=20)
  7. # plt.plot(x, y, linestyle=':', marker='v') #### 画折线图
  8. plt.plot(x, y,'--v' )
  9. plt.show()

柱状图

  • 豆瓣电影数据为例,画出需求图
  1. movies = pd.read_csv('douban_movie.csv')
  2. movies.head()
名字 投票人数 类型 产地 上映时间 时长 年代 评分 首映地点
0 肖申克的救赎 692795.0 剧情/犯罪 美国 1994-09-10 00:00:00 142 1994 9.6 多伦多电影节
1 控方证人 42995.0 剧情/悬疑/犯罪 美国 1957-12-17 00:00:00 116 1957 9.5 美国
2 美丽人生 327855.0 剧情/喜剧/爱情 意大利 1997-12-20 00:00:00 116 1997 9.5 意大利
3 阿甘正传 580897.0 剧情/爱情 美国 1994-06-23 00:00:00 142 1994 9.4 洛杉矶首映
4 霸王别姬 478523.0 剧情/爱情/同性 中国大陆 1993-01-01 00:00:00 171 1993 9.4 香港
画出各个国家或者地区电影的数量
  1. res = movies.groupby('产地').size().sort_values(ascending=False) # 根据产地分组,降序显示数量
  2. x = res.index
  3. y = res.values
  4. plt.figure(figsize=(20,6)) # 设置画布大小
  5. plt.title('各个国家或者地区电影的数量', fontsize=20, color='red') # 设置标题
  6. plt.xlabel('产地', fontsize=20) # x轴标题
  7. plt.ylabel('数量', fontsize=18) # y轴标题
  8. plt.xticks(fontsize=15, rotation=45) # x轴刻度,rotation代表旋转多少度
  9. for a, b in zip(x, y):
  10. # text就是写值,a,b+100是代表写的做表,b是代表要写的值,horizontalalignment代表些的位置
  11. plt.text(a,b+100, b, fontsize=15, horizontalalignment='center')
  12. plt.bar(x, y) # bar代表柱状图
  13. plt.show()
  14. plt.savefig('a.jpg') # 保存

饼状图

  • 根据电影的长度绘制饼图
cut方法
  1. pd.cut( np.array([0.2, 1.4, 2.5, 6.2, 9.7, 2.1]), [1,2,3] ) # 判断值是否在(1,2] (2,3]区间中
  1. [NaN, (1.0, 2.0], (2.0, 3.0], NaN, NaN, (2.0, 3.0]]
  2. Categories (2, interval[int64]): [(1, 2] < (2, 3]]

  1. df = movies.head()
  2. df
名字 投票人数 类型 产地 上映时间 时长 年代 评分 首映地点
0 肖申克的救赎 692795.0 剧情/犯罪 美国 1994-09-10 00:00:00 142 1994 9.6 多伦多电影节
1 控方证人 42995.0 剧情/悬疑/犯罪 美国 1957-12-17 00:00:00 116 1957 9.5 美国
2 美丽人生 327855.0 剧情/喜剧/爱情 意大利 1997-12-20 00:00:00 116 1997 9.5 意大利
3 阿甘正传 580897.0 剧情/爱情 美国 1994-06-23 00:00:00 142 1994 9.4 洛杉矶首映
4 霸王别姬 478523.0 剧情/爱情/同性 中国大陆 1993-01-01 00:00:00 171 1993 9.4 香港
  1. s = np.array(df['时长'])
  2. '8U' in s # 垃圾数据
  3. np.where('8U' == s)
  1. (array([31644], dtype=int64),)
  1. '12J' in s
  2. np.where('12J' == s)
  1. (array([32948], dtype=int64),)
  1. s = np.delete(s, 31644, axis=0)
  2. s = np.delete(s, 32947, axis=0) # 删了就会少一行
  3. np.where('12J' == s)
  1. (array([], dtype=int64),)
  1. data = pd.cut(s.astype('float'), [0,60,90,110,1000]).value_counts() # astype:强制转换
  2. data
  1. (0, 60] 10323
  2. (60, 90] 7727
  3. (90, 110] 13233
  4. (110, 1000] 7449
  5. dtype: int64
  1. x = data.index
  2. y = data.values
  3. plt.figure(figsize=(10,6))
  4. plt.title('电影时长分布图')
  5. patchs, l_text, p_text = plt.pie(y, labels=x, autopct='%0.2f%%', colors='bgry') # pie画饼图
  6. for i in p_text:
  7. i.set_size(15)
  8. i.set_color('w')
  9. for l in l_text:
  10. l.set_size(20)
  11. l.set_color('r')
  12. plt.show()

NICK

条形图

  1. import matplotlib.pyplot as plt
  2. # 只识别英语,所以通过以下两行增加中文字体
  3. from matplotlib.font_manager import FontProperties
  4. # 字体路径根据电脑而定
  5. font = FontProperties(fname='M:\STKAITI.TTF')
  6. # jupyter 默认不显示图片,通过这一行告诉他显示
  7. %matplotlib inline
  8. classes = ['1班', '2班', '3班', '4班'] # 相当于columns
  9. student_amounts = [30, 20, 30, 40] # 值
  10. classes_index = range(len(classes)) # [0, 1, 2, 3]
  11. plt.bar(classes_index, student_amounts)
  12. plt.xticks(classes_index, classes, FontProperties=font)
  13. for ind,student_amount in enumerate(student_amounts):
  14. print(ind,student_amount)
  15. plt.text(ind,student_amount+1,student_amount)
  16. plt.xlabel('班级', FontProperties=font)
  17. plt.ylabel('学生人数', FontProperties=font)
  18. plt.title('班级-学生人数', FontProperties=font)
  19. plt.show()
  1. 0 30
  2. 1 20
  3. 2 30
  4. 3 40

直方图

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.font_manager import FontProperties
  4. %matplotlib inline
  5. font = FontProperties(fname='M:\STKAITI.TTF')
  6. mu1, mu2, sigma = 50, 100, 10
  7. x1 = mu2 + sigma * np.random.randn(10000)
  8. print(x1)
  1. [ 93.49947877 86.87378653 98.0194217 ... 108.33555519 90.58512015
  2. 102.19048574]
  1. x1 = np.random.randn(10000)
  2. print(x1)
  1. [ 0.85927045 -0.8061112 1.30878058 ... -0.32700199 -0.67669564
  2. 0.25750884]
  1. x2 = mu2 + sigma*np.random.randn(10000)
  2. print(x2)
  1. [101.62589858 109.86489987 117.41374105 ... 97.52364544 107.21076273
  2. 99.56765772]
  1. plt.hist(x1, bins=100)
  2. plt.hist(x2, bins=100)
  3. plt.show()

  1. plt.style.use('ggplot')
  2. fig = plt.figure()
  3. # 相当于把一整块画板分成了1行2列的两个画板
  4. ax1 = fig.add_subplot(121)
  5. ax1.hist(x1, bins=100, color='red')
  6. ax1.set_title('红色', fontproperties=font)
  7. ax2 = fig.add_subplot(122)
  8. ax2.hist(x2, bins=100, color='yellow')
  9. ax2.set_title('黄色', fontproperties=font)
  10. fig.suptitle('大标题', fontproperties=font, fontsize=15, weight='bold')
  11. plt.show()

折线图

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.font_manager import FontProperties
  4. %matplotlib inline
  5. font = FontProperties(fname='M:\STKAITI.TTF')
  1. plt.style.use('ggplot')
  2. np.random.seed(1)
  3. data1 = np.random.rand(40).cumsum()
  4. data2 = np.random.rand(40).cumsum()
  5. data3 = np.random.rand(40).cumsum()
  6. data4 = np.random.rand(40).cumsum()
  1. plt.plot(data1, color='r', linestyle='-', alpha=0.5, label='红色')
  2. plt.plot(data2, color='green', linestyle='--', label='绿色')
  3. plt.plot(data3, color='yellow', linestyle=':', label='黄色')
  4. plt.plot(data4, color='blue', linestyle='-.', label='蓝色')
  5. plt.legend(prop=font)
  6. plt.show()

  1. arr = np.array([1, 2, 3, 4])
  2. arr.cumsum()# 1,1+2,1+2+3,1+2+3+4
  1. array([ 1, 3, 6, 10], dtype=int32)

散点图

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.font_manager import FontProperties
  4. %matplotlib inline
  5. font = FontProperties(fname='M:\STKAITI.TTF')
  1. x = np.arange(1, 20)
  2. x
  1. array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
  2. 18, 19])
  1. y_linear = x**2
  2. y_linear
  1. array([ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169,
  2. 196, 225, 256, 289, 324, 361], dtype=int32)
  1. y_log = np.log(x)
  2. y_log
  1. array([0. , 0.69314718, 1.09861229, 1.38629436, 1.60943791,
  2. 1.79175947, 1.94591015, 2.07944154, 2.19722458, 2.30258509,
  3. 2.39789527, 2.48490665, 2.56494936, 2.63905733, 2.7080502 ,
  4. 2.77258872, 2.83321334, 2.89037176, 2.94443898])
  1. fig = plt.figure()
  2. ax1 = fig.add_subplot(311)
  3. ax1.scatter(x, y_linear, color='red', marker='o', s=100)
  4. ax1.scatter(x, y_log, color='blue', marker='*', s=30)
  5. ax1.set_title('scatter')
  6. ax2 = fig.add_subplot(313)
  7. ax2.plot(x, y_linear)
  8. ax2.plot(x, y_log)
  9. ax2.set_title('plot')
  10. plt.plot
  11. plt.show()

day27-2 pandas模块的更多相关文章

  1. python之pandas模块

    一.pandas模块是基于Numpy模块的,pandas的主要数据结构是Series和DadaFrame,下面引入这样的约定: from pandas import Series,DataFrame ...

  2. Python 数据处理扩展包: numpy 和 pandas 模块介绍

    一.numpy模块 NumPy(Numeric Python)模块是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list str ...

  3. 关于Python pandas模块输出每行中间省略号问题

    关于Python数据分析中pandas模块在输出的时候,每行的中间会有省略号出现,和行与行中间的省略号....问题,其他的站点(百度)中的大部分都是瞎写,根本就是复制黏贴以前的版本,你要想知道其他问题 ...

  4. Pandas模块

    前言: 最近公司有数据分析的任务,如果使用Python做数据分析,那么对Pandas模块的学习是必不可少的: 本篇文章基于Pandas 0.20.0版本 话不多说社会你根哥!开干! pip insta ...

  5. pandas模块实现小爬虫功能-转载

    pandas模块实现小爬虫功能 安装 pip3 install pandas 爬虫代码 import pandas as pd df = pd.read_html("http://www.a ...

  6. Pandas模块:表计算与数据分析

    目录 Pandas之Series Pandas之DataFrame 一.pandas简单介绍 1.pandas是一个强大的Python数据分析的工具包.2.pandas是基于NumPy构建的. 3.p ...

  7. pandas模块(很详细归类),pd.concat(后续补充)

    6.12自我总结 一.pandas模块 import pandas as pd约定俗称为pd 1.模块官方文档地址 https://pandas.pydata.org/pandas-docs/stab ...

  8. Python数据分析 Pandas模块 基础数据结构与简介(一)

    pandas 入门 简介 pandas 组成 = 数据面板 + 数据分析工具 poandas 把数组分为3类 一维矩阵:Series 把ndarray强大在可以存储任意数据类型可以专门处理时间数据 二 ...

  9. 4 pandas模块,Series类

      对gtx图像进行操作,使用numpy知识 如果让gtx这张图片在竖直方向上进行颠倒.   如果让gtx这张图片左右颠倒呢?   如果水平和竖直方向都要颠倒呢?   如果需要将gtx的颜色改变一下呢 ...

  10. 开发技术--pandas模块

    开发|pandas模块 整了一篇关于pandas模块的使用文章,方便检查自己的学习质量.自从使用了pandas之后,真的是被它的功能所震撼~~~ 前言 目前所有的文章思想格式都是:知识+情感. 知识: ...

随机推荐

  1. 图片base64格式转为file文件类型上传方法

    日常使用文件上传方式,都是通过input type='file'的文件选择框进行文件上传.但是会通过其他交互方式等到图片的base64格式进行上传.具体情况如下示意: 在项目开发中,需要进行照片采集, ...

  2. elasticsearch 分析器阅读笔记(五)

    倒排索引 可以查看这里得分词原理https://www.cnblogs.com/LQBlog/articles/5743991.html 分析器 分析器处理过程的3步骤 1.字符过滤器:去除字符的特殊 ...

  3. 推荐一个同步Mysql数据到Elasticsearch的工具

    把Mysql的数据同步到Elasticsearch是个很常见的需求,但在Github里找到的同步工具用起来或多或少都有些别扭. 例如:某记录内容为"aaa|bbb|ccc",将其按 ...

  4. [bzoj1606][Usaco2008 Dec]Hay For Sale 购买干草_动态规划_背包dp

    Hay For Sale 购买干草 bzoj-1606 Usaco-2008 Dec 题目大意:约翰遭受了重大的损失:蟑螂吃掉了他所有的干草,留下一群饥饿的牛.他乘着容量为C(1≤C≤50000)个单 ...

  5. SecureCRTPortal保存的密码位置

    SecureCRTPortal保存的密码位置 Options> Global Options > Configuration Folder 一般为:C:\Users\Administrat ...

  6. SERVICE_NAME和SERVICE_NAMES和GLOBAL_DBNAME的各自己定义

    tnsnames.ora文件中边SERVICE_NAME的參数值--对于动态注冊和静态注冊.该參数有不同的取值 对于动态注冊: The following pfile/spfile parameter ...

  7. &#39;hibernate.dialect&#39; must be set when no Connection available

    今天碰到的这个问题,非常无厘头.网上搜索了非常多.都不靠谱,还是靠自己 解决方法是在hibernate.cfg.xml中加入 <property name="dialect" ...

  8. java生成一张图片

    public class CreateImage { public static void main(String[] args) throws Exception{ int width = 100; ...

  9. 国王的烦恼---nyoj

    国王的烦恼 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 C国由n个小岛组成,为了方便小岛之间联络,C国在小岛间建立了m座大桥,每座大桥连接两座小岛.两个小岛间可能存在 ...

  10. linux git保存用户名密码(避免每次push输用户名密码)

    Linux/Unix/Mac 系统 新建一个 ~/.netrc 文件, 将 git 服务器, 用户名以及密码记录在这个文件, 如下所示:   machine your-git-server   log ...