【问题标题】:How can I make this into a better graph?我怎样才能把它变成一个更好的图表?
【发布时间】:2020-11-06 19:01:54
【问题描述】:

我需要帮助将这个图表转换成更美观和可读的东西吗?任何提示/帮助将不胜感激!

plt.figure()
plt.plot(df_date_count.index, df_date_count["customer_id"], c="black", label = "Customer ID")
plt.plot(df_date_count.index, np.linspace(trans_vol,trans_vol,n_points), c="r", label = "Mean transaction volume")
plt.title("ANZ Transaction Volume vs. Date")
plt.xlabel("Date")
plt.ylabel("Number of customers")
plt.legend()
plt.tight_layout()[enter image description here][1]

【问题讨论】:

    标签: python pandas numpy matplotlib data-science


    【解决方案1】:

    我认为您的抱怨是关于 x 轴的刻度和标签。正如另一个答案中提到的,您可以使用 ma​​tplotlib 中的 dates

    from matplotlib import dates as mdates
    

    查看以下链接了解更多详情:

    https://matplotlib.org/stable/api/dates_api.html#matplotlib.dates.ConciseDateFormatter

    这是一个代码 sn-p,您可以在设置绘图配置的位置添加:

    #--------------------------------------
    #Feel free to try different options
    #--------------------------------------
    #locator = mdates.AutoDateLocator()
    locator = mdates.MonthLocator()
    
    formatter = mdates.ConciseDateFormatter(locator)
    ax.xaxis.set_major_locator(locator)
    ax.xaxis.set_major_formatter(formatter)
    

    【讨论】:

      【解决方案2】:

      您可以尝试更改滴答声的间隔

      from matplotlib.dates import MonthLocator, DateFormatter
      import matplotlib.pyplot as plt
      
      fig, ax = plt.subplots()
      ax.plot(x,y) #your data
      ax.xaxis.set_major_locator(MonthLocator(interval=3))
      

      或者可能是轮换:

      ax.xaxis.set_tick_params(labelrotation=45)
      

      甚至日期格式:

      ax.xaxis.set_major_formatter(DateFormatter('%m'))
      

      或字体设置等..使图更大

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-28
        • 1970-01-01
        • 1970-01-01
        • 2018-03-26
        • 2021-12-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多