【问题标题】:Adding count plot totals and removing specific labels添加计数图总数并删除特定标签
【发布时间】:2020-01-07 12:05:02
【问题描述】:

您好,我有以下代码。代码在 for 循环中,绘制了 300 多个图。

    sns.set(style='white', palette='cubehelix', font='sans-serif')
    fig, axs = plt.subplots(2, 3, dpi =200);
    fig.subplots_adjust(hspace=0.5, wspace=1)
    plt.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom=False,      # ticks along the bottom edge are off
    top=False,         # ticks along the top edge are off
    labelbottom=False) # labels along the bottom edge are off
    #tmppath = 'path/{0}'.format(key);
    ##
    sns.countplot(y='Ethnicity', data=value, orient='h', ax=axs[0,0]);
    sns.despine(top=True, right=True, left=True, bottom=True,offset=True)
    sns.countplot(y='Program Ratio', data=value,orient='v',ax=axs[1,0]);
    sns.despine(offset=True)
    sns.countplot(y='Site', data = value, ax=axs[0,1]);
    sns.despine(offset=True)
    sns.countplot(y='HOUSING_STATUS', data = value, ax = axs[1,1])
    sns.despine(offset=True)
    sns.countplot(y='Alt. Assessment', data = value, ax = axs[0,2])
    sns.despine(offset=True)
    pth = os.path.join(tmppath, '{0}'.format(key))
    for p in axs.patches:
         ax.text(p.get_x() + p.get_width()/2., p.get_width(), '%d' % 
          int(p.get_width()), 
          fontsize=12, color='red', ha='center', va='bottom')
    #plt.tight_layout(pad=2.0, w_pad=1.0, h_pad=2.0);
    plt.set_title('{0}'.format(key)+'Summary')
    sns.despine()
    axs[0,0].set_xticklabels('','Ethnicity')
    axs[1,0].set_axis_labels('','Program Ratio')
    axs[0,1].set_axis_labels('','Students by Site')
    axs[1,1].set_axis_labels('','Housing Status')
    axs[0,2].set_axis_labels('','Alt Assessment')
    fig.tight_layout()
    fig.subplots_adjust(top=0.88)
    fig.suptitle('{0}'.format(key)+' Summary')
    plt.suptitle('{0}'.format(key)+' Summary')
    plt.savefig("path/{0}/{1}.pdf".format(key,key), bbox_inches = 'tight'); 
    plt.clf()
    plt.suptitle('{0} Summary'.format(key))
    plt.savefig("path/{0}/{1}.pdf".format(key,key), bbox_inches = 'tight'); 
    plt.clf()

我查看了以下链接(以及更多):

当我尝试第二个链接中的方法时。我最终得到了这样的图表

如果没有图表看起来像这样

我想去掉每个子图 xaxis 上的字数和刻度。

【问题讨论】:

  • 我在这里不明白的是所需的输出应该是什么样子。
  • @ImportanceOfBeingErnest 删除每个 x 轴上的字数,删除 xtick 值(0,5,0.5,1.0 ...),而是在条形旁边显示“count”的值或顶上吧​​,可能还有个小图例。我也拿不出字幕来显示
  • ax.set_xlabel("") 删除 xlabel。如果您搜索“matplotlib 注释条”或类似内容,您会找到将文本放入条中的方法。

标签: python pandas matplotlib data-visualization seaborn


【解决方案1】:

@ImportanceOfBeingErnest

谢谢,我听从了你的建议和post

这是我最终得到的紧凑版本

sns.set(style='white', palette=sns.palplot(sns.color_palette(ui)), font='sans-serif')
        plt.figure(figsize=(20,20))
        fig, axs2 = plt.subplots(2, 3, dpi =300);
        fig.subplots_adjust(top=.8)
        fig.subplots_adjust(hspace=1, wspace=1.5)
        plt.tick_params(
        axis='x',          # changes apply to the x-axis
        which='both',      # both major and minor ticks are affected
        bottom=False,      # ticks along the bottom edge are off
        top=False,         # ticks along the top edge are off
        labelbottom=False) # labels along the bottom edge are off

sns.countplot(y='column',palette = ui,order = df.value_counts().index, data=df, 
                      orient='h', ax=axs2[0,0]);
axs2[0,0].set_xlabel('')
axs2[0,0].set_xticks([])
axs2[0,0].set_ylabel('')
axs2[0,0].set_title('label',size = 'small')
axs2[0,0].tick_params(axis='y', which='major', labelsize=8)

sns.despine(top=True, right=True, left=True, bottom=True,offset=True)
for p in axs2[0,0].patches:
      axs2[0,0].annotate(int(p.get_width()),((p.get_x() + p.get_width()), p.get_y()), xytext=(15, -10), fontsize=8,color='#000000',textcoords='offset points'
                 ,horizontalalignment='center')

fig.suptitle('{0}@{1}'.format(dur,key)+' Summary', va = 'top', ha= 'center') #size = 'small')

props = dict(boxstyle='square', facecolor='white', alpha=0.5)
fig.text(0.85, 0.925, dt.date.today().strftime("%b %d, %Y"),  fontsize=9, verticalalignment='top', bbox=props)
fig.text(0.15, 0.925, 'No. of stuff'+ str(len(value['column'].unique())),fontsize = 10, va = 'top', ha = 'center')
plt.savefig("path/{0}/{1} @ {2}.pdf".format(dur,dur,key), bbox_inches = 'tight'); 
plt.clf()
plt.close('all')

Excuse the black marks, didn't want to show the info

【讨论】:

    猜你喜欢
    • 2011-12-18
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    相关资源
    最近更新 更多