I am making 2d histograms for some data with millions of data points. matplotlib.hist2d(x,y,bins,norm=LogNorm())
works well and produces a plot in about 5 seconds, but I like the marginal histograms of seaborn.jointplot()
. How do I color the points in seaborn.jointplot()
with log density of points like in the attached matplotlib.hist2d()
figure? Using KDE takes way too long (I give up after about a minute or so), and I have lots of figures to create. So time to 'get' colors is a factor. Alternatively, how do I add marginal histograms to matplotlib.hist2d()
?
plt.hist2d(x,y,100,norm=LogNorm(),cmap='jet')
sns.jointplot(x=x, y=y)
There might be another direct way to get a color map in seaborn
. I couldn't find any yet. Here is a hacky sample solution to get things done with some random data. As to your second problem, I would suggest to post a new question.
The trick is to first create a jointplot
using seaborn and then hide the 2d-scatter and re-plot it using plt.hist2d
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
# some random data
x = np.random.normal(size=100000)
y = x * 3.5 + np.random.normal(size=100000)
ax1 = sns.jointplot(x=x, y=y)
ax1.ax_joint.cla()
plt.sca(ax1.ax_joint)
plt.hist2d(x, y, bins=(100, 100), cmap=cm.jet);
Here's an alternative, similar approach but sticking within seaborn:
import seaborn as sns
import numpy as np
x = np.random.normal(size=100)
y = x * 3.5 + np.random.normal(size=100)
sns.jointplot(x=x, y=y, kind='kde', cmap='hot_r', n_levels=60, fill=True)
Here's the final fig and code for it. Thanks to @Bazingaa for help.
def makesweetgraph(x=None, y=None, cmap='jet', ylab=None, xlab=None, bins=100, sets=sets, figsize=(5,4), snsbins=60):
set1,set2 = sets
ax1 = sns.jointplot(x=x, y=y,marginal_kws=dict(bins=snsbins))
ax1.fig.set_size_inches(figsize[0], figsize[1])
ax1.ax_joint.cla()
plt.sca(ax1.ax_joint)
plt.hist2d(x,y,bins,norm=LogNorm(),cmap=cmap)
plt.title('%s vs %s (%.4f%% of loci)\n%s and %s' % (xlab,ylab,(len(x)/numsnps)*100,set1,set2),y=1.2,x=0.6)
plt.ylabel(ylab,fontsize=12)
plt.xlabel(xlab,fontsize=12)
cbar_ax = ax1.fig.add_axes([1, 0.1, .03, .7])
cb = plt.colorbar(cax=cbar_ax)
cb.set_label(r'$\log_{10}$ density of points',fontsize=13)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With