一个销量结果呈现方式改善的思考

2023-01-31  本文已影响0人  遥远的清平湾

问题

这个链接给出了21年、22年装载机国内市场销量变化的趋势和同比情况,原图如下:

原版图片

分析

这个图单看一个月的21年、22年对比还好,但想从整体上观察21年、22年逐月销量对比有点费力(比如想一眼看多个月的对比);同时同比曲线不能很好的反映出那个点是大于0的(从而不能很好的反映出哪些月份22年有优势)。

解决

基于上面的分析,改善如下:

图片

改善的画图有以下优点:

当然,上图的细节还可以进一步优化,如左纵坐标可以以千为单位,右纵坐标ticklabel加上%等,这里不再补充。

源代码

import matplotlib.pyplot as plt
import seaborn as sn
import numpy as np
import pandas as pd

# 构造数据
month = list(range(1, 13))
sale_21 = [6100, 5500, 18100, 15000, 12100, 9000, 6000, 6200, 6300, 6300, 6400, 6100]  # 21年销量
sale_22 = [4000, 5600, 11100, 8000, 7000, 7500, 5000, 5000, 5500, 6200, 10000, 5000]  # 22年销量
percent = [100 * (s2 - s1) / s1 for s1, s2 in zip(sale_21, sale_22)]
df = pd.DataFrame({'Sale': sale_21 + sale_22, 'Year': [2021] * 12 + [2022] * 12}, index=month + month)  # 纵向拼接

# 原始画图
plt.figure()
sn.barplot(data=df, x=df.index, y='Sale', hue='Year')
plt.xlabel('Month')
plt.grid(axis='y')
plt.show()

# 改善画图
fig, ax1 = plt.subplots()
p1, = ax1.plot(month, sale_21, '-o', label='2021')
p2, = ax1.plot(month, sale_22, '-o', label='2022')
ax1.set_ylabel('Sale')
ax1.set_xticks(month)
ax1.set_xlabel('Month')
ax1.grid(axis='y')
ax2 = ax1.twinx()
p3 = ax2.bar(x=month, height=percent, label='Year-on-year',
             color='None', edgecolor=['blue' if p < 0 else 'red' for p in percent])
ax2.set_ylabel('Year-on-year')
ax2.set_ylim([-100, 100])
plt.legend([p1, p2, p3], ['2021', '2022', 'Year-on-year'])
plt.tight_layout()
plt.show()
上一篇 下一篇

猜你喜欢

热点阅读