Skip to content Skip to sidebar Skip to footer

Matplotlib Plotting: Attributeerror: 'list' Object Has No Attribute 'xaxis'

Example Plot that needs to format date I am trying to plot stock prices against time (see above). The code below does plot the 'OPEN' prices but as I try to format the X-axis date

Solution 1:

This line:

ax1 = plt.plot(df_copy['Date'], df_copy['Open'], label='Open values')

Refines your Axes object to be the list of artists returned by the plot command.

Instead of relying on the state machine to put artists on the Axes, you should use your objects directly:

df_copy = read_stock('EBAY')

fig = plt.figure(figsize=(12, 10), dpi=80)
ax1 = fig.add_subplot(111)
lines = ax1.plot(df_copy['Date'], df_copy['Open'], label='Open values')
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))

Solution 2:

The problem comes from you writing

ax1 = plt.plot(df_copy['Date'], df_copy['Open'], label = 'Open values' )

Since you are changing the type of ax1 from being the handle returned by plt.subplot(). After said line, it is a list of lines that were added to the plot, which explains your error message. See the documentary on the plot command:

Return value is a list of lines that were added. matplotlib.org

Post a Comment for "Matplotlib Plotting: Attributeerror: 'list' Object Has No Attribute 'xaxis'"