基础配置
-
安装pyecharts、pandas
pip install pycharts pip install pandas
-
导入参数配置库
import pyecharts.options as opts
柱状图配置
-
导入柱状图包
from pyecharts.charts import Bar
-
pandas读取excel数据
data=pd.read_excel("excel")
-
读取某列数据并转为list
#loc[:,""]第一个冒号前后填写行号如1:255表示读取1:255行 #loc[:,""]引号处填写列名 data.loc[:,'时间']=data.loc[:,'时间'].astype('str') #[::]中12位填写起止索引,第三位数字为1则列表为顺序,为-1则为倒序 dataTime = data.loc[:,'时间'][::].values.tolist()
-
实例化柱状图
bar=( Bar() #添加x轴数据 .add_xaxis(dataTime) #添加y轴数据 .add_yaxis("最高气温", #添加数据名 dataMax, #数据列表list label_opts=opts.LabelOpts(is_show=False), #是否显示数据标签 color="#1C86EE", #柱状图颜色 markpoint_opts=opts.MarkPointOpts(#添加最值数据标签点 data = [ opts.MarkPointItem( type_ = 'max', name = '最大值', ) ] ) )#若有多组数据显示可在此处添加多个.add_yaxis并进行类似设置 )
-
添加图像全局设置
其设置方法为实例化某opts如tittle_opts继承自opts.TitleOpts类并在此类中进行各种参数定义
下列代码中的提示框为下图所示数据框可以对其样式进行设置
bar.set_global_opts( title_opts=opts.TitleOpts( # 标题设置 title="2019气温年化图", #名称 subtitle="hhh", #副标题名称 subtitle_textstyle_opts=opts.TextStyleOpts( #此处设置字体字号 font_size=12, width=1.5, ), pos_right="10%", #位置 pos_top='2%' ),#隐藏X轴刻度 xaxis_opts = opts.AxisOpts( is_show = False, ),# 图例设置 legend_opts=opts.LegendOpts( pos_top='7%', pos_left="30%", ), # 提示框设置 tooltip_opts=opts.TooltipOpts( trigger='axis', axis_pointer_type='cross', ), # 纵轴配置 yaxis_opts=opts.AxisOpts( type_="value", name="摄氏度", position="left", min_=-600, max_=600, offset=10, axisline_opts=opts.AxisLineOpts( is_show=True, linestyle_opts=opts.LineStyleOpts( color="black", ) ), # 轴线对应标签 axislabel_opts=opts.LabelOpts( formatter="{value}", ), ) )
-
生成html文件
bar.render("./img.html")
-
成图效果
绘制折线图
line = Line()
line.add_xaxis(dataTime)
line.add_yaxis(
series_name="平均温度",
y_axis=dataAver,
color="#4b0101",
label_opts=opts.LabelOpts(
is_show=False,
),
# 线配置
linestyle_opts=opts.LineStyleOpts(
width=1.2,
color='#4b0101',
),
markpoint_opts = opts.MarkPointOpts(
data=[
opts.MarkPointItem(
type_ = "max",
name = "最大值"
),
opts.MarkPointItem(
type_ = "min",
name = "最小值"
),
]
)
)
line.render("./img2.html")
进行图像组合
all = bar.overlap(line) #靓图进行组合
aa = Grid(
init_opts=opts.InitOpts(
width="1500px", #画布的大小
height='650px',
)
)
aa.add(all,
grid_opts=opts.GridOpts(
pos_top="20%", #设置图相对画布边界的位置
pos_left="5%",
),
is_control_axis_index=True)
aa.render("./img3.html")