您能否就如何解决我在几个 Python 案例中面临的下一个 Matplotlib xticks 范围和标签问题提出建议?我正在尝试绘制几个双图表(1 X 2),但是,xticks 范围和显示的标签不是所需的方式:
案例 1)在导入库之后,起点是 4 个短数据帧,如下所示:
ye_mo_sales_Trial:
YearMo TotSales
0 201807 296.8
1 201808 255.5
2 201809 225.2
3 201810 204.5
4 201811 245.3
5 201812 267.3
ye_mo_sales_Trial.dtypes
YearMo int64
TotSales float64
dtype: object
ye_mo_sales_Control:
YearMo TotSales
0 201807 290.7
1 201808 285.9
2 201809 228.6
3 201810 185.7
4 201811 211.6
5 201812 279.8
6 201901 177.5
ye_mo_sales_Control.dtypes
YearMo int64
TotSales float64
dtype: object
ye_mo_cust_Trial:
YearMo TotCust
0 201807 55
1 201808 48
2 201809 44
3 201810 38
4 201811 44
5 201812 49
6 201901 39
ye_mo_cust_Trial.dtypes
YearMo int64
TotCust int64
dtype: object
ye_mo_cust_Control:
YearMo TotCust
0 201807 54
1 201808 50
2 201809 45
3 201810 36
4 201811 41
5 201812 50
6 201901 35
ye_mo_cust_Control.dtypes
YearMo int64
TotCust int64
dtype: object
绘制 1x2 图表的代码如下:
Trial = 77
Control = 233
fig, ax = plt.subplots(1, 2, figsize=(12, 6))
labelTrial=("Trial Store: "+str(Trial))
labelControl=("Control Store: "+str(Control))
ax[0].plot(ye_mo_sales_Trial["YearMo"], ye_mo_sales_Trial["TotSales"], color = "b", label=labelTrial)
ax[0].plot(ye_mo_sales_Control["YearMo"], ye_mo_sales_Control["TotSales"], color = "g", label=label Control)
ax[1].plot(ye_mo_cust_Trial["YearMo"], ye_mo_cust_Trial["TotCust"], color = "b", label=labelTrial)
ax[1].plot(ye_mo_cust_Control["YearMo"], ye_mo_cust_Control["TotCust"], color = "g", label=labelControl)
ax[0].set_xlabel("Year-Month")
ax[0].set_ylabel("Total Sales")
ax[0].set_title("Trends based on Total Sales")
ax[0].legend()
ax[1].set_xlabel("Year-Month")
ax[1].set_ylabel("Total Customers")
ax[1].set_title("Trends based on Total Customers")
ax[1].legend()
plt.show()
输出是: 1x2 图表绘制案例 1 在 x 轴上,我试图显示 7 个点的范围:201807、201808、201809、201810、201811、201812 和 201901……如何修复我的 Python 代码以产生这样的输出?
CASE 2) 与 CASE 1 类似,CASE 2 使用 4 个较短的数据帧,如下所示:
ye_mo_sales_Trial2:
YearMo TotSales
0 201902 235.0
1 201903 278.5
2 201904 263.5
ye_mo_sales_Trial2.dtypes
YearMo int64
TotSales float64
dtype: object
ye_mo_sales_Control2:
YearMo TotSales
0 201902 244.0
1 201903 199.1
2 201904 158.6
ye_mo_sales_Control2.dtypes
YearMo int64
TotSales float64
dtype: object
ye_mo_cust_Trial2:
YearMo TotCust
0 201902 45
1 201903 55
2 201904 48
ye_mo_cust_Trial2.dtypes
YearMo int64
TotCust int64
dtype: object
ye_mo_cust_Control2:
YearMo TotCust
0 201902 47
1 201903 41
2 201904 33
ye_mo_cust_Control2.dtypes
YearMo int64
TotCust int64
dtype: object
为 1x2 图表绘制 Python 代码是:
Trial = 77
Control = 233
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
labelTrial=("Trial Store: "+str(Trial))
labelControl=("Control Store: "+str(Control))
ax1.plot(ye_mo_sales_Trial2["YearMo"], ye_mo_sales_Trial2["TotSales"], color = "b", label=labelTrial)
ax1.plot(ye_mo_sales_Control2["YearMo"], ye_mo_sales_Control2["TotSales"], color = "g", label=labelControl)
ax2.plot(ye_mo_cust_Trial2["YearMo"], ye_mo_cust_Trial2["TotCust"], color = "b", label=labelTrial)
ax2.plot(ye_mo_cust_Control2["YearMo"], ye_mo_cust_Control2["TotCust"], color = "g", label=labelControl)
ax1.set_xlabel("Year-Month")
ax1.set_ylabel("Total Sales")
ax1.set_title("Trends based on Total Sales")
ax1.legend()
ax2.set_xlabel("Year-Month")
ax2.set_ylabel("Total Customers")
ax2.set_title("Trends based on Total Customers")
ax2.legend()
plt.show()
输出为: 1x2 图表绘制案例 2
在 x 轴上,我试图显示 3 个点的范围:201902、201903 和 201904……如何修复我的 Python 代码以产生这样的输出?
预先感谢您的支持。