一、柱形图系列

(一)单系列柱形图

from pyecharts.charts import Bar
from pyecharts import options as opts
import pandas as pd

# 读取数据
data = pd.read_excel(r'C:\Users\仁义\Desktop\data.xlsx',sheet_name='Sheet1')
areas = data['地区']
# print(type(areas))areas的数据类型为<class 'pandas.core.series.Series'>
cash_expense = data['现金消费支出Y']
wage_income = data['工资性收入X1']
other_income = data['其他收入X2']
areas_list = list(areas)# 将areas转换为list
cash_expense_list = list(cash_expense)# 将cash_expense转换为list
wage_income_list = list(wage_income) # 将wage_income转换为list
other_income_list = list(other_income) # 将other_income转换为list
#绘制图形对象
chart = (Bar()
        .add_xaxis(areas_list) #添加X轴索引
        .add_yaxis("各省现金消费", #"省份"为系列标题
                   yaxis_data=cash_expense_list,#Y轴数据
                   category_gap="50%",#单系列柱间距离
                  ) #单系列数据及修饰
        .set_global_opts(title_opts=opts.TitleOpts(title="柱形图示例一", subtitle="各省现金消费支出与两类支出对比"))
         
        )

chart.render('柱形图示例1.html')

(二)双系列柱形图

from pyecharts.charts import Bar
from pyecharts import options as opts
import pandas as pd

# 读取数据
data = pd.read_excel(r'C:\Users\仁义\Desktop\data.xlsx',sheet_name='Sheet1')
areas = data['地区']
# print(type(areas))areas的数据类型为<class 'pandas.core.series.Series'>
cash_expense = data['现金消费支出Y']
wage_income = data['工资性收入X1']
other_income = data['其他收入X2']
areas_list = list(areas)# 将areas转换为list
cash_expense_list = list(cash_expense)# 将cash_expense转换为list
wage_income_list = list(wage_income) # 将wage_income转换为list
other_income_list = list(other_income) # 将other_income转换为list
# 绘制图形对象
chart = (Bar() # 图例对象
        .add_xaxis(areas_list) # 添加X轴索引
        .add_yaxis("各省现金消费", # "省份"为系列标题
                   yaxis_data=cash_expense_list,#Y轴数据
                   category_gap='0%',# 不同系列柱间距离
                  ) 
         .add_yaxis('工资性收入',
                   yaxis_data=wage_income_list,
                   category_gap='0%',# 不同系列柱间距离
                   )
        .set_global_opts(title_opts=opts.TitleOpts(title="柱形图示例1", subtitle="各省现金消费支出与两类支出对比"))
        )

chart.render('柱形图示例2.html')