matplotlib y axis labels disappearing with fontsize change










1















I have the following code to plot the contents of a dataframe.



Using pandas and matplotlib:



thedata = '2013':[0.0,0.0]
,'2014':[0.0,0.0]
,'2015':[0.0,0.0]
,'2016':[1,0.0]
,'2017':[0.0,0.0]
,'2018':[1,0.0]

my_df = pd.DataFrame(thedata, index=['Green cars','Red cars'])

plt.figure(figsize=(7,3))
my_ax = plt.gca()
my_ax.clear()
my_ax.yaxis.set_major_locator(MaxNLocator(integer=True))

my_df.transpose().plot(kind='bar'
, stacked=True
, ax=my_ax
).grid(True,'major','y')

my_ax.legend(loc=9, bbox_to_anchor=(0.5, -0.1), frameon=False, ncol=2, fontsize=12 )
plt.title('All the cars', fontsize = 12 )
my_ax.set_xticklabels(my_ax.get_xticklabels(),rotation='horizontal', fontsize=12)
# my_ax.set_yticklabels(my_ax.get_yticklabels(),fontsize=12)


The dataframe and the output



The last line is commented out in order to get the output displayed. I want to increase the font size of the y axis labels to be the same as the x, but when i un-comment that line and run it, the y axis labels just disappear and nothing is displayed there.



Why is this happening and how do I fix it?



Edit: - create the dataframe; pandas 0.23.0, matplotlib 2.2.2










share|improve this question
























  • Can you make this reproducible by defining the dataframe inside your code and stating which version of matplotlib and pandas you are using? See Minimal, Complete, and Verifiable example.

    – ImportanceOfBeingErnest
    Nov 14 '18 at 23:28











  • Edited post to include this

    – user9588528
    Nov 15 '18 at 2:30















1















I have the following code to plot the contents of a dataframe.



Using pandas and matplotlib:



thedata = '2013':[0.0,0.0]
,'2014':[0.0,0.0]
,'2015':[0.0,0.0]
,'2016':[1,0.0]
,'2017':[0.0,0.0]
,'2018':[1,0.0]

my_df = pd.DataFrame(thedata, index=['Green cars','Red cars'])

plt.figure(figsize=(7,3))
my_ax = plt.gca()
my_ax.clear()
my_ax.yaxis.set_major_locator(MaxNLocator(integer=True))

my_df.transpose().plot(kind='bar'
, stacked=True
, ax=my_ax
).grid(True,'major','y')

my_ax.legend(loc=9, bbox_to_anchor=(0.5, -0.1), frameon=False, ncol=2, fontsize=12 )
plt.title('All the cars', fontsize = 12 )
my_ax.set_xticklabels(my_ax.get_xticklabels(),rotation='horizontal', fontsize=12)
# my_ax.set_yticklabels(my_ax.get_yticklabels(),fontsize=12)


The dataframe and the output



The last line is commented out in order to get the output displayed. I want to increase the font size of the y axis labels to be the same as the x, but when i un-comment that line and run it, the y axis labels just disappear and nothing is displayed there.



Why is this happening and how do I fix it?



Edit: - create the dataframe; pandas 0.23.0, matplotlib 2.2.2










share|improve this question
























  • Can you make this reproducible by defining the dataframe inside your code and stating which version of matplotlib and pandas you are using? See Minimal, Complete, and Verifiable example.

    – ImportanceOfBeingErnest
    Nov 14 '18 at 23:28











  • Edited post to include this

    – user9588528
    Nov 15 '18 at 2:30













1












1








1








I have the following code to plot the contents of a dataframe.



Using pandas and matplotlib:



thedata = '2013':[0.0,0.0]
,'2014':[0.0,0.0]
,'2015':[0.0,0.0]
,'2016':[1,0.0]
,'2017':[0.0,0.0]
,'2018':[1,0.0]

my_df = pd.DataFrame(thedata, index=['Green cars','Red cars'])

plt.figure(figsize=(7,3))
my_ax = plt.gca()
my_ax.clear()
my_ax.yaxis.set_major_locator(MaxNLocator(integer=True))

my_df.transpose().plot(kind='bar'
, stacked=True
, ax=my_ax
).grid(True,'major','y')

