Why will Seaborn function 'regplot' not run in Jupyter? - python-3.x

I am having trouble with code Seaborn regplot function in Jupyter notebooks using Watson-Studio.
Using Python 3.6, the code appears to get stuck whilst processing, and this happens until I stop the code.
When I run this using IDLE on my Mac, the code runs perfectly and the plot shows.
Seems to happen with plots lmplot and regplot, however boxplots etc do show as normal.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df = pd.read_csv(csv.csv)
sns.regplot(x = 'independent', y = 'dependent', data = df)
The expected results should be a graph of the linear relationship between the two variables, however I am just getting a loading bar.
When I stop running the kernel, the graph exists as a scatterplot with no line of best fit. Of course this has the error in notebook as 'Keyboard Interrupted'.
Could this possibly be a bug? Thanks for your help.

Set ci parameter to none and it will solve your problem.
sns.regplot(x = 'independent', y = 'dependent', data = df, ci = None)

Related

matplotlib.plot doesn't show any image or plot without any error

I'm trying to show some images on VScode in Ubuntu, but matplotlib doesn't work. For example, even the most simple code such as:
import matplotlib.pyplot as plt
s_in = plt.imread("/media/aro/New Volume/Super resolution dataset/set5/Test/BSDS100/3096.png")
plt.imshow(s_in)
plt.show()
is not working, and it doesn't show any errors or warnings. What should I do?
In case anybody has got the same problem as mine. The error was because of the backend. So I had to change it. the only backend that worked for me was WebAgg.
Just add the following code at the beginning of your code.
import matplotlib.pyplot as plt
plt.switch_backend('WebAgg')

Displaying both plt.show() images as Figure 1, Figure 2 at the same time

Running the following code, I am unable to display both images at the same time in separate windows, or go from figure1 to figure2 with the arrow button.
Currently I am able to get figure2, only when I close figure1.
I have tried the following code to generate separate "figure" labels.
from skimage import data, color, io
from matplotlib import pyplot as plt
rocket = data.rocket()
gray_scale_rocket = color.rgb2gray(rocket)
f1=plt.figure(1)
io.imshow(rocket)
plt.show()
f2=plt.figure(2)
io.imshow(gray_scale_rocket)
plt.show()
I expect to see two windows figure1 and figure2 to be viewable at the same time (without needing to close figure1 window first), displaying the rocket image in color and in grayscale.
You should remove the first call to plt.show(), which is blocking (meaning it stops execution until you are done with the window). When you leave only the second one, it will show both figures simultaneously.
The resulting code:
from skimage import data, color, io
from matplotlib import pyplot as plt
rocket = data.rocket()
gray_scale_rocket = color.rgb2gray(rocket)
f1=plt.figure(1)
io.imshow(rocket)
f2=plt.figure(2)
io.imshow(gray_scale_rocket)
plt.show()
behaves as you expect.

Update a Bokeh Span with an interact element in Jupyter notebook

I am trying to make a span in bokeh using jupyter widgets.
from ipywidgets import interact
import numpy as np
from scipy.stats import norm
from bokeh.sampledata.daylight import daylight_warsaw_2013
from bokeh.io import push_notebook, show, output_notebook
from bokeh.plotting import figure
from bokeh.models import Span
output_notebook()
p = figure()
x_axis = np.arange(-10, 10, 0.001)
# Mean = 0, SD = 2.
y_axis = norm.pdf(x_axis,0,2)
p.line(x_axis, y_axis, line_dash='solid', line_width=2)
cutoff = Span(location=1,
dimension='height', line_color='green',
line_dash='dashed', line_width=2)
p.add_layout(cutoff)
show(p, notebook_handle=True)
def update(new_cutoff_location):
cutoff.location = new_cutoff_location
push_notebook()
interact(update, new_cutoff_location = 1.0)
When I run this code I get ValueError: PATCH-DOC message requires at least one event at push_notebook(). I suspect this indicates that the update to cutoff.location isn't getting detected, so it it looks as if there are no changes to send. Passing the handle doesn't seem to make a difference. Looking at the sample code in this github issue, it looks like there used to be an set method on span elements, but there doesn't appear to be one on my span element cutoff. Maybe there is a different function I am supposed to call to register the change?
I'm on bokeh 0.12.11 with jupyter 1.0.0, jupyter-client 5.1.0, jupyter-console 5.2.0, jupyter-core 4.4.0
This appears to be a regression in Bokeh 0.12.11. Your code works with version 0.12.10 so the immediate workaround is to downgrade. I've made GitHub issue here that you can follow. We will issue a new point release with a fix ASAP.
UPDATE: The issue is now fixed in recent versions of Bokeh

