近期有针对pyecharts的批量制作图表的需求
ps:在已经熟悉了基础生成bar line图的基础上,仍需要一些样式上的要求,有些找了很久!特此记录一下!
需求一:要求subtitle 和 title 都居中显示,并且subtitle也【居中显示于title之下】
首先第一个需求其实很好满足的,可以通过设置pos_left/right来调整让【title和subtitle】一起移动,但是subtitle,我始终没有在TitleOpt中找到它的相关设置,pyecharts如何设置subtitle居中于title,也没在网上找到答案。
然后我就,直接搜“echarts——xxxxxx”,找到结果——>设置 textAlign:"center" 即可!pyecharts的TitleOpt中有相关设置,改代码!(ps:其实以后pyecharts搜索不到解决方法,echarts或许可以)
在titleopts源代码中:添加如下的两行代码
可以自己取名哈,重要的是self.opts中的 "textAlign":"<随你取名>" (限制)
再在你的代码中,添加这个opts就可以居中显示了。
需求二:让legend在标题的正下方显示通过Grid + legend.opts来设置。
·legnd的post_top:设置8%左右(具体根据你的图表调整)
但是此时你会发现你的legend跑到了图表的内部了,那么我们就需用通过grid来设置图的左右上下边距。
zhi
from pyecharts import options as opts from pyecharts.charts import Bar from pyecharts.charts import Grid bar=Bar(init_opts=opts.InitOpts()) #此处是你的一些配置 bar.add_xaxis(x_data) bar.add_yaxis() bar.set_series_opts() bar.set_global_opts() #省略 #添加grid配置 grid = Grid(init_opts=opts.InitOpts(width="900px",height="500px")) #将bar图添加到grid中 grid.add(bar,grid_opts=opts.GridOpts(pos_top="15%",width="1000px",height="600px"))
!!当添加grid设置后,Bar中的init就失效了!需要在Grid(中 配置你整个图表的大小!!)
而grid.add中的width和height,是指bar这个图表的大小,当然,你也可以添加bar1 bar2 bar3 barn,为它们设置图表大小和边距等
需求三:Line图中,legend的颜色和折现的颜色一致。from pyecharts.charts import Line
def Line(Line_color:sequence)
line1=Line(init_opts=opts.InitOpts())
#就这一行set_colors 即可
line1.set_colors(line_color)
#其余配置略
line_color则为你的折现的颜色,列表顺序为你line.add_yaxis() 的顺序,有多少条折线图就多长的列表!



