#导入柱状图-Bar
from pyecharts import 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("柱状图", "一年的降水量与蒸发量")
#添加柱状图的数据及配置项
bar.add("降水量", columns, data1, mark_line=["average"], mark_point=["max", "min"])
bar.add("蒸发量", columns, data2, mark_line=["average"], mark_point=["max", "min"])
#生成本地文件(默认为.html文件)
bar.render("柱状图.html")
#导入饼图Pie
from pyecharts import Pie
#设置主标题与副标题,标题设置居中,设置宽度为900
pie = Pie("饼状图", "一年的降水量与蒸发量",title_pos='center',width=900)
#加入数据,设置坐标位置为【25,50】,上方的colums选项取消显示
pie.add("降水量", columns, data1 ,center=[25,50],is_legend_show=False,is_label_show=True)
#加入数据,设置坐标位置为【75,50】,上方的colums选项取消显示,显示label标签
pie.add("蒸发量", columns, data2 ,center=[75,50],is_legend_show=False,is_label_show=True)
#保存图表
pie.render("饼图.html")
#导入饼图Pie
from pyecharts import Pie
#设置主标题与副标题,标题设置居中,设置宽度为900
pie = Pie("饼状图", "一年的降水量与蒸发量",title_pos='center',width=900)
#加入数据,设置坐标位置为【25,50】,上方的colums选项取消显示
pie.add("降水量", columns, data1 ,center=[25,50],is_legend_show=False,is_label_show=True)
#加入数据,设置坐标位置为【75,50】,上方的colums选项取消显示,显示label标签
pie.add("蒸发量", columns, data2 ,center=[75,50],is_legend_show=False,is_label_show=True)
#保存图表
pie.render("饼图.html")
from pyecharts import 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("雷达图.html")
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("柱状图-折线图合并.html")
可以看到,折线图的原点无法落在柱状图的中间。那是由于我们的一个月份里面展示的有降雨量和蒸发量两个可变值。
所以接下来稍作改变,降雨量用柱状图表示,蒸发量用折线图表示,详细代码如下:
#导入柱状图-Bar
from pyecharts import Bar
from pyecharts import Overlap
from pyecharts import Line
#设置行名
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]
overlap = Overlap()
bar = Bar("柱状图-折线图合并", "一年的降水量")
line = Line()
bar.add("降水量", columns, data1, mark_point=["max", "min"])
line.add("蒸发量", columns, data2, is_label_show=True)
overlap.add(bar)
overlap.add(line)
overlap.render("一年降水量和蒸发量:柱状图-折线图合并.html")
from pyecharts import WordCloud
name = [
'Echarts', '图表制作', '炫酷', '牛逼', 'Python',
'编程', '简单', '方便', '快捷', 'Home', 'Johnny Depp',
'Lena Dunham', 'Lewis Hamilton', 'KXAN', 'Mary Ellen Mark', 'Farrah Abraham',
'中文![下载](/Users/xie/Desktop/1.png)']
value = [ 965, 847, 582, 555, 550, 462, 366, 360, 282, 273, 265, 123, 456, 155, 657, 457, 954]
wordcloud = WordCloud(width=1000, height=620)
wordcloud.add("", name, value, word_size_range=[20, 100])
wordcloud.render("中英文词云.html")
本文参考来源:https://www.jianshu.com/p/2655ca8ad459
pyecharts官方网站:https://pyecharts.org/#/zh-cn/intro