How to manually change the tick labels of the margin plots on a Seaborn jointplot

怎甘沉沦 提交于 2019-12-20 06:43:28

问题


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:


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

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



来源:https://stackoverflow.com/questions/49326860/how-to-manually-change-the-tick-labels-of-the-margin-plots-on-a-seaborn-jointplo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!