一直想用matplotlib来画用图片填充的柱形图,但是,摸索了好久都没找到,最后只能转战pyecharts了。
好在pyecharts还是可以实现了,先放上pyecharts的样图:
接下来就是照葫芦画瓢了。
1. 条形图
# 颜色转换
def RGB_to_Hex(tmp):
rgb = tmp.split(',')#将RGB格式划分开来
strs = '#'
for i in rgb:
num = int(i)#将str转int
#将R、G、B分别转化为16进制拼接转换并大写
strs += str(hex(num))[-2:].replace('x','0').upper()
return strs
def pic_bar_horizon(symbol,values,label,formatter,filename):
# 调用symbol的json文件
with open("symbol.json", "r", encoding="utf-8") as f:
symbols = json.load(f)
#作图
pictorialbar=PictorialBar()
pictorialbar.add_xaxis(label)
pictorialbar.add_yaxis("",
values,
label_opts=opts.LabelOpts(position="right",formatter="{c}"+formatter,font_size=18),
symbol_size=50,
symbol_repeat='fixed',
symbol_offset=[0,0],
is_symbol_clip=True,
symbol='path://'+symbols[symbol],
color=RGB_to_Hex('75,172,198'),
)
pictorialbar.reversal_axis()
pictorialbar.set_global_opts(
xaxis_opts=opts.AxisOpts(is_show=False),
yaxis_opts=opts.AxisOpts(
axislabel_opts=opts.LabelOpts(font_size=18),
axistick_opts=opts.AxisTickOpts(is_show=False),
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(opacity=0))))
pictorialbar.render()
make_snapshot(snapshot, pictorialbar.render(),filename)
def pic_bar_horizon_ex():
label = ["保留月薪", "实际月薪","平均月薪","总体"]
values = [5616.34255978742,3777.4339158062,2345.75534234,5646.98876]
values=[format(i,'.2f') for i in values]
symbol,filename,formatter='money',"pictorialbar_horizon.png","元"
pic_bar_horizon(symbol,values,label,formatter,filename)
# 调用
pic_bar_horizon_ex()
2.柱状图
def pic_bar_vertical(symbol,values,label,formatter,filename):
# 调用symbol的json文件
with open("symbol.json", "r", encoding="utf-8") as f:
symbols = json.load(f)
#作图
pictorialbar=PictorialBar()
pictorialbar.add_xaxis(label)
pictorialbar.add_yaxis("",
values,
label_opts=opts.LabelOpts(position="top",formatter="{c}"+formatter,font_size=18),
symbol_size=50,
symbol_repeat='fixed',
symbol_offset=[0,0],
is_symbol_clip=True,
symbol='path://'+symbols[symbol],
color=RGB_to_Hex('75,172,198'),
)
pictorialbar.set_global_opts(
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(font_size=18),
axistick_opts=opts.AxisTickOpts(is_show=False),
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(opacity=0))),
yaxis_opts=opts.AxisOpts(is_show=False)
)
pictorialbar.render()
make_snapshot(snapshot, pictorialbar.render(),filename)
def pic_bar_vertical_ex():
label = ["保留月薪", "实际月薪","平均月薪","总体"]
values = [5616.34255978742,3777.4339158062,2345.75534234,5646.98876]
values=[format(i,'.2f') for i in values]
symbol,filename,formatter='dog',"pictorialbar_vertical.png",""
pic_bar_vertical(symbol,values,label,formatter,filename)
# 调用
pic_bar_vertical_ex()
自定义设定symbol的话,根据pyecharts文档,可以用矢量图的路径:
其中,矢量图的路径的获取方式可以是:
①在Iconfont中,检索相应的icon;
②登录,点开相应的图标;
③右键-检查,就会出现该矢量图的路径:
然后,自己造一个symbol的json文件,之后随意添加并调用就可以了,类似于:
{
"dog": "M992 192h-128l-14.32-28.62A64 64 0 0 0 792.44 128H685.2l-54.56-54.56C610.46 53.28 576 67.56 576 96.06v299.68l256 91.42V416h64c70.7 0 128-57.3 128-128v-64c0-17.68-14.32-32-32-32z m-224 96c-17.68 0-32-14.32-32-32s14.32-32 32-32 32 14.32 32 32-14.32 32-32 32zM192 448c-35.28 0-64-28.72-64-64 0-35.34-28.66-64-64-64S0 348.66 0 384c0 83.32 53.66 153.7 128 180.2V992c0 17.68 14.32 32 32 32h128c17.68 0 32-14.32 32-32V768h320v224c0 17.68 14.32 32 32 32h128c17.68 0 32-14.32 32-32V555.1L532.1 448H192z",
"money":"M35.4432 817.92C51.2 807.3984 239.9104 650.112 250.3936 637.0048c13.1072-10.4832 26.2272-10.4832 26.2272-10.4832h314.56s49.8176 5.248 49.8176 47.1808c0 26.2144-18.3552 52.48-49.8176 57.6768-26.2144 5.248-91.7504 10.4832-138.9312 13.1072-15.7312 0 0 10.4832 0 10.4832l60.3008 39.3216h209.7152s86.5024-60.288 141.5552-99.6096c91.7376-60.3008 125.824 0 125.824 0s10.4832 20.9664 0 34.0736C939.84 770.7008 764.16 917.504 751.0912 928s-28.8256 5.248-28.8256 5.248H360.4992a90.56 90.56 0 0 0-47.1808 18.3424s-57.6768 47.1936-70.784 57.6768c-28.8384 20.9664-44.5696 0-44.5696 0l-162.56-165.12s-15.7312-15.7184 0-26.2144z m754.9696-511.2192c0 165.12-133.696 301.4656-301.4656 301.4656S187.4816 474.4832 187.4816 306.7008 321.1776 5.2352 488.96 5.2352s301.4528 133.696 301.4528 301.4656zM640.9984 146.8032c7.8592-15.7312 0-28.8384-15.7312-39.3216-18.3552-10.496-34.0864-7.872-47.1936 10.4832L486.4 251.6608l-97.0624-131.072C376.2304 104.8576 363.1232 99.6096 344.768 107.52s-23.5904 20.9664-15.7312 41.9328l94.3744 131.072H347.392c-7.8592 7.872-13.1072 13.1072-13.1072 23.6032s5.248 18.3424 13.1072 23.5904h99.6096v36.6976H347.392c-10.4832 5.248-13.1072 13.1072-13.1072 23.5904 2.56 10.4832 5.248 18.3552 13.1072 23.6032h99.6096v96.9856c-2.56 26.2144 10.4832 41.9456 36.6976 41.9456s39.3344-13.1072 39.3344-41.9456v-97.024h99.6096q15.7312-7.872 15.7312-23.6032t-15.7312-23.5904h-99.6096V325.12h99.6096c10.4832-7.872 15.7312-13.1072 15.7312-23.5904s-5.248-18.3552-15.7312-23.5904h-76.0192l94.3744-131.072z m0 0"
}