1

This is the part of my code with the error (that's all related to the graph, but the points:

plt.figure (figsize = (10,6))
plt.title ("Alfa x CL")
plt.plot (Alpha,CL, "b",linewidth = 2, label = "Xflr5")
plt.plot (alfa,cl, "r",linewidth = 2, label = "Experimental")
plt.legend (loc = 'upper left')
plt.grid (True)
plt.xlabel ("Alfa")
plt.ylabel ("Cl")
plt.savefig (grafico01) #grafico01 is a variable used before
plt.show ()

And it shows the message < Figure size 1000x600 with 1 Axes > instead of the graph (After runing the program again, the graph is shown for some reason)

After searching a lot, I believe the error is on the order I placed the functions, but I have no ideia with one is the right one, everything I found here was about the plt.figure () being after the plt.plot () (with is not the case here)... Any help with the order? Or is it something else?

1 Answers1

0

Put in your variables in the plot function like you already have. Try plotting directly on the desired axes using ax. It is the preferred way of doing things:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(10,6))
ax.plot(range(0,10),range(10,0,-1),  'b',linewidth = 2, label = 'Xflr5')
ax.plot(range(0,10),[i*(i**(1/2)) for i in range(0,10)], 'r',linewidth = 2, label = 'Experimental')
ax.set_title('Alfa x CL')
ax.legend(loc='upper left')
ax.grid(True)
ax.set_xlabel ("Alfa")
ax.set_ylabel ("Cl")
plt.savefig ('grafico01.png') #grafico01 is a variable used before
plt.show ()

For another figure just do:

fig2, ax2 = plt.subplots(figsize=(10,6))
ax2.plot(range(0,10),range(10,0,-1),  'b',linewidth = 2, label = 'Xflr5')

just like before.

enter image description here

Monty
  • 781
  • 2
  • 6
  • 23
  • It didn't work... I changed the variables, of course, but I think I did it in the wrong way... `ax.plot (Alpha,CL, 'b',linewidth = 2, label = 'Xflr5')` `ax.plot (alfa,cl, 'r',linewidth = 2, label = 'Experimental')` @Monty The variables are Alpha and CL for the blue line and alfa, and cl for the red one. It gave me the same message – Marcos Matsuura Ayres Mar 07 '19 at 23:18
  • Are you saying that you saw this message again? < Figure size 1000x600 with 1 Axes > – Monty Mar 07 '19 at 23:34
  • Yes, I saw this exactly message again @Monty – Marcos Matsuura Ayres Mar 07 '19 at 23:37
  • Sorry for not seeing this sooner. Does the graph not appear the first time and then appear when you run the second time using the code I posted above? (similar to the behavior you described in the original post?) – Monty Mar 08 '19 at 05:19
  • According to this link, the behavior you have might be expected. It is odd that your example above doesn't reproduce the issue though. If adjusting the backend doesn't work, then please provide an example I where I can reproduce the error. https://github.com/matplotlib/matplotlib/issues/11745 I suppose I should also ask, are you using Jupyter or an IDE for your code? – Monty Mar 08 '19 at 05:26
  • Yes, the code have the same behavior as before, it shows the message, and if I run again, it plots the graph... I'm using Jupyter, but how do I adjust the backend? Will you need the full code if it fails? @Monty – Marcos Matsuura Ayres Mar 08 '19 at 14:42
  • In the first cell, have you tried doing the following cell magic? %matplotlib notebook – Monty Mar 08 '19 at 21:44
  • Also check out this for changing the backend: https://stackoverflow.com/questions/49471972/maplotlib-pyplot-show-works-with-python-but-not-with-jupyter-console?noredirect=1&lq=1 – Monty Mar 08 '19 at 21:45
  • I searched a lot on the internet and found some backend... Thank you! (I used TkAgg) @Monty – Marcos Matsuura Ayres Mar 08 '19 at 23:17