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绘图封装类之折线图、条状图、圆饼图的更多相关文章

  1. python 中matplotlib 绘图

    python 中matplotlib 绘图 数学建模需要,对于绘图进行简单学习 matpoltlib之类的包安装建议之间用anaconda 绘制一条y=x^2的曲线 #比如我们要绘制一条y=x^2的曲 ...

  2. HighCharts之2D堆条状图

    HighCharts之2D堆条状图 1.HighCharts之2D堆条状图源码 StackedBar.html: <!DOCTYPE html> <html> <head ...

  3. HighCharts之2D条状图

    HighCharts之2D条状图 1.HighCharts之2D条状图源码 bar.html: <!DOCTYPE html> <html> <head> < ...

  4. Excel 2010高级应用-条状图(五)

    Excel 2010高级应用-条状图(五) 基本操作如下: 1.新建一个Excel空白文档,并命名条状图 2.单击"插入",找到条状图的样图 3.选择其中一种类型的条状图样图,在空 ...

  5. Flex实现双轴条状图

    1.问题背景 一般的,柱状图可以实现双轴图,但是如何实现双轴条状图? 2.实现实例 <?xml version="1.0" encoding="utf-8" ...

  6. python中matplotlib画折线图实例(坐标轴数字、字符串混搭及标题中文显示)

    最近在用python中的matplotlib画折线图,遇到了坐标轴 "数字+刻度" 混合显示.标题中文显示.批量处理等诸多问题.通过学习解决了,来记录下.如有错误或不足之处,望请指 ...

  7. python之matplotlib绘图基础

    Python之matplotlib基础 matplotlib是Python优秀的数据可视化第三方库 matplotlib库的效果可参考 http://matplotlib.org/gallery.ht ...

  8. swift app中展示折线图, 饼状图, 柱状图等数据图表

    github 下载Charts-master SDK,该SDK有多种可自定义的图表样式 lineChart 正弦余弦线图 LineChart (cubic lines) LineChart (grad ...

  9. canvas制作柱形图/折线图/饼状图,Konva写动态饼状图

    制作饼状图 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

随机推荐

  1. 扩展方法 DataTable的ToList<T>

    using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.T ...

  2. Ubuntu13.04使用Jlink和ST-Link V2烧写STM32F1x和STM32F4x

    近期做研究openpilot和crazyflie2.0,都须要在Linux进行代码的编写和烧录,所以研究了下Linux下如何使用Jlink,还有开源的支持多个仿真器的openocd,可是困难重重.总是 ...

  3. PHP中的__call和__callStatic方法(未看完)

    如何防止调用不存在的方法而出错,使用__call魔术重载方法. __call方法原型如下: mixed __call(string $name,array $arguments) 当调用一个不可访问的 ...

  4. 安装inkscape for mac注意事项

    今天为了安装inkscape for mac,搞了一下午.按以前的方法,先安最新的XQuartz,再安最新的inkscape,在运行inkscape前先打开XQuartz.但是发现不行了,inksca ...

  5. window下安装Node.js NPM

    一.安装Node.js 下载地址:http://nodejs.org/download/ 1..msi文件,直接安装,包括了npm,结束. 2..exe文件,把node.exe所在主目录,加入到系统P ...

  6. Unity 扩展编辑器

    扩展Inspector界面 继承自Editor,添加CustomEditorAttribute,传入定制的类型 定制显示的类型要求: 类型中所有的public 字段都会自动暴露给Inspector编辑 ...

  7. 老司机的应用级监控——spring?actuator

    http://mt.sohu.com/20160824/n465783118.shtml ************************************************ 1什么是sp ...

  8. 广告过滤神器(ADMuncher)4.93

    Ad Muncher 介绍:        浏览网页时,冷不防地被网站播放的MIDI音乐声音吓一跳,或是因为弹出的广告窗口碍事,而影响你上网络的心情.Ad Muncher支持Netscape.Inte ...

  9. RandomUser 网站介绍

    RandomUser 网站介绍 tools api 介绍 使用 结果 API 错误 请求多个用户 指定性别 密码 种子 格式 使用早期版本 国家 页码 包含/不包含字段 杂项 结束语 介绍 在 201 ...

  10. JAVA转化Unicode编码

    package yyl.example.basic.codec; import java.util.Locale; import java.util.regex.Matcher; import jav ...