0

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.

3 Answers3

0

The xlim() feature from the matplotlib library allows you to explicitly define your limits for the x-axis. See the documentation here.

For CASE 2, try adding:

plt.xlim(201902, 201904)

Similarly for CASE 1, we can do the same thing. However, since it appears that dates are being used as int values, you will get values in the range that don't correspond to an actual year/month (e.g. 201813, 201814, etc.). So, you can try re-formatting the data to date/month values or you could try making a break in the x-axis of the graph to solely display the year/month values. At minimum, try adding the following line of code in before plt.show():

plt.xlim(201807, 201901)

I found this question that may help you with adding a break in the x-axis for CASE 1. Also, take a look at this question as an alternative solution where you would explicitly assign the xticks.

Paul
  • 137
  • 4
0

Thank you paulitician27 unfortunately your recommendation for CASE 2 didn't work.

For CASE 2 I added the code line you recommended without successs. My new code incluiding your recommendation for your revision is as follow:

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.xlim(201902, 201904)
plt.show()

For CASE 2 it produced same 1X2 chats I did before include your line code:

CASE 2 1X2 Charts with new line code

Any other suggestion you may have for CASE 2?

0

For CASE 2, I changed data type of the 4 dataframes in column YearMo from int64 to datetime64, so they look as:

YearMo datetime64[ns]
TotSales float64
dtype: object

I also changed Python code to plot the 1x2 charts as follow:

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_Trial2["YearMo"], ye_mo_sales_Trial2["TotSales"], color = "b", label=labelTrial)
ax[0].plot(ye_mo_sales_Control2["YearMo"], ye_mo_sales_Control2["TotSales"], color = "g", label=labelControl)
ax[1].plot(ye_mo_cust_Trial2["YearMo"], ye_mo_cust_Trial2["TotCust"], color = "b", label=labelTrial)
ax[1].plot(ye_mo_cust_Control2["YearMo"], ye_mo_cust_Control2["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()

I was expecting produce same acceptable result as I reached with CASE 1, however, xaxis shows more than 3 points range and overlapping:

1x2 Charts CASE 2 changed dtype & Coding similar to CASE 1

At the x axis, I am trying to show a range of 3 points: 2019-02, 2019-03 and 2019-04… how to fix my Python code to produce such output?

Thank you in advance for support given.