Matplotlib 简单的使用
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 简单的使用的更多相关文章
- matplotlib简单示例
一.简介 以下引用自百度百科 Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形 . 通过 Matplotlib,开发者可以仅需要 ...
- matplotlib简单的使用(二)
1.折线图 import matplotlib as mlb from matplotlib import pylab as pl # 折线图 # 分别创建x,y坐标 x = [1,3,5,7,6,9 ...
- python matplotlib 简单生成图
import numpy as np import pandas as pd from matplotlib import pyplot as plt data = pd.DataFrame([[1, ...
- Matplotlib 简单图例
图例参考:http://matplotlib.org/gallery.html API参考:http://matplotlib.org/api/pyplot_summary.html # -*- co ...
- matplotlib简单的新手教程和动画
做数据分析,首先是要熟悉和理解数据,所以掌握一个趁手的可视化工具是很重要的,否则对数据连个主要的感性认识都没有,怎样进行下一步的design 点击打开链接 还有一个非常棒的资料 Matplotlib ...
- python爬取旅游数据+matplotlib简单可视化
题目如下: 共由6个函数组成: 第一个函数爬取数据并转为DataFrame: 第二个函数爬取数据后存入Excel中,对于解题来说是多余的,仅当练手以及方便核对数据: 后面四个函数分别对应题目中的四个m ...
- Matplotlib简单回顾
import numpy as np from pylab import * from matplotlib import pyplot as plt x = [1, 2, 3, 4] y = [3, ...
- matplotlib简介及安装
官网介绍: Matplotlib is a Python 2D plotting library which produces publication quality figures in a var ...
- matplotlib绘图基本用法-转自(http://blog.csdn.net/mao19931004/article/details/51915016)
本文转载自http://blog.csdn.net/mao19931004/article/details/51915016 <!DOCTYPE html PUBLIC "-//W3C ...
随机推荐
- chrome调试手机webview中页面
http://blog.csdn.net/freshlover/article/details/42528643 注: 1. 可以调试真机上页面(USB连接)和虚拟机上页面 2. 手机系统需要4.4+ ...
- .NET解决[Serializable] Attribute引发的Json序列化k_BackingField
在WebAPI中的WebApiConfig直接加入如下配置 有问题找谷歌
- 文件解压缩 tar zip
zip -e var-log-protected.zip /var/log/* Enter password: Verify password: updating: var/log/acpid (de ...
- [JS] ECMAScript 6 - Array : compare with c#
扩展运算符(spread) 先复习下 rest 参数. (1) argument模式,但不够好. // https://blog.csdn.net/weixin_39723544/article/de ...
- 4G 通信模块在ARM 平台下的应用
收藏 评论(0) 分享到 微博 QQ 微信 LinkedIn 4G模块是连接物与物的重要载体,是终端设备接入物联网的核心部件之一,随着4G的普及,许多新兴市场对4G通信模块的需求都在日益扩大,那么在A ...
- Math.ceil()、Math.floor()和Math.round()
下面来介绍将小数值舍入为整数的几个方法:Math.ceil().Math.floor()和Math.round(). 这三个方法分别遵循下列舍入规则: Math.ceil()执行向上舍入,即它总是将数 ...
- 重装win7,win10,Ubuntu
使用的软件多了,大了,感觉用起来很卡,快就是生产力,一定要重视.就去维修电安装了120G的固态硬盘,以前所安装的软件都没有了,距上一次重装快一年了,上次修了风扇,加了4G内存条.现在很多东西也一并删除 ...
- Project Move from Qt 4 to Qt 5 项目工程的迁移
将Qt4的项目迁到Qt5中并不需要新建一个Qt5的工程,可以直接在原工程文件上修改,这里我们使用的是VS2010和Qt5.4的环境,我们需要做以下修改: 1. 在工程里找到这个文件:工程名.vcxpr ...
- python3安装PIL
原创 2017-09-29 16:15:27 系统环境: 64位win10系统,同时安装python2.7与python3.6两个版本 安装: PIL是Python平台事实上的图像处理标准库,支 ...
- python爬取并批量下载图片
import requests from lxml import etree url='http://desk.zol.com.cn/meinv/' add1='.html' urls=[] i = ...