Search code examples
pythonmatplotlibplotcustomization

Why can't I change the title of the x-axis and add a curve to a graph in a module containing a plotting function?


I am looking to study the evolution of the population using the pyworld3 module.

For this I entered the parameters and details I wanted. I get the result I wanted with my code.

Here is my code:

import pyworld3
from pyworld3 import World3
import numpy as np
import matplotlib.pyplot as plt
from pyworld3.utils import plot_world_variables

world3 = World3(year_min=1951, year_max=2100, dt=1)
world3.init_world3_constants(p1i=92e7,
                         p2i=70e7, p3i=19e7, p4i=6e7,
                          dcfsn=3,
                         fcest=4000, hsid=20, ieat=3, 
                         len=42, # life expectancy normal. 
                          lpd=20, mtfn=12, pet=4000, rlt=30, sad=20,
                          zpgt=4000, ici=2.1e11, sci=1.44e11, iet=4000, 
                          iopcd=400,lfpf=0.75, lufdt=2, icor1=3, icor2=3, 
                          scor1=1, 
                          scor2=1, alic1=14, alic2=14, alsc1=20, alsc2=20,
                          fioac1=0.43, fioac2=0.43,
                          ali=0.9e9, pali=2.3e9, lfh=0.7, palt=3.2e9,
                          pl=0.1, alai1=2, alai2=2, io70=7.9e11, lyf1=1,
                          lyf2=1, sd=0.07, uili=8.2e6, alln=6000, uildt=10,
                          lferti=600, ilf=600, fspd=2, sfpc=230,
                          ppoli=2.5e7, ppol70=1.36e8, ahl70=1.5, amti=1,
                          imti=10, imef=0.1, fipm=0.001, frpm=0.02,
                          ppgf1=1, ppgf2=1, ppgf21=1, pptd1=20, pptd2=20,
                          nri=1e12, nruf1=1, nruf2=1)
world3.init_world3_variables()
world3.set_world3_table_functions(json_file=None)
world3.set_world3_delay_functions(method= 'odeint')
world3.run_world3()
plot_world_variables(world3.time,
                     [world3.nrfr, world3.iopc, world3.fpc, world3.pop,
                      world3.ppolx],
                     ["NRFR", "IOPC", "FPC", "POP", "PPOLX"],
                     [[0, 1], [0, 1e3], [0, 1e3], [5e9, 12e9], [0, 32]],
                    # img_background="./img/fig7-7.png",
                     figsize=(12, 8),
                     title="Evolution of the world population",
                     grid=True)

Here is the output I get: output

However I would like to change the title of the x-axis and also add a curve on the graph with plt.plot.

I can choose the title I want to give to the graph because there is an argument for that in plot_world_variables but there is no argument to choose the title of the x-axis.

So I tried to make these changes with plt.gcf() and plt.gca().

Here is what I added after my previous code:

# First we get its Axes:
axes: plt.Axes = plt.gcf().gca()
# On it, we can plot:
X = np.linspace(-2, 0, 100)
Y = X2*2-1
axes.plot(X2, Y2, label="another curve")
plt.legend()
# And adjust things:
axes.set_xlabel("Year")
plt.show()

I don't get an error when adding this code. In fact, I get nothing at all. Nothing changes when I run the code. Python gives me exactly the same output as the one I got before.

Where do you think this problem comes from and how can I fix it?

P.S.: I saw that someone had asked the same question as me formerly but even reading his post I still can't figure out my problem.


Solution

  • Sadly, plot_world_variables doesn't return anything. A quick and dirty solution: you can easily copy the source code of that function and apply the necessary edits. I've looked at it and there is nothing fancy going on, easy edit to do :)

    EDIT: source code of that function.

    from matplotlib.ticker import EngFormatter
    from matplotlib.image import imread
    from numpy import isnan
    import matplotlib.pyplot as plt
    
    def plot_world_variables(time, var_data, var_names, var_lims,
                             img_background=None,
                             title=None,
                             figsize=None,
                             dist_spines=0.09,
                             grid=False):
        """
        Plots world state from an instance of World3 or any single sector.
    
        """
        prop_cycle = plt.rcParams['axes.prop_cycle']
        colors = prop_cycle.by_key()['color']
    
        var_number = len(var_data)
    
        fig, host = plt.subplots(figsize=figsize)
        axs = [host, ]
        for i in range(var_number-1):
            axs.append(host.twinx())
    
        fig.subplots_adjust(left=dist_spines*2)
        for i, ax in enumerate(axs[1:]):
            ax.spines["left"].set_position(("axes", -(i + 1)*dist_spines))
            ax.spines["left"].set_visible(True)
            ax.yaxis.set_label_position('left')
            ax.yaxis.set_ticks_position('left')
    
        if img_background is not None:
            im = imread(img_background)
            axs[0].imshow(im, aspect="auto",
                          extent=[time[0], time[-1],
                                  var_lims[0][0], var_lims[0][1]], cmap="gray")
    
        ps = []
        for ax, label, ydata, color in zip(axs, var_names, var_data, colors):
            ps.append(ax.plot(time, ydata, label=label, color=color)[0])
        axs[0].grid(grid)
        axs[0].set_xlim(time[0], time[-1])
    
        for ax, lim in zip(axs, var_lims):
            ax.set_ylim(lim[0], lim[1])
    
        for ax_ in axs:
            formatter_ = EngFormatter(places=0, sep="\N{THIN SPACE}")
            ax_.tick_params(axis='y', rotation=90)
            ax_.yaxis.set_major_locator(plt.MaxNLocator(5))
            ax_.yaxis.set_major_formatter(formatter_)
    
        tkw = dict(size=4, width=1.5)
        axs[0].set_xlabel("time [years] asd")
        axs[0].tick_params(axis='x', **tkw)
        for i, (ax, p) in enumerate(zip(axs, ps)):
            ax.set_ylabel(p.get_label(), rotation="horizontal")
            ax.yaxis.label.set_color(p.get_color())
            ax.tick_params(axis='y', colors=p.get_color(), **tkw)
            ax.yaxis.set_label_coords(-i*dist_spines, 1.01)
    
        if title is not None:
            fig.suptitle(title, x=0.95, ha="right", fontsize=10)
    
        plt.tight_layout()
    

    Now you can copy it and modify it to your liking.