我正在尋找使用該pyworld3模塊研究人口的演變。
為此,我輸入了我想要的引數和詳細資訊。我用我的代碼得到了我想要的結果。
這是我的代碼:
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)
這是我得到的輸出:
但是我想更改 x 軸的標題,并在圖表上添加一條曲線plt.plot。
我可以選擇我想賦予圖表的標題,因為其中有一個引數,plot_world_variables但沒有引數可以選擇 x 軸的標題。
因此,我嘗試使用plt.gcf()and進行這些更改plt.gca()。
這是我在之前的代碼之后添加的內容:
# 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()
添加此代碼時,我沒有收到錯誤訊息。事實上,我什么也得不到。運行代碼時沒有任何變化。Python 給我的輸出與我之前得到的輸出完全相同。
你認為這個問題來自哪里,我該如何解決?
PS:我看到以前有人問過和我一樣的問題,但是即使閱讀了他的帖子,我仍然無法弄清楚我的問題。
uj5u.com熱心網友回復:
可悲的是,plot_world_variables不回傳任何東西。一個快速而骯臟的解決方案:您可以輕松復制該函式的源代碼并應用必要的編輯。我已經看過了,沒有什么花哨的東西,很容易編輯:)
編輯:該函式的源代碼。
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()
現在您可以復制它并根據自己的喜好進行修改。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/468461.html
標籤:Python matplotlib 阴谋 定制
上一篇:Sympy圖顯示y軸的錯誤計算
