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. 【emWin】例程二十二:窗口对象——Framewin

    简介: 框架窗口为您的应用提供一个PC 应用程序的窗口外观.这些窗口由周围框架.标题栏和用户区组成. 触摸校准(上电可选择是否进入校准界面) 截图 实验指导书及代码包下载: 链接:http://pan ...

  2. hdoj:2045

    #include <iostream> using namespace std; ]; int main() { int n; a[] = ; a[] = ; a[] = ; ; i &l ...

  3. Android KK 找不到<cutils/properties.h>

    一直通过property来控制android系统的号码匹配位数,之前的项目都工作的好好的,但到了KK时,在sqlite库中引用property的相关方法,却一直编译error... 折腾了好久,发现从 ...

  4. Oracle 11g EM删除重建的方法

    虚拟机里的Oracle 11g好长时间没用了,突然打开之后发现EM无法访问了,EM可以重建,于是也不打算查找原因了,直接使大招 OS:Windows Server 2012 Oracle:11g R2 ...

  5. CentOS6上实现Tomcat8 service启动,并查看status

    service配置脚本,“/etc/init.d/tomcat”,实现通过"service tomcat status " 查看tomcat状态,并输出PID,见脚本 # desc ...

  6. 用Physijs在场景中添加物理效果

    1.创建可用Physijs的基本Three.js场景 创建一个可用Physijs的Three.js场景非常简单,只要几个步骤即可.首先我们要包含正确的文件, 需要引入physi.js文件.实际模拟物理 ...

  7. [Python] 00 - Books

    A.I. & Optimization Advanced Machine Learning, Data Mining, and Online Advertising Services Ref: ...

  8. [React] 07 - Flux: uni-flow for react

    相关资源 Ref: [Android Module] 03 - Software Design and Architecture Ref: Flux 架构入门教程 Ref: 详解React Flux架 ...

  9. WPF之依赖属性和附加属性

     参考资料: 一站式WPF--依赖属性(DependencyProperty)一 一站式WPF--依赖属性(DependencyProperty)二         依赖属性之我见: 这两篇文章介绍的 ...

  10. rtmp简要流程