0

I have prepared a code which uses imported modules with submodule as matplotlib. I have executed the following code in Jupyter. The code is as follows:

def run:
  plot_chart(a,b,c)
  print(10*'-')

run()

Output is as follows:

----------
<chart_graphics>

The plot_chart() calls matplotlib.pyplot to create the chart. When displaying the result, the output of print() displays before the output of plot_chart(). Why is that and What should I do to order it the same order as the code execution?

Note this is a repeat question from a previous question to which I have not received any replies.

The function plot_chart() calls the function plot_trunc_gr_model() in a module:

import numpy as np
import matplotlib.pyplot as plt
from openquake.hmtk.plotting.seismicity.catalogue_plots import \
        (get_completeness_adjusted_table, _save_image)
....
....
def plot_trunc_gr_model(aval, bval, min_mag, max_mag, dmag, catalogue=None,
        completeness=None, figure_size=None, filename=None, filetype='png', 
        dpi=300):
    """
    Plots a Gutenberg-Richter model
    """
    input_model = TruncatedGRMFD(min_mag, max_mag, dmag, aval, bval)
    if not catalogue:
        # Plot only the modelled recurrence
        annual_rates, cumulative_rates = _get_recurrence_model(input_model)
        plt.semilogy(annual_rates[:, 0], annual_rates[:, 1], 'b-')
        plt.semilogy(annual_rates[:, 0], cumulative_rates, 'r-')
        plt.xlabel('Magnitude', fontsize='large')
        plt.ylabel('Annual Rate', fontsize='large')
        plt.legend(['Incremental Rate', 'Cumulative Rate'])
        plt.savefig(filename, dpi=resolution, format=filetype)
    else:
        completeness = _check_completeness_table(completeness, catalogue)
        plot_recurrence_model(input_model,
                              catalogue,
                              completeness,
                              input_model.bin_width,
                              figure_size,
                              filename,
                              filetype,
                          dpi)

Edited- Solution is as follows I found the solution which had to do with the plt.savefig() function of matplotlib, which caused the incorrect sequence of output display. I use plt.show() and works flawlessly. So since this is a related question or some may say a similar question, how do you determine if this is a duplicate question?

Stephen Jacob
  • 889
  • 1
  • 15
  • 33
  • Does your plot_chart call plt.show? Please include that function (in minimal form). Also are you using matpoltlib directives? – MSeifert Apr 23 '18 at 04:46
  • @MSeifert I have added the part of the module which contains the function that is called from `plot_chart()` – Stephen Jacob Apr 23 '18 at 04:53

0 Answers0