leotac / joypy

Joyplots in Python with matplotlib & pandas :chart_with_upwards_trend:
MIT License
559 stars 59 forks source link

Joypy and gridspec #60

Open K20shores opened 3 years ago

K20shores commented 3 years ago

I'd like to add a plot, side-by-side the joyplot, showing all of the distributions on one plot. I am successful at doing that, but something is weird with the xticks. I've been looking through the code at how the xticks are set and I don't see why what is in your package would be wrong.

The problem can be seen in the figure below. It looks like the xticks are being set relative to the figure size and not the last axes instance. Would you happen to know what's going on here? Because I sure don't. Any thoughts would be appreciated.

The figure: download

The code:

from scipy.stats import gaussian_kde
import numpy as np
import pandas as pd
from sklearn import datasets

import joypy
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import rgb2hex

iris = datasets.load_iris()
df=pd.DataFrame(iris.data,columns=iris.feature_names)

fig = plt.figure(dpi=300)
gs = fig.add_gridspec(nrows=4, ncols=2)

ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[1, 0])
ax3 = fig.add_subplot(gs[2, 0])
ax4 = fig.add_subplot(gs[3, 0])

ax5 = fig.add_subplot(gs[:, 1])

axes = [ax1, ax2, ax3, ax4]
joypy.joyplot(df, ax=axes, colormap=cm.autumn_r, linewidth=0.5)

limits = ax1.get_xlim()
x = np.arange(limits[0], limits[1], 0.01)
ncols = len(df.columns)

for i, (col, vals) in enumerate(df.items()):
    gkde = gaussian_kde(vals, bw_method=None)
    y = gkde.evaluate(x)
    ax5.plot(x, y, lw=0.5, color='k')
    ax5.fill_between(x, y, color=rgb2hex(cm.autumn_r(i / ncols)))
K20shores commented 3 years ago

Oh, I see. I missed a line. Why can't you grab the last axes instance (axes[-1]) instead of creating another one?

K20shores commented 3 years ago

I would like to note that an alternative to this might be using subfigures, since joypy, as it stands right now, uses multiple axes within the same figure. Therefore, you can combine joypy with other graphics probably in an easier way with subfigures than trying to use gridspec.