如何手动更改Seaborn关节图上边距图的刻度标签

时间:2018-03-16 17:55:56

标签: python matplotlib jupyter-notebook seaborn data-science

我正在尝试使用对数刻度作为我的seaborn jointplot的边距图。我正在使用set_xticks()和set_yticks(),但我的更改没有出现。以下是我的代码和结果图:

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

非常感谢任何想法。

以下是结果图:

1 个答案:

答案 0 :(得分:0)

由于轴没有g.ax_marg_y.get_axes()方法,因此您应该从第get_axes()行获得错误。 纠正

ax1 =g.ax_marg_x
ax2 = g.ax_marg_y

应该给你想要的情节。不幸的是,直方图的log=True参数覆盖了日志轴的勾选标签。因此,您可以将其保留(因为您已经将轴设置为记录比例)或者您需要在调用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