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
Related
I managed to generate an interactive matplotlib figure that changes the position of an ellipse using some ipywidget slider.
However the plot is displayed in a new window, I could not find a working solution with the plot in the notebook.
I could generate other interactive figures within a jupyter notebook, like line plots, but with the ellipse, no way..
%matplotlib
from matplotlib import patches
import matplotlib.pyplot as plt
from ipywidgets import interact
fig = plt.figure()
ax = fig.add_subplot(111)
ellipse = patches.Ellipse((0,0), width=50, height=25, angle=15)
ax.add_patch(ellipse)
ax.set_xlim(-100, 100)
ax.set_ylim(-100, 100)
#interact
def update(xcenter=(-50,50), ycenter=(-50,50)):
ellipse.set_center((xcenter, ycenter))
#fig.canvas.draw()
plt.plot()
Using % matplotlib notebook fixed the issue in most cases.
I have the code below, which works great with a single plot, but I'm trying to create a new plot with 1x2 subplots. The second plot will be identical to the first, just in another subplot.
# This code works fine as a single plot
%matplotlib inline
import time
import pylab as pl
from IPython import display
for i in range(10):
pl.clf()
pl.plot(pl.randn(100))
display.display(pl.gcf())
display.clear_output(wait=True)
time.sleep(1.0)
I'm not familar with pylab, but the above plot runs so smoothly compared to the pyplot code I found on the nex, that I'm trying to figure out how to implement this code with subplots.
#can't implement it to a plot with subplots
%matplotlib inline
import time
import pylab as pl
from IPython import display
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, sharex = True, figsize = (10,5))
for i in range(10):
pl.clf()
ax1.plot(pl.randn(100),)
ax2.plot(pl.randn(50))
display.display(pl.show())
display.clear_output(wait=True)
time.sleep(1.0)
However, no graph is being outputted with my attempt.
I'm played around with this code, but I can't seem to make it work cleanly.
thank you.
To visualize the plot with subplots, you should know the differences between Figure and Axes in matplotlib. Basically, axes belong to the figure, and you want to plot your data in the axes, but display the figure. Both Figure and Axes instances can be obtained with a single call to pl.subplots(nrow, ncol). See if the code below does what you want:
%matplotlib inline
import time
import pylab as pl
from IPython import display
for i in range(10):
pl.clf()
f, ax = pl.subplots(1, 2)
ax[0].plot(pl.randn(100))
ax[1].plot(pl.randn(100))
display.display(f)
display.clear_output(wait=True)
time.sleep(1.0)
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?
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?
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!