Show p-value in seaborn.jointplot - python

I'm having a hard time finding out how to show "p" and "pearsonr" values in seaborn plots (as well as just get them), as it seems latest versions no longer show them as default.
Code i'm using:
import seaborn as sns
sns.jointplot('A','A',tech_rets,kind='reg',color='seagreen')
As you can predict, p and pearsonr values are expected to be 0 and 1, respectively.
I've been through several posts here in stackoverflow, which answers sugest using a jointgrid method, such as:
import seaborn as sns
import scipy as stats
sns.jointplot('A','A',tech_rets,kind='reg',color='seagreen').annotate(stats.pearsonr)
But then, it seems that "stats.pearsonr" is outdated.
Thank you in advance!

Related

Incorrect backend configuration with macosx in (old) matplotlib: plt.ion different from savefig, here overwritten alpha keyword

I am using interactive python with plt.ion() for generating figures (v2.7) and have noticed that the figure looks different from the figure exported by savefig (this is not a DPI issue (cf. matplotlib savefig() plots different from show()) - I think it might be a backend issue, but would appreciate help as I don't understand this properly).
Specifically, I wanted visualise the importance of a series of points by the intensity of their colour, which I thought I could do with the "alpha" keyword in matplotlib.
When I just do this, this works fine,
but when I want to add a line to the figure, the alpha keyword seemed to not work any more, and plt.ion() shows this:
I initially thought that perhaps the following issue on github may be related:
https://github.com/matplotlib/matplotlib/issues/4580
but then I noticed that exporting the figure actually produced the following file (i.e. as desired):
It would be great to understand a bit better what is going on, and how I can avoid such issues in the future. Is plt.ion()/plt.show() not the best way to show figures in interactive python, or is this an issue with the alpha keyword?
The code is here:
import numpy as np
from numpy import random as random
from matplotlib import pyplot as plt
fig2,ax2=plt.subplots(1,1,figsize=(3,3),sharey=True)
for ii in range(1):
ax2.plot(np.linspace(0,200,200), [0.1]*200, c= 'k')
for i in range(200):
test2=random.randint(5)
ydata= random.rand(test2)
test = random.rand(test2)
for j in range(test2):
ax2.plot(i,ydata[j],'o',ms=4, c= 'Darkblue',alpha=test[j],markeredgecolor='None')

Rotating parallel coordinate axis-names in Pandas

When using some of the built in visualization tools in Pandas, one that is very helpful for me is the parallel_coordinates visualization. However, since I have around 18 features in the dataframe, the bottom of the parallel_coords plot gets really messy.
Therefore, I was wondering if anyone knew how to rotate the axis-names to be vertical rather than horizontal as shown here:
I did find a way to use parallel_coords in a polar set up, creating a radar-chart; while that was helpful for getting the different features to be visible, that solution doesn't quite work since whenever the values are close to 0, it becomes almost impossible to see the curve. Furthermore, doing it with the polar coord frame required me to break from using pandas' dataframe which is part of what made the this method so appealing.
Use plt.xticks(rotation=90) should be enough. Here is an example with the “Iris” dataset:
import matplotlib.pyplot as plt
import pandas as pd
from pandas.plotting import parallel_coordinates
data = pd.read_csv('iris.csv')
parallel_coordinates(data, 'Name')
plt.xticks(rotation=90)
plt.show()

Python Heatmaps (Basic and Complex)

What's the best way to do a heatmap in python (2.7)? I've found the heatmap.py module, and I was wondering if people have any advice on using it, or if there are other packages that do a good job.
I'm dealing with pretty basic data, like xy = np.random.rand(1000,2) superimposed on an image.
Although there's another thing I want to try, which is doing a heatmap that's scaled to a different heatmap. E.g., I have
attempts = np.random.rand(5000,2)
successes = np.random.rand(500,2)
And I want a heatmap of the successes relative to the density of the attempts. Is this possible?
Seaborn is a pretty widely-used library for making nice-looking plots, and has a heatmap function. Seaborn uses matplotlib under the hood.
import numpy as np
import seaborn as sns
xy = np.random.rand(1000,2)
sns.heatmap(xy, yticklabels=100)
Regarding your second question, I'm not sure what you mean. But my advice would be to create a numpy array or pandas dataframe of "successes [scaled] relative to the density of the attempts", however you mean that, and then pass that scaled array or dataframe to sns.heatmap
You can plot very complex heatmap using python package PyComplexHeatmap: https://github.com/DingWB/PyComplexHeatmap
https://github.com/DingWB/PyComplexHeatmap/blob/main/examples.ipynb
The most basic heatmap you can get is an image plot:
import matplotlib.pyplot as plt
import numpy as np
xy = np.random.rand(100,2)
plt.imshow(xy, aspect="auto")
plt.colorbar()
plt.show()
Note that using more points than you have pixels to show the heatmap might not make too much sense.
There are of course also different methods to draw a heatmaps and you may go through the matplotlib example gallery and see which plot appeals most to you.

Python - Seaborn grid shows, but no content (image) is showing

I'm very new to python, and recently started learning seaborn. When I ran the code, there was no track back and the grid was showed up in a new window. But the problem was no image showed for the FaceGrid, the distplot was showning. Not sure what happened, really appreciate if anyone could help me! Thnank you!
import pandas as pd
import seaborn as sns
import matplotlib as plt
train = pd.read_csv("train.csv")
train["Age"] = train["Age"].fillna(train["Age"].median())
#THIS ONE IS NOT SHOWN
sns.FacetGrid(train, col='Survived', row='Pclass', size=2.2, aspect=1.6)
#THIS ONE WAS SHOWED
sns.distplot(train['Age'])
sns.plt.show()
click to see the image
From the seaborn docs it appears that calling sns.FacetGrid initializes the grid. After that you need to map plots onto the grid. Hopefully that helps.

Importing seaborn in python script messing up plot style

Attached below are two plots. The only difference in the script that produced them is that the second one had an additional line:
import seaborn as sns
I am not setting any seaborn style yet. Just importing seaborn is changing plot style though, even in plots not using seaborn. Is there any way I can import seaborn (to be used in other plots), and not have the style changed for plots that do not use it?
Check this
import seaborn.apionly as sns or from seaborn.apionly import lmplot
http://stanford.edu/~mwaskom/software/seaborn/whatsnew.html

Categories

Resources