0

Hi I have the following code. The code is in a for loop, and it makes over 300 plots.

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

I've checked out the links below ( and more):

When I try the method from the second link. I end up with graphs like so

Attempting to put count via patches

Without that the graph looks something like so

I want to get rid of the words count and the ticks on each subplot xaxis.

Community
  • 1
  • 1
Moo10000
  • 115
  • 11
  • What I don't understand here though is how the desired output should look like. – ImportanceOfBeingErnest Sep 04 '19 at 15:08
  • @ImportanceOfBeingErnest removing the word count on each x-axis, removing the xtick values (0,5,0.5,1.0 ...) and instead showing the value of 'count' next to the bar or on top of the bar, maybe even in a small legend.I also can't get the suptitle to show – Moo10000 Sep 04 '19 at 15:11
  • `ax.set_xlabel("")` removes the xlabel. If you search for "matplotlib annotate bars" or similar you find ways to get a text into a bar. – ImportanceOfBeingErnest Sep 04 '19 at 15:27

1 Answers1

0

@ImportanceOfBeingErnest

Thanks, I followed your advice and this post.

Here is what is a compact version of what I ended up with

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

Moo10000
  • 115
  • 11