# 绘制折线图 line = ( Line() .add_xaxis(data['year'].tolist()) .add_yaxis("电影评分", data['rating'].tolist()) .set_global_opts( title_opts=opts.TitleOpts(title="电影评分趋势图"), tooltip_opts=opts.TooltipOpts(trigger="axis"), xaxis_opts=opts.AxisOpts(type_="category"), yaxis_opts=opts.AxisOpts(type_="value"), ) .render("rating_trend.html") ) 将其在notebook中显示
时间: 2023-06-25 17:04:34 浏览: 50
可以使用 `NotebookDisplay` 函数将生成的 HTML 文件嵌入到 Jupyter Notebook 中展示,具体代码如下:
```python
from pyecharts.render import NotebookRender
from pyecharts.charts import Line
from pyecharts import options as opts
notebook_render = NotebookRender()
Line().add_xaxis(data['year'].tolist()).add_yaxis("电影评分", data['rating'].tolist()).set_global_opts(
title_opts=opts.TitleOpts(title="电影评分趋势图"),
tooltip_opts=opts.TooltipOpts(trigger="axis"),
xaxis_opts=opts.AxisOpts(type_="category"),
yaxis_opts=opts.AxisOpts(type_="value"),
).render_notebook(notebook_render)
notebook_render.show()
```
运行上述代码后,就可以在 Jupyter Notebook 中看到电影评分趋势图的折线图了。
相关问题
# 统计每年的平均评分 data = pd.concat([dates_year, dates_ratings], axis=1) data.columns = ['year', 'rating'] data = data.groupby('year').mean().reset_index() # 绘制折线图 line = ( Line() .add_xaxis(data['year'].tolist()) .add_yaxis("电影评分", data['rating'].tolist()) .set_global_opts( title_opts=opts.TitleOpts(title="电影评分趋势图"), tooltip_opts=opts.TooltipOpts(trigger="axis"), xaxis_opts=opts.AxisOpts(name='上映年份',type_="category"), yaxis_opts=opts.AxisOpts(name='电影评分',type_="value"), datazoom_opts=[opts.DataZoomOpts(is_show=True,range_start=0,range_end=20,orient='horizontal')], # 添加区域缩放 ) # 缩放区域空值条所放的位置 ) line.render_notebook() 将其改为年份评分人数散点图
可以使用散点图来展示每年的评分和评分人数的关系,代码如下:
```python
# 统计每年的平均评分和评分人数
data = pd.concat([dates_year, dates_ratings, dates_votes], axis=1)
data.columns = ['year', 'rating', 'votes']
data = data.groupby('year').agg({'rating': 'mean', 'votes': 'sum'}).reset_index()
# 绘制散点图
scatter = (
Scatter()
.add_xaxis(data['year'].tolist())
.add_yaxis("电影评分", data['rating'].tolist(), symbol_size=10, label_opts=opts.LabelOpts(is_show=False))
.add_yaxis("评分人数", data['votes'].tolist(), symbol_size=10, label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="电影年份评分人数散点图"),
tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
xaxis_opts=opts.AxisOpts(name='上映年份', type_="category"),
yaxis_opts=[
opts.AxisOpts(name='电影评分', type_="value"),
opts.AxisOpts(name='评分人数', type_="value", position="right")
],
datazoom_opts=[opts.DataZoomOpts(is_show=True, range_start=0, range_end=20, orient='horizontal')]
)
)
scatter.render_notebook()
```
这样就可以得到一张电影年份评分人数散点图,其中横轴表示年份,左纵轴表示电影评分,右纵轴表示评分人数。
line = Line() line.add_xaxis(date_count['Year_Month'].dt.month.tolist()) line.add_yaxis('客流量',date_count['Count'].tolist(),is_smooth = True) line.set_global_opts(title_opts=opts.TitleOpts(title="折线图"), xaxis_opts=opts.AxisOpts(type_="category"), yaxis_opts=opts.AxisOpts(type_="value"),) # 显示图表 line.render_notebook(),找出这段代码数据错位的原因
这段代码的数据错位可能是因为 `date_count` 数据中的时间列 `Year_Month` 不是按照时间顺序排列的。可以尝试在读取数据时将时间列按照时间顺序排序,例如:
```
date_count = pd.read_csv('data.csv', parse_dates=['Year_Month'])
date_count = date_count.sort_values(by='Year_Month')
```
如果数据已经按照时间顺序排列,可以尝试检查 `date_count['Year_Month'].dt.month.tolist()` 是否正确返回每个时间点的月份。如果返回的不是预期的月份,可以尝试使用 `date_count['Year_Month'].dt.strftime('%Y-%m')` 将时间列转换成字符串格式,再从字符串中提取月份信息,例如:
```
line.add_xaxis(date_count['Year_Month'].dt.strftime('%Y-%m').tolist())
```