首页 > 解决方案 > 缩放包含日期的 x 轴值 - 不显示日期(matplotlib)

问题描述

我正在尝试缩放包含日期的条形图的 x 轴。我也使用了set_major_locator()matplotlib 的功能。这是我的代码:

def plot_var(country):
    """
    Plots a bar chart of the given variable over the date range
    """
    
    y = confirmed_data[confirmed_data['Country_Region']==country]["TargetValue"]
    x = confirmed_data[confirmed_data['Country_Region']==country]['Date']
    plt.figure(figsize=(12,4))
    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
    plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=30))
    plt.title("Plot of \"ConfirmedCases\"",fontsize=18)
    plt.bar(x=x,height=y,color='blue')
    plt.grid(True)
    plt.xticks(fontsize=14,rotation=45)
    plt.yticks(fontsize=14)
    plt.show()

但是,我的图表在 x 轴上没有显示任何值。我不明白为什么会这样。希望你能帮我解决这个问题!

PS 我已经包含了相同的必要库(matplotlib.pyplot 和 matplotlib.dates)。

图形

标签: pythonmatplotlib

解决方案


推荐阅读