Python matplotlib模块实现数据可视化

代码如下:

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
from random import choice, randint
import pygalplt.style.use('ggplot')   #使用ggplot的风格
fig = plt.figure() #创建一个基础图
#下面这行,设置基础图的标题,fontsize是字体大小,fontweight是字体(吧)
fig.suptitle('Suptitle of Figure', fontsize = 14, fontweight = 'bold')
fig.set_size_inches(10, 10) #设置基础图大小 #条形图 Bar Figure
customers = ['Ziggy', 'John', 'Dylan', 'Ringo']
customers_index = range(len(customers))
print("Customer Index: ", customers)
sale_amount = [100, 200, 150, 140]
another_sale_amount = [sa + 10 for sa in sale_amount]
#下面这行意为:基础图分三行二列,这个条形图使用第一个
ax1 = fig.add_subplot(3,2,1)
#添加柱状图
#ax1.bar(customers_index, sale_amount, width = 0.5, align = 'center', color = 'purple')
#参数分别表示:索引/高度/柱状图的透明度/柱状图的宽度/颜色/标签(用于图例)
ax1.bar(customers_index, sale_amount, alpha = 0.7, width = 0.5, color = 'purple', label = 'First')
ax1.bar(customers_index, another_sale_amount, alpha = 0.7, width = 0.5, color = 'orange', label = 'Second')
#设置坐标轴位置
ax1.xaxis.set_ticks_position('bottom')
ax1.yaxis.set_ticks_position('left')
#设置坐标轴所表示的含义
ax1.set_xlabel('Customers')
ax1.set_ylabel('Sale Amount')
#设置横坐标标签
ax1.set_xticks(customers_index)
ax1.set_xticklabels(customers, rotation = 10)   #rotation 表示标签的倾斜角
#设置这张条形图的标题
ax1.set_title('Bar Figure')
#创建图例,loc='best'表示图里的位置随柱状图的位置确定最优位置
ax1.legend(loc = 'best')#分布直方图 Hist Figure
ax2 = fig.add_subplot(3,2,2)
mu1, mu2, sigma = 100, 130, 15
x1 = mu1 + sigma * np.random.randn(10000)
print(x1)
x2 = mu2 + sigma * np.random.randn(10000)
#设置直方图,bins=50表示每个变量的值应该被分成50份
n, bins, patches = ax2.hist(x1, bins = 50, normed = False, color = 'purple')
print(n, "  ", bins, "  ", patches)
n, bins, patches = ax2.hist(x2, bins = 50, normed = False, color = 'orange', alpha = 0.5)
ax2.xaxis.set_ticks_position('bottom')
ax2.yaxis.set_ticks_position('left')
ax2.set_xlabel('Bins')
ax2.set_ylabel('Number of Values in Bin')
ax2.set_title('Hist Figure')#折线图
plot_data1 = np.random.randn(10).cumsum()
plot_data2 = np.random.randn(10).cumsum()
plot_data3 = np.random.randn(10).cumsum()
plot_data4 = np.random.randn(10).cumsum()
ax3 = fig.add_subplot(3,2,3)
#添加折现
#‘o’表示圆点,‘+’表示加号,‘*’表示星号,‘s’表示方块
#‘-’表示实线,‘--’表示虚线,‘-.’表示点线结合的虚线,‘:’表示点虚线
ax3.plot(plot_data1, marker = r'o', color = u'blue', linestyle = '-', label = 'Blue Solid')
ax3.plot(plot_data2, marker = r'+', color = u'pink', linestyle = '--', label = 'Pink Dashed')
ax3.plot(plot_data3, marker = r'*', color = u'orange', linestyle = '-.', label = 'Orange Dash Dot')
ax3.plot(plot_data4, marker = r's', color = u'green', linestyle = ':', label = 'Green Dotted')
ax3.xaxis.set_ticks_position('bottom')
ax3.yaxis.set_ticks_position('left')
ax3.set_xlabel('Draw')
ax3.set_ylabel('Random Number')
ax3.set_title('Line Figure')
ax3.legend(loc = 'best')#散点图+回归线
x = np.arange(start = 1., stop = 15., step = 1.)
y_linear = x + 5. * np.random.randn(14)
y_quadratic = x ** 2 + 10. * np.random.randn(14)
fn_linear = np.poly1d(np.polyfit(x, y_linear, deg = 1))
fn_quadratic = np.poly1d(np.polyfit(x, y_quadratic, deg = 2))
ax4 = fig.add_subplot(3,2,4)
#‘bo’表示蓝色圆点
ax4.plot(x, y_linear, 'bo', x, y_quadratic, 'go',\x, fn_linear(x), 'b-', x, fn_quadratic(x), 'g-', linewidth = 2.)
ax4.xaxis.set_ticks_position('bottom')
ax4.yaxis.set_ticks_position('left')
ax4.set_xlabel('x')
ax4.set_ylabel('f(x)')
ax4.set_title('Scatter Plot with Best Fit Line Figure')
ax4.set_xlim((min(x)-1., max(x)+1.))
ax4.set_ylim((min(y_quadratic)-10., max(y_quadratic)+10.))#散点图
x_values = [1,2,3,4,5,6]
y_values = [x**2 for x in x_values]
ax5 = fig.add_subplot(3,2,5)
#添加散点,c表示颜色,cmap=plt.cm.Blues表示颜色是渐变的蓝色
#edgecolor表示圆点的便捷颜色,s表示圆点的大小
ax5.scatter(x_values, y_values, c = y_values, cmap = plt.cm.Blues, \edgecolor = 'none', s = 40)
ax5.xaxis.set_ticks_position('bottom')
ax5.yaxis.set_ticks_position('left')
ax5.set_xlabel('x')
ax5.set_ylabel('f(x)')
ax5.set_title('Scatter Figure')
ax5.axis([0, max(x_values)+1, 0, max(y_values)+1])#箱线图
N = 500
normal = np.random.normal(loc = 0.0, scale = 1.0, size = N)
lognormal = np.random.lognormal(mean = 0.0, sigma = 1.0, size = N)
index_value = np.random.random_integers(low = 0, high = N-1, size = N)
normal_sample = normal[index_value]
lognormal_sample = lognormal[index_value]
#添加箱型图
box_plot_data = [normal, normal_sample, lognormal, lognormal_sample]
box_labels = ['normal', 'normal_sample', 'lognormal', 'lognormal_sample']
ax6 = fig.add_subplot(3,2,6)
ax6.boxplot(box_plot_data, notch = False, sym = ',', vert = True, whis = 1.5, \showmeans = True, labels = box_labels)
ax6.set_xticklabels(box_labels, rotation = 10)
ax6.xaxis.set_ticks_position('bottom')
ax6.yaxis.set_ticks_position('left')
ax6.set_xlabel('Distribution')
ax6.set_ylabel('Value')
ax6.set_title('Box Plots: Resampling of Two Distribution')plt.tight_layout()  #布局最优化,可防止图之间相互重叠
plt.show()  #展示图
#保存图,参数分别表示:文件名、分辨率
#bbox_inches='tight'表示去掉多余的白边,不想去掉可以忽略
plt.savefig('hist_fig.png', dpi=400, bbox_inches = 'tight')

