from pyecharts import Bar,Pie,Boxplot,Line,Radar,Scatter,Grid,Overlap
'''
1.创建对量
2.准备横轴数据(坐标)
3.准备纵轴数据
4.条用add("标题",2,3)#这个方法是重点
5.render()
'''
'\n1.创建对量\n2.准备横轴数据(坐标)\n3.准备纵轴数据\n4.条用add("标题",2,3)\n5.render()\n'
#设置行名
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]
#设置柱状图的主0标题与副标题
bar = Bar("柱状图","一年的降水量与蒸发量")
#添加柱状图的数据集配置
bar.add("降水量",columns,data1,mark_line=["average"],mark_port=["max","min"])
bar.add("蒸发量",columns,data2,mark_line=['average'],mark_port=["max","min"])
#生成本地文件(默认为.html)
bar.render()
bar
#饼图
pie = Pie('饼图',"一年的降水量与蒸发量",title_pos="center",width=900)
pie.add("降水量",columns,data1,center=[25,50],is_lengend_show=False)
pie.add('蒸发量',columns,data2,center=[75,50],is_lengend_show=False,is_label_show=True)
pie.render()
pie
#箱体图
boxplot=Boxplot("箱体图","一年的降水量与蒸发量")
x_axis = ["降水量","蒸发量"]
#prepare_data方法可以将数据转为嵌套的[min,Q1,median(or Q2),Q3,max]
y_axis=[data1,data2]
yaxis = boxplot.prepare_data(y_axis)
boxplot.add("天气统计",x_axis,yaxis)
boxplot.render()
boxplot
#折现统计图
line = Line("折线图","一年的降水量与蒸发量")
line.add("降水量",columns,data1,is_label_show=True)
line.add("蒸发量",columns,data2,is_label_show=True)
line.render()
line
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)
#区分颜色
radar.add("蒸发量",radar_data2,item_color="#1C86EE")
radar.render()
radar
scatter=Scatter("散点图","一年的降水量与蒸发量")
#xais_name是设置坐标轴名称,这里由于显示问题,还需要将y轴名称与y轴的距离进行设置
scatter.add("降水量与蒸发量的散点图分布图",data1,data2,xaxis_name="降水量",yaxis_name="蒸发量",yaxis_name_gap=40)
scatter.render()
scatter
#图标布局
#设置折线图标题位置
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()
grid
#两图合并
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()
overlap
参考:https://www.jiqizhixin.com/articles/2018-08-16-6