[英]How do I change the order of the bars on this bar chart?
我正在使用 Seaborn 的计数 plot function 到 plot 繁荣评级。
用于创建 plot 的代码:
plt.figure(figsize = [10, 8])
sb.countplot(data = clean_loan_data, x = 'ProsperRating', color = base_color);
plt.xlabel('Prosper Rating')
plt.ylabel('Count')
plt.title('Prosper Rating Counts');
我要解决的问题是条的顺序。 目前,顺序为 A、D、B、E、C、AA 和 HR。 为什么这是固定顺序,我该如何更改? 为了清楚起见,我想移动每个条及其 label。 需要的顺序是 AA、A、B、C、D、E 和 HR。
您可以通过order=
参数设置订单。 如果您还想分配单独的 colors,请使用palette=
:
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np
ratings = ['AA', 'A', 'B', 'C', 'D', 'E', 'HR']
colors = ['crimson', 'limegreen', 'dodgerblue', 'gold', 'blueviolet', 'darkturquoise', 'fuchsia']
samples = np.repeat(ratings, np.random.randint(10, 100, len(ratings)))
np.random.shuffle(samples)
clean_loan_data = pd.DataFrame({'ProsperRating': samples})
plt.figure(figsize=[10, 8])
sns.countplot(data=clean_loan_data, x='ProsperRating', palette=colors, order=ratings)
plt.xlabel('Prosper Rating')
plt.ylabel('Count')
plt.title('Prosper Rating Counts')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.