2

I am trying to create a plot made of 5 subplots made of simple line graphs using Matplotlib and Pandas on Visual Studio code. However, for some reason the image always looks very small and clumped up even if I make the figure very big. I tried modifying the space between figures with subplots_adjust but that doesn't change anything.

Here is my code

ax1 = plt.subplot(1, 5, 1)
ax1 = plt.plot(returns["MSFT"])
plt.xlabel("Date")
plt.ylabel("Daily MSFT Returns")
plt.title("MSFT Returns Over Time")

ax2 = plt.subplot(1, 5, 2)
ax2 = plt.plot(returns["AMZN"])
plt.xlabel("Date")
plt.ylabel("Daily AMZN Returns")
plt.title("AMZN Returns Over Time")

ax3 = plt.subplot(1, 5, 3)
ax3 = plt.plot(returns["AAPL"])
plt.xlabel("Date")
plt.ylabel("Daily AAPL Returns")
plt.title("AAPL Returns Over Time")

ax4 = plt.subplot(1, 5, 4)
ax4 = plt.plot(returns["GOOG"])
plt.xlabel("Date")
plt.ylabel("Daily GOOG Returns")
plt.title("GOOG Returns Over Time")

ax5 = plt.subplot(1, 5, 5)
ax5 = plt.plot(returns["FB"])
plt.xlabel("Date")
plt.ylabel("Daily FB Returns")
plt.title("FB Returns Over Time")

plt.figure(figsize=(100, 100))
plt.subplots_adjust(wspace = 10)
plt.show()

figure displayed

0

2 Answers 2

2

Update: As BigBen mentioned, you can further simplify with a loop. For example, here we put all subplot axes in axes and company names in names, then zip and iterate:

fix, axes = plt.subplots(nrows=1, ncols=5, figsize=(20, 5))
names = ['MSFT', 'AMZN', 'AAPL', 'GOOG', 'FB']

for ax, name in zip(axes, names):
    ax.plot(returns[name])
    ax.set_ylabel(f'Daily {name} Returns')
    ax.set_title(f'{name} Returns Over Time')

Generally it's easier to first create the subplot axes ax1, ..., ax5 using plt.subplots() with figsize. Then operate on those premade axes handles like ax1.plot() instead of plt.plot(), ax1.set_xlabel() instead of plt.xlabel(), etc.

# first create ax1~ax5
fig, (ax1,ax2,ax3,ax4,ax5) = plt.subplots(nrows=1, ncols=5, figsize=(20, 5))

ax1.plot(returns["MSFT"])               # not plt.plot(...)
ax1.set_xlabel("Date")                  # not plt.xlabel(...)
ax1.set_ylabel("Daily MSFT Returns")    # not plt.ylabel(...)
ax1.set_title("MSFT Returns Over Time") # not plt.title(...)

ax2.plot(returns["AMZN"])
ax2.set_xlabel("Date")
ax2.set_ylabel("Daily AMZN Returns")
ax2.set_title("AMZN Returns Over Time")

ax3.plot(returns["AAPL"])
ax3.set_xlabel("Date")
ax3.set_ylabel("Daily AAPL Returns")
ax3.set_title("AAPL Returns Over Time")

ax4.plot(returns["GOOG"])
ax4.set_xlabel("Date")
ax4.set_ylabel("Daily GOOG Returns")
ax4.set_title("GOOG Returns Over Time")

ax5.plot(returns["FB"])
ax5.set_xlabel("Date")
ax5.set_ylabel("Daily FB Returns")
ax5.set_title("FB Returns Over Time")

plt.subplots() example

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this displays them well now! Have a great day ahead.
1

A couple of suggestions that might help- first matplotlib has a function "tight_layout" which can help set the white space sensibly and reduce the number of tick labels. documentation. To use it simply call plt.tight_layout() just before plt.show()

But this won't solve the problem of there being lots of text on your figure. You could try and save text by using a suptitle of "Stock returns over time" as shown here and simply referring to each plot by the stock name for the sub plot titles.

It might also be worth considering if a different style of plot could do a better job. If the returns are of similar magnitude and over the same time period it may make more sense to have them as different coloured lines on the same axes, with a legend showing identifying the stocks

5 Comments

Hello, thanks a lot for your reply! The main problem is that the figure is displayed as very cramped together and for some reason no matter how big I try to make it, it always stays the same size. I think it may be a setting in vistual studio code itself, but I can't figure it out. I am actually learning how to code, hence why I am trying to 5 subplots.
Ah ok, are you using the interactive ipython terminal? It may be worth running the script in a normal terminal to get the plot to display in a new window. You can then resize the window and reapply tight_layout in the GUI (under the configure subplots menu)
Good luck with your learning :) matplotlib can be rather confusing at first!
Thanks! I will try out your suggestions. I am learning through code academy, and there I don't have the same problem with the display, hence why I think its because of some setting in Visual Studio Code. Do you know any other program similar to Visual Studio Code by any chance?
You're welcome :) Atom, Spyder, are other IDEs. I use VSCode but get around this problem by running scripts in the integrated terminal rather than the interactive ipython one. It pops the figure out in a new window and doesn't constrain it to a fixed size

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.