Matplotlib with Eclipse PyDev is not plotting - python

I have the following problem when using Matplotlib in Eclipse with PyDev.
import datetime
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sklearn
...
# Plot results
fig = plt.figure()
fig.patch.set_facecolor('white')
# Plot the price of the SPY ETF
ax1 = fig.add_subplot(211, ylabel='SPY ETF price in $')
bars['Close'].plot(ax=ax1, color='r', lw=2.)
# Plot the equity curve
ax2 = fig.add_subplot(212, ylabel='Portfolio value in $')
returns['total'].plot(ax=ax2, lw=2.)
fig.show()
I left out the calculations in between.
What happens when I run the program is that the Python Launcher starts and then disappears after 1 second.
I tried to solve the problem on Mac OSX and on Ubuntu and in both cases it did not work.
When using IDLE on the other hand it works and it will come out the following output:
In general the matplotlib works an Eclipse as I get displayed the output of the following code:
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
I also set my backend to template in ~/.matplotlib/matplotlibrc, to
backend : Qt4Agg
Maybe someone has an idea why I can not plot this in Eclipse.
Thank you!

Related

Matplotlib FuncAnimation not plotting any chart inside Jupyter Notebook

Simple matplotlib plot. Here is my code
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
from itertools import count
import random
x = []
y = []
index=count()
def animate(i):
x.append(next(index))
y.append(random.randint(0,10))
plt.plot(x,y)
a = FuncAnimation(plt.gcf(),animate,interval=1000)
plt.tight_layout()
plt.show()
Running the code above I get
<Figure size 576x396 with 0 Axes>
but no chart appears.
Are you using Jupyter notebooks to run it? I tried with native libraries and it works just fine. The plots are visible.
Checking here i see the same situation. Could you try to use %matplotlib inline before importing matplotlib as:
%matplotlib inline # this line before importing matplotlib
from matplotlib import pyplot as plt
That said, the animation can be displayed using JavaScript. This is similar to the ani.to_html5() solution, except that it does not require any video codecs.
from IPython.display import HTML
HTML(a.to_jshtml())
this answer brings a more complete overview...

Show matplotlib figure statelessly

Here's how to create a "stateful" plot in matplotlib and show it in non-interactive mode:
import matplotlib.pyplot as plt
plt.plot([1,2,8])
plt.show()
I am more interested in the "stateless" approach as I wish to embed matplotlib in my own python library. The same plot can be constructed "statelessly" as follows:
from matplotlib.figure import Figure
fig = Figure()
ax = fig.subplots()
lines = ax.plot([1,2,8])
However I don't know how to show it without resorting to pyplot , which I don't want to do as I would like to build up my own display mechanism.
How do I show the figure without resorting to pyplot?

Why legend does not appear outside plot?

I run code from this answer How to put the legend out of the plot in PyCharm in console (Anaconda environment) and it didn't produced right output
I recreated a conda env, but it still does not work.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,2*np.pi)
colors=["#7aa0c4","#ca82e1" ,"#8bcd50","#e18882"]
fig, axes = plt.subplots(ncols=2)
for i in range(4):
axes[i//2].plot(x,np.sin(x+i), color=colors[i],label="y=sin(x+{})".format(i))
fig.legend(loc=7)
fig.tight_layout()
fig.subplots_adjust(right=0.75)
plt.show()
Expected: what's presented in answer.
Actual in PyCharm (SciView)
Actual in prompt

pyplot.show function in spyder with python 3.4

I'm a novice with Python and working through "Machine Learning In Action" by P. Harrington. I'm having issues seeing my plot when using the pyplot.show() function. Below is the code that I'm entering in the IPython console on the bottom right of Spyder.
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(datingDataMat[:,1], datingDataMat[:,2])
plt.show()
After I enter this last line, nothing happens. Does anyone have an idea why it doesn't appear?

Making plot in ipython with matplotlib

I've been trying to make a scatter plot with the following code
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x, y)
plt.show()
If I type these commands line by line into ipython console, there is no graph displayed after the plt.show() command. However, if I copy and paste the whole code block into the console, the graph is displayed.
Has anyone had this issue before? What could be the reason for this?

Categories