累积百分比柱状图是一种展示数据累积分布情况的图表形式。它的优势包括:
-
可以直观显示数据的累积情况:通过累积百分比柱状图,可以清晰地看到数据随着不同维度的累积分布情况,有助于理解数据的全貌。
-
方便比较不同组别的数据比例:累积百分比柱状图可以将不同组别的数据直观地比较,帮助用户更好地理解数据之间的比例关系。
-
使得数据变化趋势更为明显:通过观察累积百分比柱状图,可以更容易地看出数据的变化趋势,从而进行分析和决策。
如下图所示:
代码如下所示:
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Times New Roman + SimSun'] # 字体采用新罗马加宋体
plt.rcParams['axes.unicode_minus'] = False # 正常显示负号
def cumulative_histogram_plot():
data_dict = {'A':[50, 52, 56, 58, 60, 61, 62],
'B':[20, 21, 22, 24, 25, 27, 29],
'C':[30, 27, 22, 18, 15, 12, 9]} # 百分比数据
x_list = range(2013, 2020)
columns = [f"{i}年" for i in x_list]
label_list = ['A', 'B', 'C']
fig = plt.figure(figsize=(10, 5))
marks = ['x', '..', '\\']
ax = fig.add_subplot(1, 1, 1)
plt.grid(axis='y', alpha=0.5, ls='--')
temp_v = np.linspace(0, 0, len(columns))
color_l = ['red', 'blue', 'green']
fs = 15
for i in range(len(label_list)):
plt.bar(x_list, height=data_dict[label_list[i]], bottom=temp_v, color=color_l[i], edgecolor='black', width=0.5,
label=label_list[i], hatch=marks[i])
temp_v = temp_v+data_dict[label_list[i]]
plt.ylim(0, 105)
plt.xlabel('年份', fontsize=fs)
plt.ylabel('百分比/%', fontsize=fs)
plt.xticks(x_list, fontsize=fs)
plt.yticks(fontsize=fs)
plt.legend(loc='center', bbox_to_anchor=(0.5, 1.05), ncol=8, fontsize=fs)
plt.savefig("累积百分比柱状图.png", dpi=600, bbox_inches='tight')
plt.show()
if __name__ == "__main__":
cumulative_histogram_plot()