1

I'm stumped by the use of 'figsize' to change the size of a plot. Perhaps I don't fully understand how matplotlib works. The code below is from an earlier question from me regarding plotting series with different scales in one graph, using two axis. Prin_Balances is a dataframe consisting of floating numbers, the column names should be self explanatory and correspond to features.

fig, ax = plt.subplots()
# plt.figure(figsize=(9,6))

plt.xticks(rotation=45)
plt.plot(Prin_Balances['UPB'], '--r', label='UPB')
plt.legend()
ax.tick_params('Bal', colors='r')


# Get second axis
ax2 = ax.twinx()
plt.plot(Prin_Balances['1 Mos'],  label='1 Mos', color = 'blue')
plt.plot(Prin_Balances['2 Mos'],  label='2 Mos', color = 'green')
plt.plot(Prin_Balances['3 Mos'],  label='3 Mos', color = 'yellow')
plt.plot(Prin_Balances['> 3 Mos'],  label='>3 Mos', color = 'purple')
plt.legend()

ax.tick_params('vals', colors='b')

Now when I run this code, I get a nice, but small graph:

enter image description here

How can I change the size of this graph? The point in the code at which I invoke the second line ('plt.figure') seems to have an effect on the output, in some instances drawing an empty box above a new plot with different labels. (I have not included a shot of this, as I am using Juypter notebook and the two graphs are can only be viewed by scrolling down the output window.

GPB
  • 2,395
  • 8
  • 26
  • 36
  • https://stackoverflow.com/questions/332289/how-do-you-change-the-size-of-figures-drawn-with-matplotlib – Raf Jul 18 '17 at 18:00
  • in the answer above, note there are two ways, one is the one you have in your code, `figure(figsize=size)`, and the other is changing `rcParams`. I prefer using `rcParams` since it applies for all plots. – Raf Jul 18 '17 at 18:03
  • THANK YOU! Such a simple oversight. It was driving me crazy. – GPB Jul 18 '17 at 18:45

1 Answers1

-1

Try something like this:

fig = plt.figure(figsize=(9,6))
ax = fig.add_subplot(121)

# ax.xticks(rotation=45)
ax.plot(Prin_Balances['UPB'], '--r', label='UPB')
ax.legend()
ax.tick_params('Bal', colors='r')


# Get second axis
ax2 = fig.add_subplot(122)
ax2.plot(Prin_Balances['1 Mos'],  label='1 Mos', color = 'blue')
ax2.plot(Prin_Balances['2 Mos'],  label='2 Mos', color = 'green')
ax2.plot(Prin_Balances['3 Mos'],  label='3 Mos', color = 'yellow')
ax2.plot(Prin_Balances['> 3 Mos'],  label='>3 Mos', color = 'purple')
ax2.legend()
ax2.tick_params('vals', colors='b')

plt.show()

ax is an AxesSubplot, which doesn't have the xticks() method, so you'll have to find the equivalent. (set_xticks() works, but not with the rotation keyword, from what I can tell.)