【问题标题】:Python matplotlib changing the major unit scale of the x-axisPython matplotlib改变x轴的主要单位比例
【发布时间】:2019-05-22 09:51:09
【问题描述】:

我有一个未在 x 轴上显示正确日期的图。可能是因为要显示的观察结果太多,导致没有显示任何观察结果。我不确定是否应该使用set_xticks 函数,但它给了我属性错误

AttributeError: 模块 'matplotlib.pyplot' 没有属性 'set_xticks'

我当前的代码是这样的:

def plot_w(dataframe,ticker,benchmark):

    # a. Printing highest observed values and corresponding date
    max1 = data_df.loc[:, ticker].max()
    max2 = data_df.loc[:, benchmark].max()
    max1_date = data_df[data_df[ticker] == max1]['Date'].values[0] 
    max2_date = data_df[data_df[benchmark] == max2]['Date'].values[0]
    print("The highest adjusted close price observed at: \n", ticker, ":", max1.round(2), "USD on the date ", max1_date, 
          "\n", benchmark, ":", max2.round(2), "USD on the date", max2_date)

    # b. Setting up plot based on dropdown input
    I = data_df.columns == ticker
    mpl_figure = dataframe.loc[:, ['Date',ticker,benchmark]]
    mpl_figure.plot(x='Date', y=[ticker,benchmark], style=['-b','-k'], figsize=(10, 5), fontsize=11, legend='true', linestyle = '-')
    plt.ylabel("USD",labelpad=5)
    plt.locator_params(axis='x', nbins=20)
    title = "Adjusted close prices for " + ticker + " and " + benchmark
    plt.title(title)
    plt.set_xticks(data_df['Date'].values) # Code fails here

# c. Creating the widget for the plot
widgets.interact(plot_w,
    dataframe = widgets.fixed(data_df),
    ticker = widgets.Dropdown(
            options=data_df.columns,
            value='ATVI',
            description='Company 1:',
            disabled=False,
        ),
    benchmark = widgets.Dropdown(
            options=data_df.columns,
            value='AAPL',
            description='Company 2:',
            disabled=False,
        )
)

这个图是这样的:

【问题讨论】:

  • 你忘了显示你在哪里使用set_xticks
  • @Sheldore 现已添加。

标签: python pandas matplotlib figure


【解决方案1】:

set_xticks 用于轴,如果您为图形设置刻度,则为

plt.xticks(data_df['Date'].values)

【讨论】:

    【解决方案2】:

    或者,您可以在处理轴对象的函数中尝试以下操作。这个想法是首先创建一个轴实例ax,然后将其传递给绘图命令。稍后使用它来设置 x-ticks。

    I = data_df.columns == ticker
    fig, ax = plt.subplots(figsize=(10, 5))
    mpl_figure = dataframe.loc[:, ['Date',ticker,benchmark]]
    mpl_figure.plot(x='Date', y=[ticker,benchmark], style=['-b','-k'], ax=ax, fontsize=11, legend='true', linestyle = '-')
    plt.ylabel("USD",labelpad=5)
    plt.locator_params(axis='x', nbins=20)
    title = "Adjusted close prices for " + ticker + " and " + benchmark
    plt.title(title)
    ax.set_xticks(data_df['Date'].values) 
    

    【讨论】:

    • 这对我来说很有意义,但是当我用建议的代码替换代码时,我得到了错误。 AttributeError: 'NoneType' object has no attribute 'update'
    • @BlackBear:没有a Minimal, Complete, and Verifiable example,人们将很难在这里帮助您。在某些时候,他们可能会放弃回答。因此,请花一些时间重新构建您的问题,包括重现错误和数字所需的一切。这样你就有更多的机会得到答案
    【解决方案3】:

    模块 matplotlib.pyplot 没有set_xticks 函数,而是一个xticks 函数。 (link to doc)。但是,类对象 matplotlib.axes.Axes 确实有 set_xticks (link to doc)。

    因此,您的代码中最简单的修复方法是

    plt.xticks(data_df['Date'].values)
    

    旁注:我不完全确定为什么 matplotlib.pyplot 会保留两个具有(几乎)相同效果但名称不同的函数。我猜允许从模块或对象调用它是对 MATLAB 的模仿,但在 MATLAB 中,函数名称是相同的。

    【讨论】:

    • 使用上面的行返回错误AttributeError: 'NoneType' object has no attribute 'update'
    • 好吧,我的“盲调试”尝试失败了。你能分享一个minimum, complete and reproducible 代码示例吗?您在 OP 中提供的那个缺少数据框输入数据(并且有很多不相关的混乱)。
    猜你喜欢
    • 2017-09-03
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 2015-06-10
    • 2020-08-27
    相关资源
    最近更新 更多