my_ax.legend(loc=9, bbox_to_anchor=(0.5, -0.1), frameon=False, ncol=2, fontsize=12 )
plt.title('All the cars', fontsize = 12 )
my_ax.set_xticklabels(my_ax.get_xticklabels(),rotation='horizontal', fontsize=12)
# my_ax.set_yticklabels(my_ax.get_yticklabels(),fontsize=12)


The dataframe and the output



The last line is commented out in order to get the output displayed. I want to increase the font size of the y axis labels to be the same as the x, but when i un-comment that line and run it, the y axis labels just disappear and nothing is displayed there.



Why is this happening and how do I fix it?



Edit: - create the dataframe; pandas 0.23.0, matplotlib 2.2.2










share|improve this question
















I have the following code to plot the contents of a dataframe.



Using pandas and matplotlib:



thedata = '2013':[0.0,0.0]
,'2014':[0.0,0.0]
,'2015':[0.0,0.0]
,'2016':[1,0.0]
,'2017':[0.0,0.0]
,'2018':[1,0.0]

my_df = pd.DataFrame(thedata, index=['Green cars','Red cars'])

plt.figure(figsize=(7,3))
my_ax = plt.gca()
my_ax.clear()
my_ax.yaxis.set_major_locator(MaxNLocator(integer=True))

my_df.transpose().plot(kind='bar'
, stacked=True
, ax=my_ax
).grid(True,'major','y')

my_ax.legend(loc=9, bbox_to_anchor=(0.5, -0.1), frameon=False, ncol=2, fontsize=12 )
plt.title('All the cars', fontsize = 12 )
my_ax.set_xticklabels(my_ax.get_xticklabels(),rotation='horizontal', fontsize=12)
# my_ax.set_yticklabels(my_ax.get_yticklabels(),fontsize=12)


The dataframe and the output



The last line is commented out in order to get the output displayed. I want to increase the font size of the y axis labels to be the same as the x, but when i un-comment that line and run it, the y axis labels just disappear and nothing is displayed there.



Why is this happening and how do I fix it?



Edit: - create the dataframe; pandas 0.23.0, matplotlib 2.2.2







python pandas matplotlib






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 2:01







user9588528

















asked Nov 14 '18 at 23:19









user9588528user9588528

269




269












  • Can you make this reproducible by defining the dataframe inside your code and stating which version of matplotlib and pandas you are using? See Minimal, Complete, and Verifiable example.

    – ImportanceOfBeingErnest
    Nov 14 '18 at 23:28











  • Edited post to include this

    – user9588528
    Nov 15 '18 at 2:30

















  • Can you make this reproducible by defining the dataframe inside your code and stating which version of matplotlib and pandas you are using? See Minimal, Complete, and Verifiable example.

    – ImportanceOfBeingErnest
    Nov 14 '18 at 23:28











  • Edited post to include this

    – user9588528
    Nov 15 '18 at 2:30
















Can you make this reproducible by defining the dataframe inside your code and stating which version of matplotlib and pandas you are using? See Minimal, Complete, and Verifiable example.

– ImportanceOfBeingErnest
Nov 14 '18 at 23:28





Can you make this reproducible by defining the dataframe inside your code and stating which version of matplotlib and pandas you are using? See Minimal, Complete, and Verifiable example.

– ImportanceOfBeingErnest
Nov 14 '18 at 23:28













Edited post to include this

– user9588528
Nov 15 '18 at 2:30





Edited post to include this

– user9588528
Nov 15 '18 at 2:30












1 Answer
1






active

oldest

votes


















1














The problem you face here is that the ticklabels are not actually defined before the figure is drawn.



import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator

fig, ax = plt.subplots()
ax.plot([1,3,4,2])
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
ax.set_xticklabels(ax.get_xticklabels(),fontsize=12)
plt.show()


enter image description here



You may draw the figure, before accessing those labels



fig, ax = plt.subplots()
ax.plot([1,3,4,2])
ax.xaxis.set_major_locator(MaxNLocator(integer=True))

fig.canvas.draw()
ax.set_xticklabels(ax.get_xticklabels(),fontsize=12)
plt.show()


enter image description here



