提出问题
我想利用pyecharts绘制双Y轴图像,但由于pycharts版本更新,类似这种博客教程的代码已经无法使用。
解决思路
在pyecharts的中文说明文档中,看到了这样一段说明文档:绘制双Y轴
解决办法
import pandas as pd
from pyecharts.charts import Bar,Line
from pyecharts import options as opts
from pyecharts import charts
import re
def getDate(date):
result=re.split(r'-',date)
theStr=result[0]+"年"+result[1]+"月"+result[2]+"日"
return theStr
def getListDistinct(theList):
result=[]
for item in theList:
if item not in result:
result.append(item)
return result
content=pd.read_excel('order(5).xls')
content2=pd.read_excel('order(4).xls')
title=content["球鞋名称"]
size=content["尺码"]
date=content["销售日期"]
price=content["价格"]
soldNum=content2["销量"]
# 不习惯链式调用的开发者依旧可以单独调用方法
bar = Bar()
line=Line()
print(getListDistinct(date))
bar.add_xaxis(getListDistinct(date))
sizeDistinct=getListDistinct(size)
for i in range(len(sizeDistinct)):
sizeSold=[]
for j in range(len(size)):
if sizeDistinct[i]==size[j]:
sizeSold.append(int(soldNum[j]))
#print(int(soldNum[i]))
print(type(sizeSold[0]))
bar.add_yaxis(str(sizeDistinct[i]),sizeSold)
#bar.set_global_opts(title_opts=opts.TitleOpts(title="销售数据"))
#bar.render()
bar.extend_axis(yaxis=opts.AxisOpts())
line.add_xaxis(getListDistinct(date))
for i in range(len(sizeDistinct)):
sizePrice=[]
for j in range(len(size)):
if sizeDistinct[i]==size[j]:
sizePrice.append(int(price[j])/100)
#print(int(soldNum[i]))
line.add_yaxis(str(sizeDistinct[i]),sizePrice,yaxis_index=1)
bar.overlap(line)
bar.render()
关键在于bar.extend_axis()
和line.add_yaxis(..,...,yaxis_index=1)
网友评论