3

I have two python lists - a (entries are strings) and b (numerical). I plot them with the following snippet (works perfectly) -

import matplotlib.pyplot as plt
plt.bar(names, values)
plt.suptitle('Average Resale Price (SGD) vs Flat Model')
plt.xticks(rotation='82.5')
plt.show()

Now I try to save the above figure -

plt.savefig('foo.png',dpi=400)

However I end up getting a white figure! How do I save the barplot ?

Abhijit Ghosh
  • 41
  • 1
  • 1
  • 5
  • What else have you tried? Why do you expect this to work and it does not? – RaphaMex Dec 21 '17 at 03:37
  • 1
    The python code halts when you call `plt.show()`, so I'm guessing that you are closing the figure before `plt.savefig` is being called -- it might be that this is the reason for the white figure. Try to comment out the `plt.show()` line and re-run the code. – Thomas Kühn Dec 21 '17 at 07:00
  • Thomas Kuhn, thanks a lot ! – Abhijit Ghosh Dec 21 '17 at 07:33

1 Answers1

4

It's not hard. Try to put plt.savefig('foo.png',dpi=400) before plt.show():

import matplotlib.pyplot as plt

names=['alex', 'simon', 'beta']
values=[10,20,30]

plt.bar(names, values)

plt.suptitle('Average Resale Price (SGD) vs Flat Model')
plt.xticks(rotation='82.5')

plt.savefig('foo.png',dpi=400)
plt.show()

enter image description here

Thinklex
  • 83
  • 6
Alessandro Peca
  • 873
  • 1
  • 15
  • 40