However, in order to change the fontsize of the ticklabels, one would rather use



ax.tick_params(axis="x", labelsize=12)


or set the property to the labels, without actually setting the labels' content. E.g. via setp



plt.setp(ax.get_xticklabels(), fontsize=12)





share|improve this answer























  • Thank you, this works. Would you please explain why would one rather use tick_params() or setp()?

    – user9588528
    Nov 15 '18 at 4:35






  • 1





    If you set the fontsize, both are equally well suited. The difference is that you cannot set every property via tick_params. So for example, if you want to change the rotation_mode of a label, you cannot use tick_params. If then you want to change the fontsize and the rotation mode, using a single line with setp is preferable. Another argument: When you haven't imported pyplot, you won't do that exactly for use of setp. So in that case you can use tick_params or the longer variant of using a loop.

    – ImportanceOfBeingErnest
    Nov 15 '18 at 13:25











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%2f53310218%2fmatplotlib-y-axis-labels-disappearing-with-fontsize-change%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














The problem you face here is that the ticklabels are not actually defined before the figure is drawn.



import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator

fig, ax = plt.subplots()
ax.plot([1,3,4,2])
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
ax.set_xticklabels(ax.get_xticklabels(),fontsize=12)
plt.show()


enter image description here



You may draw the figure, before accessing those labels



fig, ax = plt.subplots()
ax.plot([1,3,4,2])
ax.xaxis.set_major_locator(MaxNLocator(integer=True))

fig.canvas.draw()
ax.set_xticklabels(ax.get_xticklabels(),fontsize=12)
plt.show()


enter image description here



However, in order to change the fontsize of the ticklabels, one would rather use



ax.tick_params(axis="x", labelsize=12)


or set the property to the labels, without actually setting the labels' content. E.g. via setp



plt.setp(ax.get_xticklabels(), fontsize=12)





share|improve this answer























  • Thank you, this works. Would you please explain why would one rather use tick_params() or setp()?

    – user9588528
    Nov 15 '18 at 4:35






  • 1





    If you set the fontsize, both are equally well suited. The difference is that you cannot set every property via tick_params. So for example, if you want to change the rotation_mode of a label, you cannot use tick_params. If then you want to change the fontsize and the rotation mode, using a single line with setp is preferable. Another argument: When you haven't imported pyplot, you won't do that exactly for use of setp. So in that case you can use tick_params or the longer variant of using a loop.

    – ImportanceOfBeingErnest
    Nov 15 '18 at 13:25















1














The problem you face here is that the ticklabels are not actually defined before the figure is drawn.



import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator

fig, ax = plt.subplots()
ax.plot([1,3,4,2])
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
ax.set_xticklabels(ax.get_xticklabels(),fontsize=12)
plt.show()


enter image description here



You may draw the figure, before accessing those labels



fig, ax = plt.subplots()
ax.plot([1,3,4,2])
ax.xaxis.set_major_locator(MaxNLocator(integer=True))

fig.canvas.draw()
ax.set_xticklabels(ax.get_xticklabels(),fontsize=12)
plt.show()


enter image description here



However, in order to change the fontsize of the ticklabels, one would rather use



ax.tick_params(axis="x", labelsize=12)


or set the property to the labels, without actually setting the labels' content. E.g. via setp



plt.setp(ax.get_xticklabels(), fontsize=12)





share|improve this answer























  • Thank you, this works. Would you please explain why would one rather use tick_params() or setp()?

    – user9588528
    Nov 15 '18 at 4:35






  • 1





    If you set the fontsize, both are equally well suited. The difference is that you cannot set every property via tick_params. So for example, if you want to change the rotation_mode of a label, you cannot use tick_params. If then you want to change the fontsize and the rotation mode, using a single line with setp is preferable. Another argument: When you haven't imported pyplot, you won't do that exactly for use of setp. So in that case you can use tick_params or the longer variant of using a loop.

    – ImportanceOfBeingErnest
    Nov 15 '18 at 13:25













1












1








1







The problem you face here is that the ticklabels are not actually defined before the figure is drawn.



import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator

fig, ax = plt.subplots()
ax.plot([1,3,4,2])
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
ax.set_xticklabels(ax.get_xticklabels(),fontsize=12)
plt.show()


