【发布时间】: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