Matplotlib是一个Python 2D绘图库, 只需几行代码即可生成绘图,直方图,功率谱,条形图,错误图,散点图等。 有关示例,请参阅示例图和缩

import matplotlib.pyplot as plt
import numpy as np
class TestPlot(object):
def __init__(self,plt):
self.plt = plt
#定义内部属性
# 解决中文乱码问题(第二种)
plt.rcParams['font.sans-serif'] = ['SimHei']
#指定编码
plt.rcParams['axes.unicode_minus'] = False #定义面积图方法(*********************************************) def my_area(self):
#定义日期区间
data = ['2019-03-01','2019-03-02','2019-03-03','2019-03-04','2019-03-05']
#定义数据
#收入
earn = [166,155,355,422,622]
#支出
pay = [[16,30,25,46,20],[10,15,20,144,122]]
#将数据传入方法
self.plt.stackplot(data,earn,pay,colors=['green','yellow','orange'])
#生成图例
self.plt.plot([],[],color='green',label="收入")
self.plt.plot([],[],color='yellow',label="午餐")
self.plt.plot([],[],color='orange',label="晚餐")
#设置标题
self.plt.title("面积图样例")
self.plt.legend()
self.plt.show()
#定义柱状图(*********************************************)
def my_bar(self):
my_plt = self.plt
GDP = [12404.1,13396.222,5335.22,5223.22]
my_plt.bar(["北京",'上海','深圳','重庆'],GDP,align='center',color="lime",alpha=0.8)
my_plt.ylabel("生产总值")
#添加标题
my_plt.title("直辖市GDP")
#刻度范围
my_plt.ylim([5000,15000])
my_plt.show()
#定义饼图(*********************************************)
def my_pie(self):
my_plt = self.plt
beijing = [17,22,23,46]
#定义标签
label = ['2-3年','3-4年','4-5年','五年以上']
color = ['red','green','blue','purple']
#将数值最大的突出展示
indic = []
#使用enumerate方法来添加索引
for index,i in enumerate(beijing):
if i == max(beijing):
indic.append(0.5)
elif index == 1:
indic.append(0.2)
else:
indic.append(0)
# for i in beijing:
# if i == max(beijing):
# indic.append(0.1)
# else:
# indic.append(0)
#将数据传入 ,数据,标签,颜色,角度,阴影,突出显示
my_plt.pie(
beijing,
labels= label,
colors = color,
startangle=90,
shadow = True,
explode = tuple(indic),
#格式化数字
autopct = "%1.1f%%"
)
# 设置标题
my_plt.title("饼图实例-统计北京程序员工龄占比")
my_plt.show()
##曲线图走势图(*********************************************)
def my_line(self):
my_line= self.plt
# #设置本机字体(第一种)
# font = FontProperties(fname='C:/Windows/Fonts/simhei.ttf',size=15)
#定制数据
x1 = ['2019-03-01','2019-03-02','2019-03-03','2019-03-04','2019-03-05','2019-03-06']
y1 = [0,6,9,5,3,2]
y2 = [10,12,16,12,16,17]
#填充数据
my_line.plot(x1,y1,label='temperature')
my_line.plot(x1,y2,label='water')
#设置标题
# my_line.title("趋势图",FontProperties=font)
my_line.title("趋势图")
# #显示图例
my_line.legend()
my_line.show()
#点状图(*********************************************)
def my_dot(self):
my_dot = self.plt
x = list(range(101))
y = [xvalue * np.random.rand() for xvalue in x]
#填充数据 s 点的大小和粗细 c 颜色
my_dot.scatter(x,y,s=20,c='lime')
#绘图
my_dot.show()
# 定义横向条形图(*********************************************)
def my_barh(self):
my_plt = self.plt
#定义价格
price = [40,32.8,20,19.6]
#将数据传入
my_plt.barh(range(4),price,align="center",color="red",alpha=0.5)
#设置标签
my_plt.xlabel('价格')
#将数据传入y轴
my_plt.yticks(range(4),['红楼梦','三国演义','西游记','水浒传'])
#设置上下限
my_plt.xlim([10,60])
#设置标题
my_plt.title("四大名著")
#绘制
my_plt.show() if __name__ == "__main__":
testplot = TestPlot(plt)
# testplot.my_area()
# testplot.my_bar()
# testplot.my_pie()
# testplot.my_line()
# testplot.my_dot()
# testplot.my_barh() # # Matplot 生成子图(一个图中两个子图)
# #第一组数据
# x1 = list(range(5))
# y1 = list(map(lambda x:x**2,x1))
# #第二组数据
# x2 = list(range(4,10))
# y2 = list(map(lambda x:x**2/2,x2)) # ##做第一幅图 2*1矩阵
# ax1 = plt.subplot(121)
# #传入数据
# ax1.plot(x1,y1)
# ax2 = plt.subplot(122)
# ax2.plot(x2,y2)
# # #删除字图
# # plt.delaxes(ax2)
# # #增加字图
# # plt.subplot(ax2)
# plt.show() #画散点图 # df = pd.read_excel('tips.xlsx','sheet1')
# df.plot(kind='hist',x='tip',y='total_bill',color='lime',label='bill_tip')
# plt.title("heelo")
# plt.show()

