Python 条形图只显示一种颜色
Python bar graph only shows one color
我有这个数据集(您可以将其用作 .csv/.txt 文件)
First name, Final,Grade
Aloysius, 49.0, D-
University, 48.0, D+
Gramma, 44.0, C
Electric, 47.0, B-
Fred, 45.0, A-
Betty, 46.0, C-
Cecil, 43.0, F
Bif, 50.0, B+
Andrew, 83.0, A
Jim, 97.0, A+
Art, 40.0, D+
Jim, 45.0, C+
Ima, 77.0, B-
Benny, 90.0, B-
Boy, 4.0, B
Harvey, 40.0, C
我运行这个:
import pandas as pd
import matplotlib.pyplot as plt
df['Grade'].value_counts().sort_values().plot(kind='barh', colormap='Reds')
这是输出:我指定了 colormap='Reds'
但它只显示一种颜色
我怎样才能做到这一点,一个连续的红色:
你必须为每个年级选择你自己的颜色:
import matplotlib.cm as cm
cmap = cm.get_cmap('Reds')
colors = cmap(np.linspace(0.1, 0.9, df['Grade'].nunique()))
ax = df['Grade'].value_counts('Grade', ascending=True).plot.barh(color=colors)
plt.show()
我有这个数据集(您可以将其用作 .csv/.txt 文件)
First name, Final,Grade
Aloysius, 49.0, D-
University, 48.0, D+
Gramma, 44.0, C
Electric, 47.0, B-
Fred, 45.0, A-
Betty, 46.0, C-
Cecil, 43.0, F
Bif, 50.0, B+
Andrew, 83.0, A
Jim, 97.0, A+
Art, 40.0, D+
Jim, 45.0, C+
Ima, 77.0, B-
Benny, 90.0, B-
Boy, 4.0, B
Harvey, 40.0, C
我运行这个:
import pandas as pd
import matplotlib.pyplot as plt
df['Grade'].value_counts().sort_values().plot(kind='barh', colormap='Reds')
这是输出:我指定了 colormap='Reds'
但它只显示一种颜色
我怎样才能做到这一点,一个连续的红色:
你必须为每个年级选择你自己的颜色:
import matplotlib.cm as cm
cmap = cm.get_cmap('Reds')
colors = cmap(np.linspace(0.1, 0.9, df['Grade'].nunique()))
ax = df['Grade'].value_counts('Grade', ascending=True).plot.barh(color=colors)
plt.show()