Skip to content Skip to sidebar Skip to footer

How To Manually Change The Tick Labels Of The Margin Plots On A Seaborn Jointplot

I am trying to use a log scale as the margin plots for my seaborn jointplot. I am usings set_xticks() and set_yticks(), but my changes do not appear. Here is my code below and the

Solution 1:

You should actually get an error from the line g.ax_marg_y.get_axes() since an axes does not have a get_axes() method. Correcting for that

ax1 =g.ax_marg_x
ax2 = g.ax_marg_y

should give you the desired plot. The ticklabels for the log axis are unfortunately overwritten by the histogram's log=True argument. So you can either leave that out (since you already set the axes to log scale anyways) or you need to set the labels after calling hist.

import matplotlib.pyplot as plt
import seaborn as sns

tips = sns.load_dataset('tips')

defgraph_joint_histograms(tips):
    g=sns.jointplot(x = 'total_bill',y = 'tip', data = tips, space = 0.3,ratio = 3)
    g.ax_joint.cla()
    g.ax_marg_x.cla()
    g.ax_marg_y.cla()

    for xlabel_i in g.ax_marg_x.get_xticklabels():
        xlabel_i.set_visible(False)
    for ylabel_i in g.ax_marg_y.get_yticklabels():
        ylabel_i.set_visible(False)

    x_labels = g.ax_joint.get_xticklabels()
    x_labels[0].set_visible(False)
    x_labels[-1].set_visible(False)

    y_labels = g.ax_joint.get_yticklabels()
    y_labels[0].set_visible(False)
    y_labels[-1].set_visible(False)

    g.ax_joint.set_xlim(0,200)
    g.ax_marg_x.set_xlim(0,200)

    g.ax_joint.scatter(x = tips['total_bill'],y = tips['tip'],data = tips,
                       c = 'y',edgecolors= '#080808',zorder = 2)
    g.ax_joint.scatter(x = tips['total_bill'],y = tips['tip'],data = tips, 
                       c= 'c',edgecolors= '#080808')

    ax1 =g.ax_marg_x
    ax2 = g.ax_marg_y
    ax1.set_yscale('log')
    ax2.set_xscale('log')

    ax2.set_xlim(1e0, 1e4)
    ax1.set_ylim(1e0, 1e3)

    ax2.xaxis.set_ticks([1e0,1e1,1e2,1e3])
    ax2.xaxis.set_ticklabels(("1","10","100","1000"), visible = True)

    plt.setp(ax2.get_xticklabels(), visible = True)
    colors = ['y','c']
    ax1.hist([tips['total_bill'],tips['total_bill']],bins = 10, 
             stacked=True, color = colors, ec='black')

    ax2.hist([tips['tip'],tips['tip']],bins = 10,orientation = 'horizontal', 
             stacked=True, color = colors, ec='black')
    ax2.set_ylabel('')


graph_joint_histograms(tips)
plt.show()

enter image description here

Post a Comment for "How To Manually Change The Tick Labels Of The Margin Plots On A Seaborn Jointplot"