1

I have the following code (removed most of the fluff for brevity):

# Assign 'aInf' to the DataFrame called 'dfDis'
dfDis = pd.DataFrame(aDis, columns=cols)

dfDis.columns = ['Year', 'Type', 'Total']

try:
     dfDisB = pd.pivot_table(dfDis, index=['Year'], columns = ['Type'],aggfunc= 'sum',fill_value=0)
     dfDisB.columns = [x[1] for x in dfDisB.columns]
     dfDisC = dfDisB.fillna(0)

     dfFinalDis = dfDisC.reindex(dfDisC.sum().sort_values(ascending=False).index, axis=1)

     # This is a dictonary of dissemination with a colour assigned to it
     colorDict = {**** cut ****}

     # Change the font
     plt.rcParams["font.family"] = "Gill Sans MT"

     axDis = dfFinalDis.plot.bar(stacked=True, color=[colorDict.get(x, '#333333') for x in dfFinalDis.columns],
                                 figsize=(10, 8))
     plt.legend(loc='upper right', bbox_to_anchor=(0.6, -0.3), frameon=False)
     plt.title(items1)
     # Hide the spines
     for spine in axDis.spines:
         axDis.spines[spine].set_visible(False)

     # Hide the tick marks
     axDis.tick_params(axis=u'both', which=u'both', length=0)

     # Move the x label down slightly
     axDis.xaxis.labelpad = 25

     # Make new folder
     newpath = r'C://Users//{}//Desktop//{}_RFPlots'.format(staName2, itemsGraph)
     if not os.path.exists(newpath):
         os.makedirs(newpath)

     # Show plot
     plt.savefig('C://Users//{}//Desktop//{}_RFPlots//Dissemination.png'.format(staName2, itemsGraph), bbox_inches='tight')

It produces this:

enter image description here

On the x-axis, the 'invalid' label is being cut off, which is weird as I have about 10 or so charts being produced, and none of the rest are doing it.

I have tried this link: Why is my xlabel cut off in my matplotlib plot?

And none of the solutions are working.

As I am writing this, I am thinking maybe it is do to with it matching the length of the other columns i.e. 4 digits long, but I am not setting the length anywhere, and as mentioned, I have other charts working fine.

Any help would be much apprecaited

Nicholas
  • 3,517
  • 13
  • 47
  • 86
  • Please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Without access to your dataframe, how can anybody figure out what the problem is besides just guessing? – Diziet Asahi May 18 '18 at 07:34
  • I thought there might be something obviously wrong..... I have asked over 70 questions, some way more complex than this question which people have solved without the full dataframe and code. Unfortunately I cannot provide a minimal, complete and verifiable example as the data is private and comes from my employers SQL database. Thank you anyway though :) – Nicholas May 18 '18 at 07:49
  • This could be an alignment issue. See if specifying `ha` or `va` in `ax.set_xticklabels` works. – MPA May 18 '18 at 07:50
  • Thank you MPA, I will give it a go now – Nicholas May 18 '18 at 07:50
  • Hmmm, I have been trying 'ax.set_xticklabels(ha='center')' or 'axDis.set_xticklabels(ha='center')'.... for some reason if I add that code, the graph doesnt run. – Nicholas May 18 '18 at 08:23
  • 1
    Does it raise an error? Or is the plot just blank? Also, because the labels are rotated, I'm not sure of you should use `ha` or `va` for the alignment perpendicular to the x-axis – MPA May 18 '18 at 08:27
  • It does raise an error, but I've hid the error with the exception. I'll be back at my desk in 5-10 mins and I'll find out what the error is. Thank you. And maybe you are right, as it's already rotated 90 – Nicholas May 18 '18 at 08:28
  • 1
    I have tried to reproduce your problem with synthetic data ([pastebin](https://pastebin.com/Nvu706ir)), and failed. I'm afraid that your code sample provides insufficient information to reproduce and resolve the problem. – MPA May 18 '18 at 09:55
  • Hey MPA, thank you for trying. Unfortunately I have been distracted at work by superiors so haven't had a moment to test it out again. To be honest, I think it might be something to do with the SQL code that goes into it as I cannot replicate the issue with the other charts. Thank you again and have a great weekend! – Nicholas May 18 '18 at 11:57

0 Answers0