-1

In Python DataFrame, Im trying to generate histogram, it gets generated the first time when the function is called. However, when the create_histogram function is called second time it gets stuck at h = df.hist(bins=3, column="amount"). When I say "stuck", I mean to say that it does not finish executing the statement and the execution does not continue to the next line but at the same time it does not give any error or break out from the execution. What is exactly the problem here and how can I fix this?

import matplotlib.pyplot as plt
...
...
    def create_histogram(self, field):
        df = self.main_df    # This is DataFrame
        h = df.hist(bins=20, column="amount")
        fileContent = StringIO()
        plt.savefig(fileContent, dpi=None, facecolor='w', edgecolor='w',
                    orientation='portrait', papertype=None, format="png",
                    transparent=False, bbox_inches=None, pad_inches=0.5,
                    frameon=None)
        content = fileContent.getvalue()
        return content
Temp O'rary
  • 5,366
  • 13
  • 49
  • 109
  • 1
    This is going to be difficult to answer without a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Can you provide the minimal amount of code that one can copy&paste that reproduces the problem? – Diziet Asahi Oct 02 '18 at 09:37
  • Just for fun, I created a DataFrame, and run two successive call of `DataFrame.hist()` and had no problem, so for the moment, your problem is not reproducible – Diziet Asahi Oct 02 '18 at 09:38
  • Given in the question is the majority code related to that function. The remaining is code of an API call that calls this function. So, I've already given you the code. Since it was not reproducible to you, could it be the data size? or could it be anything else? Anything that you may guess of would be helpful. – Temp O'rary Oct 02 '18 at 12:44
  • @DizietAsahi, finally, I found the issue and fixed it (given in the answers section). Thank you for your comments. – Temp O'rary Oct 03 '18 at 12:11

1 Answers1

0

Finally I figured this out myself.

Whenever I executed the function I was always getting the following log message but I was ignoring it due to my lack of awareness.

Backend TkAgg is interactive backend. Turning interactive mode on.

But then I realised that may be its running in interactive mode (which was not my purpose). So, I found out that there is a way to turn it off, which is given below.

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt 

And this fixed my issue.

NOTE: the use should be called immediately after importing matplotlib in the sequence given here.

Temp O'rary
  • 5,366
  • 13
  • 49
  • 109