显示结果:

解决的问题:

六种图的创建方法;

更改图片的尺寸大小;

坐标轴标签倾斜角度;

颜色渐变;

图片之间重叠问题;

图例遮挡问题。

未解决的问题:

通过我这种方法实现多个条形图并列在一个坐标轴中的效果。如果有人知道麻烦告诉我,谢谢!!!

参考文献:
《Python数据分析基础》
《Python编程从入门到实践》
https://www.cnblogs.com/shanger/p/13054285.html

Python matplotlib模块实现数据可视化相关推荐

  1. python matplotlib 地图_Python数据可视化,看这篇就够了

    说到python的常见应用,很多人会想到python的数据分析,作为数据分析中的表现层面,数据可视化都是其中必不可少的部分.但本文并非只推荐无任何数据分析需求仅需要做漂亮可视化图表的人学习python ...

  2. Python爬虫实战,pyecharts模块,Python实现中国地铁数据可视化

    前言 利用Python实现中国地铁数据可视化.废话不多说. 让我们愉快地开始吧~ 开发工具 Python 版本:3.6.4 相关模块: requests模块; wordcloud模块; 熊猫模块; n ...

  3. python matplotlib模块——绘制三维图形、三维数据散点图

    2019独角兽企业重金招聘Python工程师标准>>> 分类: 计算机视觉 python matplotlib模块,是扩展的MATLAB的一个绘图工具库.他可以绘制各种图形,可是最近 ...

  4. python导出数据顿号做分隔符_Python语言和matplotlib库做数据可视化分析

    这是我的第51篇原创文章,关于数据可视化分析. 阅读完本文,你可以知道: 1 Python语言的可视化库-matplotlib? 2 使用matplotlib实现常用的可视化? 0前言 数据记者和信息 ...

  5. python与excel做数据可视化-Python的Excel操作及数据可视化

    Excel表操作 python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库. 安装xlrd pip install xlrd 简单的表格读取 ...

  6. 新书《Python Qt GUI与数据可视化编程》

    经过一年多写作和出版社的编辑加工,我的第二本书<Python Qt GUI与数据可视化编程>马上就正式上架了,敬请关注. 本书介绍在Python中使用PyQt5和其他模块进行GUI和数据可 ...

  7. python qt5 gui快速编程_现货正版 Python Qt GUI与数据可视化编程 pyqt5教程书籍 pyqt5快速开发与实战Qt5 GUI快速编程 计算机网络程序设计人民邮电出版社...

    热销单品 查看更多 > RMB:85.00 立即购买 RMB:63.50 立即购买 RMB:73.50 立即购买 RMB:49.50 立即购买 RMB:127.80 立即购买 RMB:66.00 ...

  8. python与excel做数据可视化-我在工作中是怎么玩数据的—数据可视化系列教程—Python篇...

    一. 为什么是Python? Python现在已经成为数据科学的语言!基于 Python 代码实现批量化,流程化的数据探索与汇报!按照地产大佬***的话讲--就是重复性的工作直接用Python搞定就可 ...

  9. 30行python代码设计_30行Python代码实现3D数据可视化

    原标题:30行Python代码实现3D数据可视化 作者:潮汐 来源:Python技术 欢迎来到 编程教室~ 我们之前的文章中有讲解过不少 Matplotlib 的用法,比如: 之前我们基本都是用它来绘 ...