Quantile-Quantile Plot using Seaborn and SciPy

Can anyone give me a way to do a qq plot in Seaborn as a test for normality of data? Or failing that, at least in matplotlib.
Thanks in advance
After reading the wikipedia article, I understand that the Q-Q plot is a plot of the quantiles of two distributions against each other.
numpy.percentile allows to obtain the percentile of a distribution. Hence you can call numpy.percentile on each of the distributions and plot the results against each other.
import numpy as np
import matplotlib.pyplot as plt
a = np.random.normal(5,5,250)
b = np.random.rayleigh(5,250)
percs = np.linspace(0,100,21)
qn_a = np.percentile(a, percs)
qn_b = np.percentile(b, percs)
plt.plot(qn_a,qn_b, ls="", marker="o")
x = np.linspace(np.min((qn_a.min(),qn_b.min())), np.max((qn_a.max(),qn_b.max())))
plt.plot(x,x, color="k", ls="--")
plt.show()
Try statsmodels.api.qqplot().
Using same data as above, this example shows a normal distribution plotted against a normal distribution, resulting in fairly straight line:
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
a = np.random.normal(5, 5, 250)
sm.qqplot(a)
plt.show()
This example shows a Rayleigh distribution plotted against normal distribution, resulting in a slightly concave curve:
a = np.random.rayleigh(5, 250)
sm.qqplot(a)
plt.show()
I'm not sure if this still recent, but I notice that neither of the answers really addresses the question, which asks how to do qq-plots with scipy and seaborn, but doesn't mention statsmodels. In fact, qq-plots are available in scipy under the name probplot:
from scipy import stats
import seaborn as sns
stats.probplot(x, plot=sns.mpl.pyplot)
The plot argument to probplot can be anything that has a plot method and a text method. Probplot is also quite flexible about the kinds of theoretical distributions it supports.
At seaborn-qqplot addon documentation an example is shown. Also see.
Working with pycharm and windows 10 I had difficulties installing the library with:
pip install seaborn-qqplot
in my virtual environment. The import line:
from seaborn_qqplot import pplot
was not recognized.
With (commands for PyCharm): file -> settings -> Project -> Python Interpreter -> + (Install) I could import pplot from seaborn_qqplot and could create a Quantile - Quantile plot.

Python3x + MatPlotLib - Updating a chart?

I am new to both the python and matplotlib languages and working on something for my husband.
I hope you guys can help me out.
I would like to pull in a file using Open, read it, and update a graph with it's values.
Sounds easy enough right? Not so much in practice.
Here is what I have so far to open and chart the file. This works fine as it is to chart the file 1 time.
import matplotlib.pyplot as plt
fileopen = open('.../plotresults.txt', 'r').read()
fileopen = eval(fileopen) ##because the file contains a dict and security is not an issue.
print(fileopen) ## So I can see it working
for key,value in fileopen.items():
plot1 = value
plt.plot(plot1, label=str(key))
plt.legend()
plt.show()
Now I would like to animate the chart or update it so that I can see changes to the data. I have tried to use matplotlib's animation feature but it is advanced beyond my current knowledge.
Is there a simple way to update this chart, say every 5 minutes?
Note:
I tried using Schedule but it breaks the program (maybe a conflict between schedule and having matplotlib figures open??).
Any help would be deeply appreciated.
Unfortunately you will just waste time trying to get a clean solution without either using matplotlib's animation feature or using the matplotlib OO interface.
As a dirty hack you can use the following:
from threading import Timer
from matplotlib import pyplot as plt
import numpy
# Your data generating code here
def get_data():
data = numpy.random.random(100)
label = str(data[0]) # dummy label
return data, label
def update():
print('update')
plt.clf()
data, label = get_data()
plt.plot(data, label=label)
plt.legend()
plt.draw()
t = Timer(0.5, update) # restart update in 0.5 seconds
t.start()
update()
plt.show()
It spins off however a second thread by Timer. So to kill the script, you have to hit Ctrl-C twice on the console.
I myself would be interested if there is a cleaner way to do this in this simple manner in the confines of the pyplot machinery.
Edits in italic.

Resources