将下列代码重新定义df_com = df[['title','comments']].sort_values('comments',ascending=False) df_com = df_com[:10] _x = df_com['title'].tolist() _y = df_com['comments'].tolist() plt.plot(_x, _y) plt.title('网易云音乐华语歌单评论数 TOP10') plt.xlabel('歌单名称') plt.ylabel('评论数') plt.xticks(rotation=45, ha='right') plt.show()
时间: 2023-09-03 21:16:49 浏览: 177
这段代码使用 pandas 和 matplotlib 库对 DataFrame 进行操作,首先选取了 DataFrame 中的 'title' 和 'comments' 两列数据,按照 'comments' 列进行降序排列,选取前10行数据,然后将 'title' 和 'comments' 列的值分别转换为列表类型 _x 和 _y。接着,调用 matplotlib 库的 plot 函数生成一个折线图,并输出结果。
以下是完整的代码:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取数据
df = pd.read_csv('music.csv', encoding='utf-8')
# 对数据进行处理
df_com = df[['title','comments']].sort_values('comments',ascending=False)
df_com = df_com[:10]
_x = df_com['title'].tolist()
_y = df_com['comments'].tolist()
# 生成图表
plt.plot(_x, _y)
plt.title('网易云音乐华语歌单评论数 TOP10')
plt.xlabel('歌单名称')
plt.ylabel('评论数')
plt.xticks(rotation=45, ha='right')
plt.show()
```
相关问题
将下列代码重新定义df_col = df[['title','collection']].sort_values('collection',ascending=False)[2:12] _x = df_col['title'].tolist() _y = df_col['collection'].tolist() fig, ax = plt.subplots(figsize=(8, 8)) wedges, texts, autotexts = ax.pie(_y, labels=_x, autopct='%1.1f%%', startangle=90, colors=color[1:]) ax.set_title('网易云音乐华语歌单收藏 TOP10', loc='left', fontsize=16) plt.setp(autotexts, size=12, weight='bold') plt.setp(texts, size=12) plt.show()
这段代码使用 pandas 和 matplotlib 库对 DataFrame 进行操作,首先选取了 DataFrame 中的 'title' 和 'collection' 两列数据,按照 'collection' 列进行降序排列,选取第3至第11行数据,然后将 'title' 和 'collection' 列的值分别转换为列表类型 _x 和 _y。接着,调用 matplotlib 库的 pie 函数生成一个饼图,并输出结果。
以下是完整的代码:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取数据
df = pd.read_csv('music.csv', encoding='utf-8')
# 对数据进行处理
df_col = df[['title','collection']].sort_values('collection',ascending=False)[2:12]
_x = df_col['title'].tolist()
_y = df_col['collection'].tolist()
# 生成图表
fig, ax = plt.subplots(figsize=(8, 8))
wedges, texts, autotexts = ax.pie(_y, labels=_x, autopct='%1.1f%%', startangle=90, colors=color[1:])
ax.set_title('网易云音乐华语歌单收藏 TOP10', loc='left', fontsize=16)
plt.setp(autotexts, size=12, weight='bold')
plt.setp(texts, size=12)
plt.show()
```
df_play = df[['title','play']].sort_values('play',ascending=False) # print(df_play[:10]) df_play = df_play[:10] _x = df_play['title'].tolist() _y = df_play['play'].tolist() df_play = get_matplot(x=_x,y=_y,chart='barh',title='网易云音乐华语歌单播放 TOP10',ha='left',size=8,color=color[0]) print(df_play)将此代码重新定义完整
这段代码使用 pandas 库对 DataFrame 进行操作,首先选取了 DataFrame 中的 'title' 和 'play' 两列数据,按照 'play' 列进行降序排列,选取前10行数据,然后将 'title' 和 'play' 列的值分别转换为列表类型 _x 和 _y。最后,调用自定义函数 get_matplot 生成一个水平条形图,并输出结果。
以下是完整的代码:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 自定义函数,用于生成图表
def get_matplot(x, y, chart='bar', title='', ha='center', size=10, color='blue'):
plt.figure(figsize=(12, 6))
if chart == 'bar':
plt.bar(x, y, color=color)
elif chart == 'barh':
plt.barh(x, y, color=color)
plt.title(title, fontsize=size)
plt.xlabel('播放量', fontsize=size)
plt.ylabel('歌单名称', fontsize=size)
plt.xticks(fontsize=size)
plt.yticks(fontsize=size, ha=ha)
plt.grid(axis='x', linestyle='--', alpha=0.5)
plt.show()
# 读取数据
df = pd.read_csv('music.csv', encoding='utf-8')
# 对数据进行处理
df_play = df[['title','play']].sort_values('play',ascending=False)
df_play = df_play[:10]
_x = df_play['title'].tolist()
_y = df_play['play'].tolist()
# 生成图表
df_play = get_matplot(x=_x,y=_y,chart='barh',title='网易云音乐华语歌单播放 TOP10',ha='left',size=8,color='red')
```
阅读全文
相关推荐
















