Could you please advise on how to solve next Matplotlib xticks ranges & labels issue I am facing with a couple of Python cases? I am trying to plot a couple of double charts (1 X 2) however, xticks ranges and labels showed are not in the way needed:
CASE 1) Starting point are 4 short dataframes as follow, after import libraries:
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
Code to plot 1x2 charts is as follow:
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()
and the output is: 1x2 charts plot CASE 1 At the x axis, I am trying to show a range of 7 points: 201807, 201808, 201809, 201810, 201811, 201812 and 201901…how to fix my Python code to produce such output?
CASE 2) Similar to CASE 1, CASE 2 uses 4 shorter dataframes as follow:
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
Plotting Python Code for 1x2 charts is:
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()
And output is: 1x2 charts plot CASE 2
At the x axis, I am trying to show a range of 3 points: 201902, 201903 and 201904… how to fix my Python code to produce such output?
Thank you in advance for support given.