Quantile-Quantile Plot using Seaborn and SciPy - python-3.x

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.

Related

Displaying two SHAP beeswarm plots side by side in the same figure using matplotlib

I wonder how to place two independent SHAP beeswarm plots into the same figure but in different axes. I am using matplotlib version 3.4.2, shap version 0.39.0, and Python 3.8.2. I am trying using a single figure and two axes but realize that the beeswarm method does not handle the axis as parameter (error: TypeError: beeswarm() got an unexpected keyword argument 'ax').
I am aware of this related question, but this is different since I am using SHAP API. Any recommendation is welcome. Thanks in advance.
from matplotlib import pyplot as plt
import shap
fig, axes = plt.subplots(1, 2, figsize=(35, 7), gridspec_kw = {"wspace":1.0})
shap.plots.beeswarm(explainer_a(X_test_a), max_display=10, ax=axes[0])
shap.plots.beeswarm(explainer_b(X_test_b), max_display=10, ax=axes[1])
plt.show()

Attempting to convert an image to grayscale, or better, binary

My basic plan here is to create an image recognition software that tracks the size of different bubbles. I basically have a compilation of pictures that constitute a video. I have it working as of right now using PIMS to import the files I need and place them into an array (rawframes). I can print my picture.
import numpy as np
import pandas as pd
import pims
from pims import pipeline
import trackpy as tp
import os
import matplotlib as mpl
import matplotlib.pyplot as plt
#pipeline
def binary(frame):
return frame[:, :, 1]
id_example = 1
rawframes = pims.ImageSequence(os.path.join('BubbleSize/90FoamQuality/DryFoams', 'T20190411_002_ (*).jpg'), process_func=binary)
plt.imshow(rawframes[id_example])
What I am trying to do here is convert the images from regular into black and white. I have not used many of the things I imported yet I know, this is a very preliminary step.
However, below is a before and after image comparison. Can someone help me out or walk me through these steps here? I get lost when it comes to filtering the images through python.
edit --> when I change my pipeline function to the below, I get the same green image
edit2 --> printing frame.shape and frame.dtype in binary pipeline respectively

Why will Seaborn function 'regplot' not run in Jupyter?

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)

How to install colormap using pip for Spyder (Python 3.5) on Windows

I want to use cmap_builder, So I tried
from colormap import cmap_builder.
When I tried, Spyder throwed me an error
ImportError: No module named 'colormap'
So I tried installing, pip install colormap as described in http://colormap.readthedocs.io/en/latest/
This didn't work and displayed a message
No matching distribution found
So is there a different way to install colormap for python 3.5 to use on Spyder ?
In principle matplotlib already has all the tools available to create custom colormaps. The two main options are to create a segmented colormap, LinearSegmentedColormap or a discrete colormap ListedColormap.
Find here an example of a continuous colormap between crimson, gold and blue:
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
import numpy as np
cmap = mcolors.LinearSegmentedColormap.from_list("n", ["crimson", "gold","steelblue"])
x = np.linspace(-1,2.7)
X,Y = np.meshgrid(x,x)
Z = np.exp(-X**2-Y**2)
im =plt.imshow(Z, cmap=cmap)
plt.colorbar()
plt.show()
A discrete colormap could be created like this:
cmap = mcolors.ListedColormap(["crimson", "gold","steelblue"])

Getting an object in Python Matplotlib

To make a plot, I have written my code in the following fashion:
from pylab import *
x = [1,2,3]
y = [1,2,3]
matplotlib.pyplot.scatter(x,y,label='Blah')
matplotlib.pyplot.legend(title='Title')
matplotlib.pyplot.show()
I want to change the font size of the legend title. The way to go about this is to get the legend object and then change the title that way (e.g., How to set font size of Matplotlib axis Legend?)
Instead of rewriting all my code using ax.XXX, figure.XXX, etc, is there any way to get at the legend object from the code I have written, and then go from there?
That is to say, how do I define
Legend
from my original piece of code, such that
Title = Legend.get_title()
Title.set_fontsize(30)
would get at the title object and then allow me to play with .get_title()?
I think I'm on the verge of a eureka moment regarding object-orientated languages. I have a feeling a good answer will give me that eureka moment!
cheers,
Ged
First, in your code you should stick to using either from pylab import * and then use the imported methods directly, or import matplotlib.pyplot as plt and then plt.* instead of matplotlib.pyplot.*. Both these are "conventions" when it comes to working with matplotlib. The latter (i.e. pyplot) is generally preferred for scripting, as pylab is mainly used for interactive plotting.
To better understand the difference between pylab and pyplot see the matplotlib FAQ.
Over to the problem at hand; to "get" an object in Python, simply assign the object to a variable.
from pylab import *
x = [1,2,3]
y = [1,2,3]
scatter(x,y,label='Blah')
# Assign the Legend object to a variable leg
leg = legend(title='Title')
leg_title = leg.get_title()
leg_title.set_fontsize(30)
# Optionally you can use the one-liner
#legend(title='Title').get_title().set_fontsize(30)
show()
Visual comparison (rightmost subplot produced with the above code):

Resources