-1

I have a figure that isn't showing the correct dates on the x-axis. Possibly because there are too many observations to show and it results in not showing any. I am not sure if I should use the set_xticks function, but it gives me the attribute error

AttributeError: module 'matplotlib.pyplot' has no attribute 'set_xticks'

My current code is this:

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

The figure looks like this: enter image description here

BlackBear
  • 385
  • 1
  • 5
  • 25

3 Answers3

0

set_xticks is for an axis, if you are setting ticks for a figure it's

plt.xticks(data_df['Date'].values)
Zoe
  • 1,402
  • 1
  • 12
  • 21
0

Alternatively, you can try the following inside your function working on an axis object. The idea is to first create an axis instance ax, and then pass it to the plot command. Later use it to set the 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) 
Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • That makes sense to me, but when I replaced the code with one suggested I get the error. `AttributeError: 'NoneType' object has no attribute 'update'` – BlackBear May 22 '19 at 10:24
  • 1
    @BlackBear: Without having [a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), people will have hard time helping you here. At some point, they might give up answering. So please take some time to re-structure your question by including everything necessary to reproduce the error and the figure. That way you have more chances to get answers – Sheldore May 22 '19 at 10:32
0

The module matplotlib.pyplot has no set_xticks function, but an xticks function instead. (link to doc). However, the class object matplotlib.axes.Axes does have set_xticks (link to doc).

The easiest fix in your code is thus

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

Side note: I am not entirely sure why matplotlib.pyplot keeps around two functions with (almost) the same effect but different names. I guess allowing it to be called from either the module or the object is an imitation of MATLAB, but in MATLAB the function name is the same.

Leporello
  • 638
  • 4
  • 12
  • Using the line above returns the error `AttributeError: 'NoneType' object has no attribute 'update'` – BlackBear May 22 '19 at 10:20
  • 1
    OK then, my attempt at "blind debugging" failed. Can you share a [minimum, complete and reproducible](https://stackoverflow.com/help/reprex) code sample? The one you give in your OP lacks the dataframe input data (and has a lot of irrelevant clutter). – Leporello May 22 '19 at 10:28