最新文章

  1. 【项目展示】一个有点难度的猜数字小游戏(Java编写)
  2. QShell下的进程和ILE中的Job
  3. OpenGL 绘制彩色三角形的实例
  4. Android 中的 Context
  5. Apache2.4开启php
  6. iOS AVPlayer 简单应用
  7. Linux下的一些简单网络配置命令介绍
  8. [PY3]——IO——文件目录操作
  9. nebula模拟器_nebula模拟器中文金手指版本
  10. 游戏必备组件有哪些_微信广告将升级小程序、小游戏开发者收入方案
  11. 诺基亚s40机破权相关说明
  12. php油酸诱导剂,油酸诱导建鲤(Cyprinus carpio var. Jian)肝细胞脂肪变性模型的建立
  13. excel各种填充的总结
  14. H.264中的SPamp;amp;SI帧技术简述
  15. 微信自动检测色情图片_python +itchat
  16. 取汉字拼音首字母--生成不重复ID(汉字--拼音--首字母)
  17. Command ‘pip‘ not found, but can be installed with:
  18. 新年第一篇,androidHAL层知多少?
  19. iOS开发 - 类似美团选商品页,从按钮上往上滑动,tableview依然响应,点击按钮,按钮也可响应
  20. 《程序员养成记》第1集 做程序员需要天赋吗?

热门文章

  1. 数字图像处理实验[实验二:图像拉普拉斯锐化]
  2. ps-增加光影效果质感
  3. 土地利用数据与土地覆被数据的区别
  4. vue定时器(实时刷新列表)
  5. somebot机械臂安装说明书-v0.4
  6. 仿作苏宁易购主页(前端学习记录)
  7. Linux常见命令作用笔记
  8. Java后台项目的顶层设计思路
  9. 我们需要怎样的大学生
  10. mc服务器经济系统,[经济]NyEconomy — 多经济系统 , 让你不局限于只有几种货币[1.8-1.12] - 服务端插件 - Minecraft(我的世界)中文论坛 -...