-1

I can't save my figure to jpeg (or any other) file (it is blank)

x=list(df2['DAYTIME'])
z=list(df2['av100002 - temp Wywiew'])
x3= x[::75]


fig1 = plt.figure()
axes1 = fig1.add_axes([0,30,3.5,1.4])
axes1.set_title('Nawiew')


axes1.plot(x,z, lw=3)

axes1.set_xticks(x3)  

plt.xticks(x3, rotation=60)
fig1.savefig('xx.png', dpi=200)
Johny686
  • 3
  • 1

2 Answers2

0

The position of your axes is wrong and puts the axes off figure.

try axes1 = fig1.add_subplot() for a quick fix, which creates an axes centered in the figure space.

If you want to use add_axes() for manual placement of the axes, then the coordinates are given in figure fractions. The coordinate are [left, bottom, width, height] where 0 represent the left/bottom edge of the figure and 1 the right/top edge of the figure.

by default fig.add_subplot() is equivalent to fig.add_axes([0.125, 0.11, 0.9, 0.88])

Full code:

import matplotlib.pyplot as plt

fig1 = plt.figure()
axes1 = fig1.add_subplot(111)
axes1.set_title('Nawiew')
fig1.savefig('xx.png', dpi=200)

enter image description here

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • Thanks for replay. I am a rookie and i dont know where to put 'axes1 = fig1.add_subplot()' in my code, can you help? – Johny686 Jan 23 '20 at 20:31
  • replace the line `axes1 = fig1.add_axes([0,30,3.5,1.4])` with `axes1 = fig1.add_subplot()` – Diziet Asahi Jan 23 '20 at 20:39
  • AttributeError Traceback (most recent call last) in 2 3 axes1 = fig1.add_subplot() ----> 4 axes1.set_title('Nawiew') 5 6 AttributeError: 'NoneType' object has no attribute 'set_title' – Johny686 Jan 23 '20 at 20:45
  • I've added a full code that produces a plot. Please run it as it is written. If that does not work then you need to provide a full [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Diziet Asahi Jan 23 '20 at 20:52
  • It dose not work when i paste it this is my error this time --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in 3 fig1 = plt.figure() 4 axes1 = fig1.add_subplot() ----> 5 axes1.set_title('Nawiew') 6 fig1.savefig('xxx.png', dpi=200) AttributeError: 'NoneType' object has no attribute 'set_title' – Johny686 Jan 23 '20 at 21:09
  • can you try to write `fig1.add_subplot(111)` instead of `fig1.add_subplot()` – Diziet Asahi Jan 23 '20 at 21:13
  • yeah, it is working, but how can i manipulate the plot size ? – Johny686 Jan 23 '20 at 21:22
  • This https://towardsdatascience.com/the-fundamentals-of-matplotlib-10b742e3616b seems like a good tutorial. Also look at the gallery / tutorials on the matplotlib website https://matplotlib.org/tutorials/index.html – Diziet Asahi Jan 23 '20 at 21:29
-1

ok i figure it out

` fig,axes = plt.subplots(nrows=1,ncols=1,figsize=(30,3.5))

axes.plot(x,z)


plt.grid()
plt.xticks(x3, rotation=60)
plt.tight_layout()
plt.savefig('xx.png', dpi=200)

` thanks for your help

Johny686
  • 3
  • 1