from pyecharts.charts import Bar, Page
from pyecharts import options as opts
import pandas as pd
from pyecharts.components import Table
df = pd.read_csv('export.csv')
df_sorted = df.sort_values(by='rating', ascending=False)#评分降序
#转为列表,获取电影名称、评分、链接和其他信息
movies = df_sorted['title'].tolist()
ratings = df_sorted['rating'].tolist()
links = df_sorted['link'].tolist()
other_info = df_sorted[['type', 'country', 'year', 'director', 'time', 'actor']].values.tolist()
#构建 tooltip 格式,光标位置显示信息
tooltip_formatter = '''
{movie}
评分: {rating}
'''.format(
movie='{b}',
rating='{c}',
)
#创建柱状图对象
bar = (
Bar()
.add_xaxis(movies)
.add_yaxis("评分", ratings, label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="电影评分排行榜"),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-45)),
tooltip_opts=opts.TooltipOpts(formatter=tooltip_formatter),
yaxis_opts=opts.AxisOpts(min_=8),
datazoom_opts=[opts.DataZoomOpts(type_="slider", range_start=0, range_end=100)],
)
.set_series_opts(#标记线
markline_opts=opts.MarkLineOpts(
data=[
opts.MarkLineItem(type_="average", name="平均值"),
opts.MarkLineItem(type_="max", name="最大值"),
opts.MarkLineItem(type_="min", name="最小值"),
]
)
)
)
#创建表格数据
table_data = []
for i in range(len(movies)):
row = [movies[i], ratings[i], links[i]] + other_info[i]
table_data.append(row)
#创建表格对象
table = (
Table()
.add(["电影名称", "评分", "链接", "类型", "国家", "年份", "导演", "时长", "演员"], table_data)
.set_global_opts(
title_opts=opts.ComponentTitleOpts(title="电影信息表格"),
)
)
#生成HTML文件
page = Page()
page.add(bar,table)
page.render("Movie.html")