0

I'm newbie with matplotlib. I try to save a statistical graph which I generate with some data. I try to save it before to show the image of the graph.

The image that I can see is this:

enter image description here

And the image that I save in my hard disk is this:

enter image description here

The code where I define the background is this:

        if background_color is not None:
            self.background_color = background_color
        else:
            self.background_color = STATISTICS_GRAPH.BACKGROUND_COLOR
        if size is not None:
            self.size = size
        else:
            self.size = (STATISTICS_GRAPH.WIDTH, STATISTICS_GRAPH.HEIGHT)
        #We create the container of our statistical graphic
        self.fig, self.axes = plt.subplots(facecolor = self.background_color, 
        figsize=self.size)

And here the code where I save and show the image:

plt.savefig("files/images/processed/" + self.get_league_name() + "_" + self.get_competition_name() + ".png", dpi = 300)
plt.show()

So, what am I doing wrong to show the image with its background correctly and save it without the background color?

José Carlos
  • 2,850
  • 12
  • 59
  • 95

1 Answers1

1

You have to specify the face color when saving the figure as well, i.e.

plt.savefig("files/images/processed/" + self.get_league_name() + "_" + self.get_competition_name() + ".png", facecolor=self.background_color, dpi = 300)
plt.show()
William Miller
  • 9,839
  • 3
  • 25
  • 46
  • 1
    Hi @William Miller thank you so much for your appreaciated help!!! It works!!! – José Carlos Jan 22 '20 at 13:55
  • I keep seeing you answering a lot of questions that have been asked and answered before already. Maybe check out https://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled – ImportanceOfBeingErnest Jan 22 '20 at 14:07
  • @ImportanceOfBeingErnest Sorry, haven't been around as long as you and I'm not usually aware if a question is a duplicate - but I will try to be more mindful of it – William Miller Jan 22 '20 at 14:09
  • I'm usually also not aware of duplicates. There are two simple questions though you can use as guideline: (a) What are the odds this person is to first ever to have this kind of problem? (b) Why do I, myself, know the solution? The answers to those directly hint you at whether it would be worth doing a quick search - and since stackoverflow Q&A are usually on top of search engine results, you can directly find out about potential dupes. – ImportanceOfBeingErnest Jan 22 '20 at 14:35