enter image description here



You may draw the figure, before accessing those labels



fig, ax = plt.subplots()
ax.plot([1,3,4,2])
ax.xaxis.set_major_locator(MaxNLocator(integer=True))

fig.canvas.draw()
ax.set_xticklabels(ax.get_xticklabels(),fontsize=12)
plt.show()


enter image description here



However, in order to change the fontsize of the ticklabels, one would rather use



ax.tick_params(axis="x", labelsize=12)


or set the property to the labels, without actually setting the labels' content. E.g. via setp



plt.setp(ax.get_xticklabels(), fontsize=12)





share|improve this answer













The problem you face here is that the ticklabels are not actually defined before the figure is drawn.



import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator

fig, ax = plt.subplots()
ax.plot([1,3,4,2])
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
ax.set_xticklabels(ax.get_xticklabels(),fontsize=12)
plt.show()


enter image description here



You may draw the figure, before accessing those labels



fig, ax = plt.subplots()
ax.plot([1,3,4,2])
ax.xaxis.set_major_locator(MaxNLocator(integer=True))

fig.canvas.draw()
ax.set_xticklabels(ax.get_xticklabels(),fontsize=12)
plt.show()


enter image description here



However, in order to change the fontsize of the ticklabels, one would rather use



ax.tick_params(axis="x", labelsize=12)


or set the property to the labels, without actually setting the labels' content. E.g. via setp



plt.setp(ax.get_xticklabels(), fontsize=12)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 3:06









ImportanceOfBeingErnestImportanceOfBeingErnest

139k13162241




139k13162241












  • Thank you, this works. Would you please explain why would one rather use tick_params() or setp()?

    – user9588528
    Nov 15 '18 at 4:35






  • 1





    If you set the fontsize, both are equally well suited. The difference is that you cannot set every property via tick_params. So for example, if you want to change the rotation_mode of a label, you cannot use tick_params. If then you want to change the fontsize and the rotation mode, using a single line with setp is preferable. Another argument: When you haven't imported pyplot, you won't do that exactly for use of setp. So in that case you can use tick_params or the longer variant of using a loop.

    – ImportanceOfBeingErnest
    Nov 15 '18 at 13:25

















  • Thank you, this works. Would you please explain why would one rather use tick_params() or setp()?

    – user9588528
    Nov 15 '18 at 4:35






  • 1





    If you set the fontsize, both are equally well suited. The difference is that you cannot set every property via tick_params. So for example, if you want to change the rotation_mode of a label, you cannot use tick_params. If then you want to change the fontsize and the rotation mode, using a single line with setp is preferable. Another argument: When you haven't imported pyplot, you won't do that exactly for use of setp. So in that case you can use tick_params or the longer variant of using a loop.

    – ImportanceOfBeingErnest
    Nov 15 '18 at 13:25
















Thank you, this works. Would you please explain why would one rather use tick_params() or setp()?

– user9588528
Nov 15 '18 at 4:35





Thank you, this works. Would you please explain why would one rather use tick_params() or setp()?

– user9588528
Nov 15 '18 at 4:35




1




1





If you set the fontsize, both are equally well suited. The difference is that you cannot set every property via tick_params. So for example, if you want to change the rotation_mode of a label, you cannot use tick_params. If then you want to change the fontsize and the rotation mode, using a single line with setp is preferable. Another argument: When you haven't imported pyplot, you won't do that exactly for use of setp. So in that case you can use tick_params or the longer variant of using a loop.

– ImportanceOfBeingErnest
Nov 15 '18 at 13:25





If you set the fontsize, both are equally well suited. The difference is that you cannot set every property via tick_params. So for example, if you want to change the rotation_mode of a label, you cannot use tick_params. If then you want to change the fontsize and the rotation mode, using a single line with setp is preferable. Another argument: When you haven't imported pyplot, you won't do that exactly for use of setp. So in that case you can use tick_params or the longer variant of using a loop.

– ImportanceOfBeingErnest
Nov 15 '18 at 13:25



















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%2f53310218%2fmatplotlib-y-axis-labels-disappearing-with-fontsize-change%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

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus