Tight_Layout: Attribute Error . 'AxesSubplot' object has no attribute 'tight_layout'












1















I want to plot a stacked-bar graph with data coming from two separate dataframes in pandas (My_means and My_stds). I am using Python 3.7.



The plotting call works fine, however, the labels are cut-off. Setting tight_layout does not help me and generates an "Attribute Error: 'AxesSubplot' object has no attribute 'tight_layout'". What does this mean and how do I overcome this?



I haven´t found any problems related to this error, so I assume I am doing something rather stupid...



Here is my code:



My_means= ratios.pivot_table('Data', 'Sample ID', 'User ID', aggfunc= np.mean)
My_stds= ratios.pivot_table('Data', 'Sample ID', 'User ID', aggfunc= np.std)
plot_it = My_means.plot(kind='bar', color=stack_cols, stacked=True, yerr=My_stds, capsize=3, title='Just some graph')
plot_it.tight_layout()


Thanks for your help!
Jazz










share|improve this question























  • You can't apply tight_layout on an axes, you ned to do it on a figure. Most often done using plt.tight_layout()

    – DavidG
    Nov 20 '18 at 10:37











  • Hi David, thanks for the quick reply. I thought the way I do it, plot_it would be my figure. However, it seems that it isnt... (new to coding, sorry). How can I get it as my figure object so that tight_layout would work for me?

    – JazzyJazz
    Nov 20 '18 at 10:48











  • plot_it is the axes, not the figure. If you have done import matplotlib.pyplot as plt then you can simply do plt.tight_layout()

    – DavidG
    Nov 20 '18 at 10:52













  • It works fine! Thanks for you help. Appreciate it!

    – JazzyJazz
    Nov 20 '18 at 10:57
















1















I want to plot a stacked-bar graph with data coming from two separate dataframes in pandas (My_means and My_stds). I am using Python 3.7.



The plotting call works fine, however, the labels are cut-off. Setting tight_layout does not help me and generates an "Attribute Error: 'AxesSubplot' object has no attribute 'tight_layout'". What does this mean and how do I overcome this?



I haven´t found any problems related to this error, so I assume I am doing something rather stupid...



Here is my code:



My_means= ratios.pivot_table('Data', 'Sample ID', 'User ID', aggfunc= np.mean)
My_stds= ratios.pivot_table('Data', 'Sample ID', 'User ID', aggfunc= np.std)
plot_it = My_means.plot(kind='bar', color=stack_cols, stacked=True, yerr=My_stds, capsize=3, title='Just some graph')
plot_it.tight_layout()


Thanks for your help!
Jazz










share|improve this question























  • You can't apply tight_layout on an axes, you ned to do it on a figure. Most often done using plt.tight_layout()

    – DavidG
    Nov 20 '18 at 10:37











  • Hi David, thanks for the quick reply. I thought the way I do it, plot_it would be my figure. However, it seems that it isnt... (new to coding, sorry). How can I get it as my figure object so that tight_layout would work for me?

    – JazzyJazz
    Nov 20 '18 at 10:48











  • plot_it is the axes, not the figure. If you have done import matplotlib.pyplot as plt then you can simply do plt.tight_layout()

    – DavidG
    Nov 20 '18 at 10:52













  • It works fine! Thanks for you help. Appreciate it!

    – JazzyJazz
    Nov 20 '18 at 10:57














1












1








1








I want to plot a stacked-bar graph with data coming from two separate dataframes in pandas (My_means and My_stds). I am using Python 3.7.



The plotting call works fine, however, the labels are cut-off. Setting tight_layout does not help me and generates an "Attribute Error: 'AxesSubplot' object has no attribute 'tight_layout'". What does this mean and how do I overcome this?



I haven´t found any problems related to this error, so I assume I am doing something rather stupid...



Here is my code:



My_means= ratios.pivot_table('Data', 'Sample ID', 'User ID', aggfunc= np.mean)
My_stds= ratios.pivot_table('Data', 'Sample ID', 'User ID', aggfunc= np.std)
plot_it = My_means.plot(kind='bar', color=stack_cols, stacked=True, yerr=My_stds, capsize=3, title='Just some graph')
plot_it.tight_layout()


Thanks for your help!
Jazz










share|improve this question














I want to plot a stacked-bar graph with data coming from two separate dataframes in pandas (My_means and My_stds). I am using Python 3.7.



The plotting call works fine, however, the labels are cut-off. Setting tight_layout does not help me and generates an "Attribute Error: 'AxesSubplot' object has no attribute 'tight_layout'". What does this mean and how do I overcome this?



I haven´t found any problems related to this error, so I assume I am doing something rather stupid...



Here is my code:



My_means= ratios.pivot_table('Data', 'Sample ID', 'User ID', aggfunc= np.mean)
My_stds= ratios.pivot_table('Data', 'Sample ID', 'User ID', aggfunc= np.std)
plot_it = My_means.plot(kind='bar', color=stack_cols, stacked=True, yerr=My_stds, capsize=3, title='Just some graph')
plot_it.tight_layout()


Thanks for your help!
Jazz







python matplotlib layout label attributeerror






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 10:34









JazzyJazzJazzyJazz

84




84













  • You can't apply tight_layout on an axes, you ned to do it on a figure. Most often done using plt.tight_layout()

    – DavidG
    Nov 20 '18 at 10:37











  • Hi David, thanks for the quick reply. I thought the way I do it, plot_it would be my figure. However, it seems that it isnt... (new to coding, sorry). How can I get it as my figure object so that tight_layout would work for me?

    – JazzyJazz
    Nov 20 '18 at 10:48











  • plot_it is the axes, not the figure. If you have done import matplotlib.pyplot as plt then you can simply do plt.tight_layout()

    – DavidG
    Nov 20 '18 at 10:52













  • It works fine! Thanks for you help. Appreciate it!

    – JazzyJazz
    Nov 20 '18 at 10:57



















  • You can't apply tight_layout on an axes, you ned to do it on a figure. Most often done using plt.tight_layout()

    – DavidG
    Nov 20 '18 at 10:37











  • Hi David, thanks for the quick reply. I thought the way I do it, plot_it would be my figure. However, it seems that it isnt... (new to coding, sorry). How can I get it as my figure object so that tight_layout would work for me?

    – JazzyJazz
    Nov 20 '18 at 10:48











  • plot_it is the axes, not the figure. If you have done import matplotlib.pyplot as plt then you can simply do plt.tight_layout()

    – DavidG
    Nov 20 '18 at 10:52













  • It works fine! Thanks for you help. Appreciate it!

    – JazzyJazz
    Nov 20 '18 at 10:57

















You can't apply tight_layout on an axes, you ned to do it on a figure. Most often done using plt.tight_layout()

– DavidG
Nov 20 '18 at 10:37





You can't apply tight_layout on an axes, you ned to do it on a figure. Most often done using plt.tight_layout()

– DavidG
Nov 20 '18 at 10:37













Hi David, thanks for the quick reply. I thought the way I do it, plot_it would be my figure. However, it seems that it isnt... (new to coding, sorry). How can I get it as my figure object so that tight_layout would work for me?

– JazzyJazz
Nov 20 '18 at 10:48





Hi David, thanks for the quick reply. I thought the way I do it, plot_it would be my figure. However, it seems that it isnt... (new to coding, sorry). How can I get it as my figure object so that tight_layout would work for me?

– JazzyJazz
Nov 20 '18 at 10:48













plot_it is the axes, not the figure. If you have done import matplotlib.pyplot as plt then you can simply do plt.tight_layout()

– DavidG
Nov 20 '18 at 10:52







plot_it is the axes, not the figure. If you have done import matplotlib.pyplot as plt then you can simply do plt.tight_layout()

– DavidG
Nov 20 '18 at 10:52















It works fine! Thanks for you help. Appreciate it!

– JazzyJazz
Nov 20 '18 at 10:57





It works fine! Thanks for you help. Appreciate it!

– JazzyJazz
Nov 20 '18 at 10:57












1 Answer
1






active

oldest

votes


















1














My_means.plot(…) returns an axes object. Whereas tight_layout requires a figure object. There are a number of different approaches you can use:



Perhaps the simplest one would be to use plt.tight_layout() which works on the current figure:



import matplotlib.pyplot as plt
# Your plotting code here
plt.tight_layout()


Alternatively, you can create the figure and axes beforehand, pass the axes as an argument to plot and then use tight_layout on the figure object:



import matplotlib.pyplot as plt

fig, ax = plt.subplots()
plot_it = My_means.plot(kind='bar', ax=ax, …)

fig.tight_layout()





share|improve this answer
























  • Thanks again. It works (and looks) fine now!

    – JazzyJazz
    Nov 20 '18 at 11:09











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391086%2ftight-layout-attribute-error-axessubplot-object-has-no-attribute-tight-lay%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














My_means.plot(…) returns an axes object. Whereas tight_layout requires a figure object. There are a number of different approaches you can use:



Perhaps the simplest one would be to use plt.tight_layout() which works on the current figure:



import matplotlib.pyplot as plt
# Your plotting code here
plt.tight_layout()


Alternatively, you can create the figure and axes beforehand, pass the axes as an argument to plot and then use tight_layout on the figure object:



import matplotlib.pyplot as plt

fig, ax = plt.subplots()
plot_it = My_means.plot(kind='bar', ax=ax, …)

fig.tight_layout()





share|improve this answer
























  • Thanks again. It works (and looks) fine now!

    – JazzyJazz
    Nov 20 '18 at 11:09
















1














My_means.plot(…) returns an axes object. Whereas tight_layout requires a figure object. There are a number of different approaches you can use:



Perhaps the simplest one would be to use plt.tight_layout() which works on the current figure:



import matplotlib.pyplot as plt
# Your plotting code here
plt.tight_layout()


Alternatively, you can create the figure and axes beforehand, pass the axes as an argument to plot and then use tight_layout on the figure object:



import matplotlib.pyplot as plt

fig, ax = plt.subplots()
plot_it = My_means.plot(kind='bar', ax=ax, …)

fig.tight_layout()





share|improve this answer
























  • Thanks again. It works (and looks) fine now!

    – JazzyJazz
    Nov 20 '18 at 11:09














1












1








1







My_means.plot(…) returns an axes object. Whereas tight_layout requires a figure object. There are a number of different approaches you can use:



Perhaps the simplest one would be to use plt.tight_layout() which works on the current figure:



import matplotlib.pyplot as plt
# Your plotting code here
plt.tight_layout()


Alternatively, you can create the figure and axes beforehand, pass the axes as an argument to plot and then use tight_layout on the figure object:



import matplotlib.pyplot as plt

fig, ax = plt.subplots()
plot_it = My_means.plot(kind='bar', ax=ax, …)

fig.tight_layout()





share|improve this answer













My_means.plot(…) returns an axes object. Whereas tight_layout requires a figure object. There are a number of different approaches you can use:



Perhaps the simplest one would be to use plt.tight_layout() which works on the current figure:



import matplotlib.pyplot as plt
# Your plotting code here
plt.tight_layout()


Alternatively, you can create the figure and axes beforehand, pass the axes as an argument to plot and then use tight_layout on the figure object:



import matplotlib.pyplot as plt

fig, ax = plt.subplots()
plot_it = My_means.plot(kind='bar', ax=ax, …)

fig.tight_layout()






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 10:57









DavidGDavidG

10.8k103041




10.8k103041













  • Thanks again. It works (and looks) fine now!

    – JazzyJazz
    Nov 20 '18 at 11:09



















  • Thanks again. It works (and looks) fine now!

    – JazzyJazz
    Nov 20 '18 at 11:09

















Thanks again. It works (and looks) fine now!

– JazzyJazz
Nov 20 '18 at 11:09





Thanks again. It works (and looks) fine now!

– JazzyJazz
Nov 20 '18 at 11:09


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391086%2ftight-layout-attribute-error-axessubplot-object-has-no-attribute-tight-lay%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$