略图库。

Matplotlib 简单的使用的更多相关文章

  1. matplotlib简单示例

    一.简介 以下引用自百度百科 Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形 . 通过 Matplotlib,开发者可以仅需要 ...

  2. matplotlib简单的使用(二)

    1.折线图 import matplotlib as mlb from matplotlib import pylab as pl # 折线图 # 分别创建x,y坐标 x = [1,3,5,7,6,9 ...

  3. python matplotlib 简单生成图

    import numpy as np import pandas as pd from matplotlib import pyplot as plt data = pd.DataFrame([[1, ...

  4. Matplotlib 简单图例

    图例参考:http://matplotlib.org/gallery.html API参考:http://matplotlib.org/api/pyplot_summary.html # -*- co ...

  5. matplotlib简单的新手教程和动画

    做数据分析,首先是要熟悉和理解数据,所以掌握一个趁手的可视化工具是很重要的,否则对数据连个主要的感性认识都没有,怎样进行下一步的design 点击打开链接 还有一个非常棒的资料  Matplotlib ...

  6. python爬取旅游数据+matplotlib简单可视化

    题目如下: 共由6个函数组成: 第一个函数爬取数据并转为DataFrame: 第二个函数爬取数据后存入Excel中,对于解题来说是多余的,仅当练手以及方便核对数据: 后面四个函数分别对应题目中的四个m ...

  7. Matplotlib简单回顾

    import numpy as np from pylab import * from matplotlib import pyplot as plt x = [1, 2, 3, 4] y = [3, ...

  8. matplotlib简介及安装

    官网介绍: Matplotlib is a Python 2D plotting library which produces publication quality figures in a var ...

  9. matplotlib绘图基本用法-转自(http://blog.csdn.net/mao19931004/article/details/51915016)

    本文转载自http://blog.csdn.net/mao19931004/article/details/51915016 <!DOCTYPE html PUBLIC "-//W3C ...

随机推荐

  1. Angular4学习笔记(三)- 路由

    路由简介 路由是 Angular 应用程序的核心,它加载与所请求路由相关联的组件,以及获取特定路由的相关数据.这允许我们通过控制不同的路由,获取不同的数据,从而渲染不同的页面. 相关的类 Routes ...

  2. 转:【WebView的cookie机制 】轻松搞定WebView cookie同步问题

    原文链接:http://blog.csdn.net/fengyuzhengfan/article/details/51517622 在进行APP+H5混合开发的时候,一些功能是用native方法实现的 ...

  3. [Bayes] prod: M-H: Independence Sampler for Posterior Sampling

    M-H是Metropolis抽样方法的扩展,扩展后可以支持不对称的提议分布. 对于M-H而言,根据候选分布g的不同选择,衍生出了集中不同的变种: (1)Metropolis抽样方法 (2)随机游动Me ...

  4. [React] 11 - Redux: redux

    Ref: Redux中文文档 Ref: React 讀書會 - B團 - Level 19 Redux 深入淺出 Ref: React+Redux 分享會 Ruan Yifeng, Redux 架构: ...

  5. Hadoop -- HDFS 读写数据

    一.HDFS读写文件过程 1.读取文件过程 1)       初始化FileSystem,然后客户端(client)用FileSystem的open()函数打开文件 2)       FileSyst ...

  6. LUA重难点解析

    1.元表 元表也是一个 table,它附加在另一个 table 上,可以扩展该 table 的某些行为. 拿 __index 来举例,它是用来扩展查找索引行为的.在查找一个 key 对应的值时,会依次 ...

  7. js给原型增加新属性和方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. WP8.1学习系列(第二十七章)——ListView和GridView入门

    快速入门:添加 ListView 和 GridView 控件 (XAML)   在本文中 先决条件 选择 ListView 或 GridView 将项添加到项集合 设置项目源 指定项目的外观 指定视图 ...

  9. shell join详解

    首先贴一个,join --help Usage: join [OPTION]... FILE1 FILE2 For each pair of input lines with identical jo ...

  10. jmeter之结果重定向

    在使用jmeter与jenkins对接时,发现默认打印出来的日志就是正常的summary统计,如果要查看日志,只能通过jmeter.log去查看. 来来来,我们一起温习下jmeter的命令行参数 -- ...