【问题标题】:Matplotlib xticks ranges & labels issueMatplotlib xticks 范围和标签问题
【发布时间】:2020-12-18 04:03:04
【问题描述】:

您能否就如何解决我在几个 Python 案例中面临的下一个 Matplotlib xticks 范围和标签问题提出建议?我正在尝试绘制几个双图表(1 X 2)但是,xticks 范围和显示的标签不是所需的方式:

CASE 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 charts plot CASE 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 charts plot CASE 2

在 x 轴上,我试图显示 3 个点的范围:201902、201903 和 201904……如何修复我的 Python 代码以产生这样的输出?

提前感谢您的支持。

【问题讨论】:

  • ...这些图片倒置有什么原因吗?

标签: python matplotlib xticks


【解决方案1】:

matplotlib 库中的 xlim() 功能允许您明确定义 x 轴的限制。请参阅文档here

对于CASE 2,尝试添加:

plt.xlim(201902, 201904)

对于CASE 1,我们可以做同样的事情。但是,由于日期似乎被用作 int 值,因此您将获得与实际年/月不对应的范围内的值(例如 201813、201814 等)。因此,您可以尝试将数据重新格式化为日期/月份值,或者您可以尝试在图表的 x 轴上中断以仅显示年份/月份值。至少,尝试在plt.show() 之前添加以下代码行:

plt.xlim(201807, 201901)

我发现 this question 可以帮助您在 CASE 1 的 x 轴上添加中断。另外,请查看 this question 作为替代解决方案,您可以在其中显式分配 xticks

【讨论】:

    【解决方案2】:

    感谢 paulitician27,很遗憾,您对 CASE 2 的建议没有奏效。

    对于 CASE 2,我添加了您推荐的代码行,但没有成功。我的新代码包括您对修订的建议如下:

    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()
    

    对于案例 2,它产生了与我之前所做的相同的 1X2 聊天,包括您的线路代码:

    CASE 2 1X2 Charts with new line code

    您对案例 2 有什么其他建议吗?

    【讨论】:

      【解决方案3】:

      对于案例 2,我将 YearMo 列中 4 个数据帧的数据类型从 int64 更改为 datetime64,因此它们看起来如下:

      YearMo datetime64[ns]
      TotSales float64
      dtype: object
      

      我还更改了 Python 代码以绘制 1x2 图表,如下所示:

      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()
      

      我期望产生与案例 1 相同的可接受结果,但是,xaxis 显示超过 3 个点范围和重叠:

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

      在 x 轴上,我试图显示 3 个点的范围:2019-02、2019-03 和 2019-04……如何修复我的 Python 代码以产生这样的输出?

      提前感谢您的支持。

      【讨论】:

        猜你喜欢
        • 2012-08-29
        • 2021-01-11
        • 2020-04-10
        • 2014-12-28
        • 1970-01-01
        • 2015-08-18
        • 2022-01-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多