我有一个函数可以精确生成所需的图形:
def graph(x, y, yName, dataset, colour, residuals = False, calc_tau = False):
"""
Takes as an argument two arrays, one for x and one y, a string for the
name of the y-variable, and two colours (which could be strings, values
or iterators).
Plots a scatter graph of the array against log(z), along with the best fit
line, its equation and its regression coefficient, as well as Kendall's tau
and the total number of points plotted.
"""
arrayresults, arrayresids, arrayparams, arrayfit, arrayequation, arraytau = computeLinearStats(x,
y,
yName, calc_tau)
count = np.count_nonzero(~np.logical_or(np.isnan(x), np.isnan(y)))
# arrayequation = 'r\'' + yName +arrayequation[arrayequation.index('='):]
plt.scatter(x, y,
# label = arrayequation,
s = 10,
alpha = 0.5,
c = colour)
if calc_tau: #if calc_tau is set to True, display the value
#in the legend along with equation, r and n
plt.plot(x, arrayfit,
label = r'''%s
r=%g
$\tau$=%g
n=%d'''%(arrayequation, arrayparams[2], arraytau[0], count),
c = colour)
else: #otherwise just display equation, r and n
plt.plot(x, arrayfit,
label = r'''%s
$r=%g$
$n=%d$
$r^2n=%.2f$'''%(arrayequation, arrayparams[2], count, arrayresults.nobs*arrayparams[2]**2),
c = colour)
legendfont = 16
labelfont = 20
plt.xlabel('$log_{10}(z)$', fontsize = labelfont)
plt.ylabel('Magnitude combination, %s dataset'%dataset, fontsize = labelfont)
plt.legend(fontsize = legendfont)
plt.xticks(fontsize = labelfont)
plt.yticks(fontsize = labelfont)
plt.grid(True, which = 'both')
# plt.title(r'The three best high-$r$ combinations in both Hunstead and MgII', fontsize = labelfont)
if residuals:
plotResids(x, y, yName, dataset, colour)
plt.show()
return arrayresults, arrayresids, arrayparams, arrayfit, arrayequation, arraytau
我可以多次调用生成,例如:
这是一种特殊功能,但是它产生了我希望它们看起来如何的图形。有没有一种简单的方法可以将通过多次调用函数输出的图形组合到一组子图中?我尝试过类似的
x = sources['z']
y1 = I-W2
y2 = W3-U
fig,ax = plt.subplots(2,1, sharex=True, sharey=False, gridspec_kw={'hspace': 0})
fig.suptitle('Graphs')
ax[0].scatter(x, y1, c='red', s = 10, alpha = 0.3)
ax[1].scatter(x, y2, c='purple', s = 10, alpha = 0.3)
但是我希望他们有所有的装备。理想情况下,我希望ax1
和ax2
调用我的绘图函数并以子图显示输出。这可能吗?
编辑:感谢您的评论,我可以使用
x = sources['z']
y1 = I-W2
y2 = W3-U
fig,ax = plt.subplots(2,1, sharex=True, sharey=True, gridspec_kw={'hspace': 0})
ax1 = qf.graph(x, y1, 'I-W2', dataset, 'blue', ax[0], residuals = False, calc_tau = False) #2 rows, 1 column, first plot
ax2 = qf.graph(x, y2, 'W3-U', dataset, 'red', ax[1], residuals = False, calc_tau = False) #2 rows, 1 column, second plot
fig.suptitle('Graphs')
生产
但是如何将一张图放到最高位置(ax [0])?