0

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 resulting graph:

import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import seaborn as sns
import pandas as pd

tips = sns.load_dataset('tips')
female_waiters = tips[tips['sex']=='Female']

def graph_joint_histograms(df1):
    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 = df1['total_bill'],y = df1['tip'],data = df1,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.get_axes()
    ax2 = g.ax_marg_y.get_axes()
    ax1.set_yscale('log')
    ax2.set_xscale('log')
    
    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([df1['total_bill'],tips['total_bill']],bins = 10, stacked=True,log = True,color = colors, ec='black')

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

Any ideas would be much appreciated.

Here is the resulting graph:

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
grsoxfan
  • 13
  • 3
  • You asked [the same question](https://stackoverflow.com/questions/49266091/how-to-change-the-tick-labels-on-a-a-seaborn-joint-plot) two days ago. I do not think that reposting same questions is a good practice on SO and it will definitely not help you to find an answer. – SergiyKolesnikov Mar 16 '18 at 18:03
  • Yes I know. I'm trying to improve the question so I made it not reliant on my own dataset and my original question was lacking in detail on the code. – grsoxfan Mar 16 '18 at 18:07
  • Then you should update the original question, because there are two ppl already trying to help you there and you can notify them about the change. – SergiyKolesnikov Mar 16 '18 at 18:10

1 Answers1

2

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')

def graph_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

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • I see, thank you for that! Do you know why the tick labels for the y margin plot are not being changed to 1,10,100,1000? – grsoxfan Mar 16 '18 at 18:59