修改下列代码,使得最后得到的图中每个类别的颜色都不一样:import pandas as pd import matplotlib.pyplot as plt # 将 Release Date 列转化为年份格式 df['year'] = pd.to_datetime(df['Release Date']).dt.year df['Genre'] = df['Genre'].apply(lambda x: eval(x)) genre_counts = df.groupby('year')['Genre'].apply(lambda x: pd.Series(x).value_counts(normalize=True)).unstack(fill_value=0) genre_counts.plot(kind='bar', stacked=True, figsize=(10, 6)) plt.title('Genre Distribution Over the Years') plt.xlabel('Year') plt.ylabel('Frequency') ax.set_xlim(df['year'].min(), 2021) plt.legend(loc='upper left', bbox_to_anchor=(1.05, 1)) plt.show()
时间: 2023-09-06 13:13:13 浏览: 101
pycharm使用matplotlib.pyplot不显示图形的解决方法
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# 将 Release Date 列转化为年份格式
df['year'] = pd.to_datetime(df['Release Date']).dt.year
df['Genre'] = df['Genre'].apply(lambda x: eval(x))
# 利用 seaborn 调色板来设置颜色
sns.set_palette("husl")
genre_counts = df.groupby('year')['Genre'].apply(lambda x: pd.Series(x).value_counts(normalize=True)).unstack(fill_value=0)
genre_counts.plot(kind='bar', stacked=True, figsize=(10, 6))
plt.title('Genre Distribution Over the Years')
plt.xlabel('Year')
plt.ylabel('Frequency')
ax.set_xlim(df['year'].min(), 2021)
plt.legend(loc='upper left', bbox_to_anchor=(1.05, 1))
plt.show()
阅读全文