0

I'm plotting 3 charts in one figure.

The first chart came out fine and I could use the methods associated with a matplotlib chart.

fig = plt.figure(constrained_layout = True, figsize = [10,10])
gs = fig.add_gridspec(4,2)
fig_ax_curve = fig.add_subplot(gs[0:2,:])

However, as I proceeded with the second chart:

fig_ax_2_10 = fig.add_subplot(gs[2,:])
fig_ax_2_10 = fig_ax_2_10.plot(yield_history_2_10_30["2Y-10Y Spread"], label = "2Y - 10Y Spread", color = "red")
fig_ax_2_10.axhline(color = "black")

I was unable to execute any of the associated methods and got back:

AttributeError: 'list' object has no attribute 'axhline'

I am aware of an existing question that was similar: Matplotlib Plotting: AttributeError: 'list' object has no attribute 'xaxis'

If I'm not mistaken, my code has already followed what is suggested in that answer to avoid the error, specifically using ax.(methods)

1 Answer 1

1

The error comes from the line :

fig_ax_2_10 = fig_ax_2_10.plot(yield_history_2_10_30["2Y-10Y Spread"], label = "2Y - 10Y Spread", color = "red")

You are assigning the result of the plot function (a list of Line2D) to fig_ax_2_10. This is why you can not call axhline on that object.

Just replace :

fig_ax_2_10 = fig.add_subplot(gs[2,:])
fig_ax_2_10.plot(yield_history_2_10_30["2Y-10Y Spread"], label = "2Y - 10Y Spread", color = "red")
fig_ax_2_10.axhline(color = "black")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.