python中matplotlib绘图封装类之折线图、条状图、圆饼图
DrawHelper.py封装类源码:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np class DrawHelper:
def __init__(self):
# 指定默认字体 下面三条代码用来解决绘图中出现的乱码
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['font.family'] = 'sans-serif'
# 解决负号'-'显示为方块的问题
matplotlib.rcParams['axes.unicode_minus'] = False # 绘制饼状图清除type值为零,同时设置颜色(相同的类型相同的颜色)
def clear_zeroData(self, keys, values):
colors = ['yellow', 'green', 'red', 'blue', 'black', 'purple', 'pink', 'brown', 'grey', 'yellow', 'green', 'red', 'blue', 'black', 'purple', 'pink', 'brown', 'grey', 'yellow', 'green', 'red', 'blue', 'black', 'purple', 'pink', 'brown', 'grey']
keys_list = []
values_list = []
colors_list = []
for i in range(0, len(keys)):
if values[i] != 0:
keys_list.append(keys[i])
values_list.append(values[i])
colors_list.append(colors[i])
return (keys_list,values_list,colors_list) # 绘制折线图
def get_plot(self, key_list, value_list, actor):
index = np.arange(len(key_list))
# 设置画板大小
plt.figure(figsize=(9,9))
# 设置条状图标题
plt.title(actor+'电影类型分布折线图')
plt.xticks(index, key_list)
plt.grid(True)
plt.plot(index,value_list)
# 保存成图片
plt.savefig('images/plot/' + actor + '.png')
plt.close() # 绘制条状图
def get_bar(self, key_list, value_list, actor):
index = np.arange(len(key_list))
# 设置画板大小
plt.figure(figsize=(9,9))
# 设置条状图标题
plt.title(actor + '电影类型分布直方图')
plt.bar(index, value_list, 0.5)
plt.xticks(index, key_list)
plt.grid(True)
plt.savefig('images/bar/' + actor + '.png')
# 关闭图
plt.close() # 绘制饼状图
def get_pie(self, key_list, value_list, actor):
# 调用绘制饼状图清除type值为零,同时设置颜色函数
types_no_zero = self.clear_zeroData(key_list,value_list)
keys = types_no_zero[0]
values = types_no_zero[1]
colors = types_no_zero[2]
# 设置标题
plt.title(actor + '电影类型分布饼状图')
plt.pie(values, labels=keys, colors=colors,shadow=True, autopct='%1.1f%%')
plt.axis('equal')
plt.savefig('images/pie/' + actor + '.png')
# 关闭图
plt.close()
test.py测试:
from DrawHelper import DrawHelper types = (['剧情', '喜剧', '爱情', '动作', '犯罪', '武侠', '悬疑', '古装', '科幻', '惊悚', '奇幻', '恐怖', '鬼怪', '冒险', '家庭', '运动', '西部', '传记', '歌舞', '历史', '同性'], [11, 2, 3, 8, 10, 0, 2, 0, 0, 2, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0])
keys = types[0]
values = types[1]
actor = '刘德华'
DrawHelper().get_bar(keys,values,actor)
DrawHelper().get_pie(keys,values,actor)
DrawHelper().get_plot(keys,values,actor)
print("OK")
截图:
python中matplotlib绘图封装类之折线图、条状图、圆饼图的更多相关文章
- python 中matplotlib 绘图
python 中matplotlib 绘图 数学建模需要,对于绘图进行简单学习 matpoltlib之类的包安装建议之间用anaconda 绘制一条y=x^2的曲线 #比如我们要绘制一条y=x^2的曲 ...
- HighCharts之2D堆条状图
HighCharts之2D堆条状图 1.HighCharts之2D堆条状图源码 StackedBar.html: <!DOCTYPE html> <html> <head ...
- HighCharts之2D条状图
HighCharts之2D条状图 1.HighCharts之2D条状图源码 bar.html: <!DOCTYPE html> <html> <head> < ...
- Excel 2010高级应用-条状图(五)
Excel 2010高级应用-条状图(五) 基本操作如下: 1.新建一个Excel空白文档,并命名条状图 2.单击"插入",找到条状图的样图 3.选择其中一种类型的条状图样图,在空 ...
- Flex实现双轴条状图
1.问题背景 一般的,柱状图可以实现双轴图,但是如何实现双轴条状图? 2.实现实例 <?xml version="1.0" encoding="utf-8" ...
- python中matplotlib画折线图实例(坐标轴数字、字符串混搭及标题中文显示)
最近在用python中的matplotlib画折线图,遇到了坐标轴 "数字+刻度" 混合显示.标题中文显示.批量处理等诸多问题.通过学习解决了,来记录下.如有错误或不足之处,望请指 ...
- python之matplotlib绘图基础
Python之matplotlib基础 matplotlib是Python优秀的数据可视化第三方库 matplotlib库的效果可参考 http://matplotlib.org/gallery.ht ...
- swift app中展示折线图, 饼状图, 柱状图等数据图表
github 下载Charts-master SDK,该SDK有多种可自定义的图表样式 lineChart 正弦余弦线图 LineChart (cubic lines) LineChart (grad ...
- canvas制作柱形图/折线图/饼状图,Konva写动态饼状图
制作饼状图 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
随机推荐
- 基于委托的C#异步编程的一个小例子 带有回调函数的例子
我创建的是一个winform测试项目:界面如下: 设置: 下面是代码: using System; using System.Collections.Generic; using System.Com ...
- chrome 脚本学习
# 编写可复用的代码段(snippet)教程 https://jingyan.baidu.com/article/67508eb423d2929ccb1ce45b.html # chrome 脚本开发 ...
- socket 통신
00.C# --> Application.ThreadException --> Application.Run --> Container --> Application. ...
- plist 与 JOSN的解析
- Unix系统编程(六)write系统调用
write系统调用将数据写入一个打开的文件. ssize_t write(int fd, void *buffer, size_t count); write调用的参数含义与read调用相类似.buf ...
- 基于Redis实现延迟队列
背景 在后端服务中,经常有这样一种场景,写数据库操作在异步队列中执行,且这个异步队列是多进程运行的,这时如果对同一资源进行写库操作,很有可能产生数据被覆盖等问题,于是就需要业务层在更新数据库之前进行加 ...
- 利用:header匹配所有标题做目录
1.问题背景 查找到h1-h6,并遍历它们,打印出内容 2.实现源码 <!DOCTYPE html> <html> <head> <meta charset= ...
- android启动界面
/** * 应用程序启动类:显示欢迎界面并跳转到主界面 * <a href="http://my.oschina.net/arthor" target="_bl ...
- 获取openid
请将文件MP_verify_IbWggk5i4gWgrast.txt上传至mp.weixin.qq.com指向的web服务器(或虚拟主机)的目录 怎么处理的?//将txt文件上传到你填的域名的根目录 ...
- MPAndroidChart -- LimitLine的坑
好久没有写博客了,最近比较忙,项目中使用了MPAndroidChart,初次使用也比较蠢,很多API都得一个一个查,但有些时候当你想应该有这个API的时候他却没有,就比如今天的主角LimitLine ...