pyecharts画图总结

pyecharts 画图归纳
将本地文件导入到Pyecharts:

test = open(filename, 'r')

data = test.readlines()

test.close()

如果遇到无法导入包的情况:

sudo pip install pyecharts == 0.1.9.4

再不行:

sudo apt - get install python3 - tk

pip3 install pyecharts

mysql文件导入Pycharm的代码

import pymysql

一页多图

from pyecharts import Page

导入柱状图Bar

from pyecharts import Bar

导入饼图Pie

from pyecharts import Pie

导入折线图Line

from pyecharts import Line

导入雷达图Radar

from pyecharts import Radar

导入散点图Scatter

from pyecharts import Scatter

导入词云图WordCloud

from pyecharts import WordCloud

将mysql的数据导入pycharm

db = pymysql.connect("要连接的主机地址localhost", "用于登录的数据库用户root", "密码strongs", "要连接的数据库名")
cursor = db.cursor()
sql = "select * from 表名"
try:
cursor.execute(sql)
data = cursor.fetchall()
except:
print("Error!")
db.close()

print(data)
x = [x[0] for x in data]
y = [x[1] for x in data]

page = Page()

柱状图-Bar

设置行名

columns = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

设置数据

data1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
data2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]

设置柱状图的主标题与副标题

bar = Bar("柱状图", "一年的降水量与蒸发量", title_color='red', width=1000)

添加柱状图的数据及配置项

bar.add("图标", 列名, 列高(数据), mark_line=["average"], mark_point=["max", "min"])

bar.add("降水量", columns, data1, mark_line=['max'], mark_point=["max", "min"], is_convert=False, area_color='yellow')
bar.add("蒸发量", columns, data2, mark_line=["average"], mark_point=["max", "min"], is_convert=False)

打印输出图表的所有配置项

bar.show_config()

生成本地文件(默认为.html文件)

bar.render('./bar.html')

page.add(bar)

饼图-Pie

设置主标题与副标题,标题设置居中,设置宽度为900

pie = Pie("饼状图", "一年的降水量与蒸发量", title_pos='center', width=900)

加入数据,设置坐标位置为【25,50】,上方的colums选项取消显示

pie.add("降水量", columns, data1, center=[25, 50], is_legend_show=True)

加入数据,设置坐标位置为【75,50】,上方的colums选项取消显示,显示label标签

pie.add("蒸发量", columns, data2, center=[75, 50], is_legend_show=False, is_label_show=True)

pie.show_config()

保存图表

pie.render('./pie.html')

page.add(pie)

折线图-Line

line = Line("折线图", "一年的降水量与蒸发量")

is_label_show是设置上方数据是否显示

line.add("降水量", columns, data1, is_label_show=True)
line.add("蒸发量", columns, data2, is_label_show=True)

line.render('./line.html')

page.add(line)

雷达图-Radar

radar = Radar("雷达图", "一年的降水量与蒸发量")

由于雷达图传入的数据得为多维数据,所以这里需要做一下处理

radar_data1 = [[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]]
radar_data2 = [[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]]

设置column的最大值,为了雷达图更为直观,这里的月份最大值设置有所不同

schema = [
("Jan", 5), ("Feb", 10), ("Mar", 10),
("Apr", 50), ("May", 50), ("Jun", 200),
("Jul", 200), ("Aug", 200), ("Sep", 50),
("Oct", 50), ("Nov", 10), ("Dec", 5)
]

传入坐标

radar.config(schema)
radar.add("降水量", radar_data1)

一般默认为同一种颜色,这里为了便于区分,需要设置item的颜色

radar.add("蒸发量", radar_data2, item_color="#1C86EE")

radar.render('./radar.html')

page.add(radar)

散点图-scatter

scatter = Scatter("散点图", "一年的降水量与蒸发量")

xais_name是设置横坐标名称,这里由于显示问题,还需要将y轴名称与y轴的距离进行设置

scatter.add("降水量与蒸发量的散点分布", data1, data2, xaxis_name="降水量", yaxis_name="蒸发量",
yaxis_name_gap=40)

scatter.render('./scatter.html')

page.add(scatter)

词云图-word_cloud

word_cloud = WordCloud(width=1300, height=620)
name = ['Sam S Club', 'Macys', 'Amy Schumer', 'Jurassic World', 'Charter Communications', 'Chick Fil A',
'Planet Fitness', 'Pitch Perfect', 'Express', 'Home', 'Johnny Depp', 'Lena Dunham', 'Lewis Hamilton', 'KXAN',
'Mary Ellen Mark', 'Farrah Abraham', 'Rita Ora', 'Serena Williams', 'NCAA baseball tournament', 'Point Break']
value = [10000, 6181, 4386, 4055, 2467, 2244, 1898, 1484, 1112, 965, 847, 582, 555, 550, 462, 366, 360, 282, 273, 265]
word_cloud.add("", name, value, word_size_range=[30, 100], shape='diamond')

word_cloud.show_config()

word_cloud.render()
page.add(word_cloud)

page.render('./all-plots.html')

图表布局

from pyecharts import Grid

设置折线图标题位置

line = Line("折线图", "一年的降水量与蒸发量", title_top="45%")
line.add("降水量", columns, data1, is_label_show=True)
line.add("蒸发量", columns, data2, is_label_show=True)
grid = Grid()

设置两个图表的相对位置

grid.add(bar, grid_bottom="60%")
grid.add(line, grid_top="60%")
grid.render()

结合不同类型图表叠加

from pyecharts import Overlap
overlap = Overlap()
bar = Bar("柱状图-折线图合并", "一年的降水量与蒸发量")
bar.add("降水量", columns, data1, mark_point=["max", "min"])
bar.add("蒸发量", columns, data2, mark_point=["max", "min"])
overlap.add(bar)
overlap.add(line)
overlap.render()

posted @ 2019-11-30 00:17  赵大暖  阅读(4949)  评论(0编辑  收藏  举报