blandaltman.py
def _drawBlandAltman(mean, diff, md, sd, percentage, limitOfAgreement, confidenceIntervals, detrend, title, ax, figureSize, dpi, savePath, figureFormat, meanColour, loaColour, pointColour):
	"""
	Sub function to draw the plot.
	"""
	if ax is None:
		fig, ax = plt.subplots(1,1, figsize=figureSize, dpi=dpi)
		plt.rcParams.update({'font.size': 15,'xtick.labelsize':15,
         'ytick.labelsize':15})

		ax.tick_params(axis='x', labelsize=15)
		ax.tick_params(axis='y', labelsize=15)
		# ax.rcParams.update({'font.size': 15})

		# ax=ax[0,0]
		draw = True
	else:
		draw = False

	##
	# Plot CIs if calculated
	##
	if 'mean' in confidenceIntervals.keys():
		ax.axhspan(confidenceIntervals['mean'][0],
				   confidenceIntervals['mean'][1],
				   facecolor='lightblue', alpha=0.2)

	if 'upperLoA' in confidenceIntervals.keys():
		ax.axhspan(confidenceIntervals['upperLoA'][0],
				   confidenceIntervals['upperLoA'][1],
				   facecolor='wheat', alpha=0.2)

	if 'lowerLoA' in confidenceIntervals.keys():
		ax.axhspan(confidenceIntervals['lowerLoA'][0],
				   confidenceIntervals['lowerLoA'][1],
				   facecolor='wheat', alpha=0.2)

	##
	# Plot the mean diff and LoA
	##
	ax.axhline(md, color=meanColour, linestyle='--')
	ax.axhline(md + limitOfAgreement*sd, color=loaColour, linestyle='--')
	ax.axhline(md - limitOfAgreement*sd, color=loaColour, linestyle='--')

	##
	# Plot the data points
	##
	# ax.scatter(mean[0:22], diff[0:22], alpha=0.8,  c='orange', marker='.', s=100, label='India Male')
	# ax.scatter(mean[22:44], diff[22:44], alpha=0.8,  c='blue', marker='.', s=100, label='India Female')
	# ax.scatter(mean[44:66], diff[44:66], alpha=0.8,  c='red', marker='.', s=100, label='Sierra Leone Male')
	# ax.scatter(mean[66:88], diff[66:88], alpha=0.8,  c='purple', marker='.', s=100, label='Sierra Leone Female')
	ax.scatter(mean[0:20], diff[0:20], alpha=0.8,  c='orange', marker='.', s=100, label='India Male')
	ax.scatter(mean[20:39], diff[20:39], alpha=0.8,  c='blue', marker='.', s=100, label='India Female')
	ax.scatter(mean[39:59], diff[39:59], alpha=0.8,  c='red', marker='.', s=100, label='Sierra Leone Male')
	ax.scatter(mean[59:77], diff[59:77], alpha=0.8,  c='purple', marker='.', s=100, label='Sierra Leone Female')
	ax.set_ylim(-50, 70)
	ax.legend(loc='upper right', fontsize=12)
	trans = transforms.blended_transform_factory(
		ax.transAxes, ax.transData)

	limitOfAgreementRange = (md + (limitOfAgreement * sd)) - (md - limitOfAgreement*sd)
	offset = (limitOfAgreementRange / 100.0) * 1.5

	ax.text(0.98, md + offset, 'Mean', ha="right", va="bottom", transform=trans)
	ax.text(0.98, md - offset, f'{md:.2f}', ha="right", va="top", transform=trans)

	ax.text(0.98, md + (limitOfAgreement * sd) + offset, f'+{limitOfAgreement:.2f} SD', ha="right", va="bottom", transform=trans)
	ax.text(0.98, md + (limitOfAgreement * sd) - offset, f'{md + limitOfAgreement*sd:.2f}', ha="right", va="top", transform=trans)

	ax.text(0.98, md - (limitOfAgreement * sd) - offset, f'-{limitOfAgreement:.2f} SD', ha="right", va="top", transform=trans)
	ax.text(0.98, md - (limitOfAgreement * sd) + offset, f'{md - limitOfAgreement*sd:.2f}', ha="right", va="bottom", transform=trans)

	# Only draw spine between extent of the data
	# ax.spines['left'].set_bounds(min(diff), max(diff))
	# ax.spines['bottom'].set_bounds(min(mean), max(mean))

	# Hide the right and top spines
	# ax.spines['right'].set_visible(False)
	# ax.spines['top'].set_visible(False)

	if percentage:
		ax.set_ylabel('Percentage difference between methods', fontsize=20)
	else:
		ax.set_ylabel('Difference between methods', fontsize=20)
	ax.set_xlabel('Mean of methods', fontsize=20)

	# tickLocs = ax.xaxis.get_ticklocs()
	# cadenceX = tickLocs[2] - tickLocs[1]
	# tickLocs = rangeFrameLocator(tickLocs, (min(mean), max(mean)))
	# ax.xaxis.set_major_locator(ticker.FixedLocator(tickLocs))

	# tickLocs = ax.yaxis.get_ticklocs()
	# cadenceY = tickLocs[2] - tickLocs[1]
	# tickLocs = rangeFrameLocator(tickLocs, (min(diff), max(diff)))
	# ax.yaxis.set_major_locator(ticker.FixedLocator(tickLocs))

	# plt.draw() # Force drawing to populate tick labels

	# labels = rangeFrameLabler(ax.xaxis.get_ticklocs(), [item.get_text() for item in ax.get_xticklabels()], cadenceX)
	# ax.set_xticklabels(labels)

	# labels = rangeFrameLabler(ax.yaxis.get_ticklocs(), [item.get_text() for item in ax.get_yticklabels()], cadenceY)
	# ax.set_yticklabels(labels)


	# ax.patch.set_alpha(0)

	if detrend[0] is None:
		pass
	else:
		plt.text(1, -0.1, f'{detrend[0]} slope correction factor: {detrend[1]:.2f} ± {detrend[2]:.2f}', ha='right', transform=ax.transAxes)

	if title:
		ax.set_title(title)

	##
	# Save or draw
	##
	plt.tight_layout()

	if (savePath is not None) & draw:
		fig.savefig(savePath, format=figureFormat, dpi=dpi)
		plt.close()
	elif draw:
		plt.show()
	else:
		return ax

	Sub function to draw the plot.
	
Sub function to draw the plot.
Sub function to draw the plot .
def _drawBlandAltman(mean, diff, md, sd, percentage, limitOfAgreement, confidenceIntervals, detrend, title, ax, figureSize, dpi, savePath, figureFormat, meanColour, loaColour, pointColour):
	if ax is None:
		fig, ax = plt.subplots(1,1, figsize=figureSize, dpi=dpi)
		plt.rcParams.update({'font.size': 15,'xtick.labelsize':15,
         'ytick.labelsize':15})
		ax.tick_params(axis='x', labelsize=15)
		ax.tick_params(axis='y', labelsize=15)
		draw = True
	else:
		draw = False
	if 'mean' in confidenceIntervals.keys():
		ax.axhspan(confidenceIntervals['mean'][0],
				   confidenceIntervals['mean'][1],
				   facecolor='lightblue', alpha=0.2)
	if 'upperLoA' in confidenceIntervals.keys():
		ax.axhspan(confidenceIntervals['upperLoA'][0],
				   confidenceIntervals['upperLoA'][1],
				   facecolor='wheat', alpha=0.2)
	if 'lowerLoA' in confidenceIntervals.keys():
		ax.axhspan(confidenceIntervals['lowerLoA'][0],
				   confidenceIntervals['lowerLoA'][1],
				   facecolor='wheat', alpha=0.2)
	ax.axhline(md, color=meanColour, linestyle='--')
	ax.axhline(md + limitOfAgreement*sd, color=loaColour, linestyle='--')
	ax.axhline(md - limitOfAgreement*sd, color=loaColour, linestyle='--')
	ax.scatter(mean[0:20], diff[0:20], alpha=0.8,  c='orange', marker='.', s=100, label='India Male')
	ax.scatter(mean[20:39], diff[20:39], alpha=0.8,  c='blue', marker='.', s=100, label='India Female')
	ax.scatter(mean[39:59], diff[39:59], alpha=0.8,  c='red', marker='.', s=100, label='Sierra Leone Male')
	ax.scatter(mean[59:77], diff[59:77], alpha=0.8,  c='purple', marker='.', s=100, label='Sierra Leone Female')
	ax.set_ylim(-50, 70)
	ax.legend(loc='upper right', fontsize=12)
	trans = transforms.blended_transform_factory(
		ax.transAxes, ax.transData)
	limitOfAgreementRange = (md + (limitOfAgreement * sd)) - (md - limitOfAgreement*sd)
	offset = (limitOfAgreementRange / 100.0) * 1.5
	ax.text(0.98, md + offset, 'Mean', ha="right", va="bottom", transform=trans)
	ax.text(0.98, md - offset, f'{md:.2f}', ha="right", va="top", transform=trans)
	ax.text(0.98, md + (limitOfAgreement * sd) + offset, f'+{limitOfAgreement:.2f} SD', ha="right", va="bottom", transform=trans)
	ax.text(0.98, md + (limitOfAgreement * sd) - offset, f'{md + limitOfAgreement*sd:.2f}', ha="right", va="top", transform=trans)
	ax.text(0.98, md - (limitOfAgreement * sd) - offset, f'-{limitOfAgreement:.2f} SD', ha="right", va="top", transform=trans)
	ax.text(0.98, md - (limitOfAgreement * sd) + offset, f'{md - limitOfAgreement*sd:.2f}', ha="right", va="bottom", transform=trans)
	if percentage:
		ax.set_ylabel('Percentage difference between methods', fontsize=20)
	else:
		ax.set_ylabel('Difference between methods', fontsize=20)
	ax.set_xlabel('Mean of methods', fontsize=20)
	if detrend[0] is None:
		pass
	else:
		plt.text(1, -0.1, f'{detrend[0]} slope correction factor: {detrend[1]:.2f} ± {detrend[2]:.2f}', ha='right', transform=ax.transAxes)
	if title:
		ax.set_title(title)
	plt.tight_layout()
	if (savePath is not None) & draw:
		fig.savefig(savePath, format=figureFormat, dpi=dpi)
		plt.close()
	elif draw:
		plt.show()
	else:
		return ax
def _drawBlandAltman ( mean , diff , md , sd , percentage , limitOfAgreement , confidenceIntervals , detrend , title , ax , figureSize , dpi , savePath , figureFormat , meanColour , loaColour , pointColour ) : if ax is None : fig , ax = plt . subplots ( 1 , 1 , figsize = figureSize , dpi = dpi ) plt . rcParams . update ( { 'font.size' : 15 , 'xtick.labelsize' : 15 , 'ytick.labelsize' : 15 } ) ax . tick_params ( axis = 'x' , labelsize = 15 ) ax . tick_params ( axis = 'y' , labelsize = 15 ) draw = True else : draw = False if 'mean' in confidenceIntervals . keys ( ) : ax . axhspan ( confidenceIntervals [ 'mean' ] [ 0 ] , confidenceIntervals [ 'mean' ] [ 1 ] , facecolor = 'lightblue' , alpha = 0.2 ) if 'upperLoA' in confidenceIntervals . keys ( ) : ax . axhspan ( confidenceIntervals [ 'upperLoA' ] [ 0 ] , confidenceIntervals [ 'upperLoA' ] [ 1 ] , facecolor = 'wheat' , alpha = 0.2 ) if 'lowerLoA' in confidenceIntervals . keys ( ) : ax . axhspan ( confidenceIntervals [ 'lowerLoA' ] [ 0 ] , confidenceIntervals [ 'lowerLoA' ] [ 1 ] , facecolor = 'wheat' , alpha = 0.2 ) ax . axhline ( md , color = meanColour , linestyle = '--' ) ax . axhline ( md + limitOfAgreement * sd , color = loaColour , linestyle = '--' ) ax . axhline ( md - limitOfAgreement * sd , color = loaColour , linestyle = '--' ) ax . scatter ( mean [ 0 : 20 ] , diff [ 0 : 20 ] , alpha = 0.8 , c = 'orange' , marker = '.' , s = 100 , label = 'India Male' ) ax . scatter ( mean [ 20 : 39 ] , diff [ 20 : 39 ] , alpha = 0.8 , c = 'blue' , marker = '.' , s = 100 , label = 'India Female' ) ax . scatter ( mean [ 39 : 59 ] , diff [ 39 : 59 ] , alpha = 0.8 , c = 'red' , marker = '.' , s = 100 , label = 'Sierra Leone Male' ) ax . scatter ( mean [ 59 : 77 ] , diff [ 59 : 77 ] , alpha = 0.8 , c = 'purple' , marker = '.' , s = 100 , label = 'Sierra Leone Female' ) ax . set_ylim ( - 50 , 70 ) ax . legend ( loc = 'upper right' , fontsize = 12 ) trans = transforms . blended_transform_factory ( ax . transAxes , ax . transData ) limitOfAgreementRange = ( md + ( limitOfAgreement * sd ) ) - ( md - limitOfAgreement * sd ) offset = ( limitOfAgreementRange / 100.0 ) * 1.5 ax . text ( 0.98 , md + offset , 'Mean' , ha = "right" , va = "bottom" , transform = trans ) ax . text ( 0.98 , md - offset , f'{md:.2f}' , ha = "right" , va = "top" , transform = trans ) ax . text ( 0.98 , md + ( limitOfAgreement * sd ) + offset , f'+{limitOfAgreement:.2f} SD' , ha = "right" , va = "bottom" , transform = trans ) ax . text ( 0.98 , md + ( limitOfAgreement * sd ) - offset , f'{md + limitOfAgreement*sd:.2f}' , ha = "right" , va = "top" , transform = trans ) ax . text ( 0.98 , md - ( limitOfAgreement * sd ) - offset , f'-{limitOfAgreement:.2f} SD' , ha = "right" , va = "top" , transform = trans ) ax . text ( 0.98 , md - ( limitOfAgreement * sd ) + offset , f'{md - limitOfAgreement*sd:.2f}' , ha = "right" , va = "bottom" , transform = trans ) if percentage : ax . set_ylabel ( 'Percentage difference between methods' , fontsize = 20 ) else : ax . set_ylabel ( 'Difference between methods' , fontsize = 20 ) ax . set_xlabel ( 'Mean of methods' , fontsize = 20 ) if detrend [ 0 ] is None : pass else : plt . text ( 1 , - 0.1 , f'{detrend[0]} slope correction factor: {detrend[1]:.2f} ± {detrend[2]:.2f}', a= ' right', ransform= a x. t ransAxes) if title : ax . set_title ( title ) plt . tight_layout ( ) if ( savePath is not None ) & draw : fig . savefig ( savePath , format = figureFormat , dpi = dpi ) plt . close ( ) elif draw : plt . show ( ) else : return ax
Sub function to draw the plot.
Sub function to draw the plot .
"""
	Sub function to draw the plot.
	""" # ax.rcParams.update({'font.size': 15})
 # ax=ax[0,0]
 ##
 # Plot CIs if calculated
 ##
 ##
 # Plot the mean diff and LoA
 ##
 ##
 # Plot the data points
 ##
 # ax.scatter(mean[0:22], diff[0:22], alpha=0.8,  c='orange', marker='.', s=100, label='India Male')
 # ax.scatter(mean[22:44], diff[22:44], alpha=0.8,  c='blue', marker='.', s=100, label='India Female')
 # ax.scatter(mean[44:66], diff[44:66], alpha=0.8,  c='red', marker='.', s=100, label='Sierra Leone Male')
 # ax.scatter(mean[66:88], diff[66:88], alpha=0.8,  c='purple', marker='.', s=100, label='Sierra Leone Female')
 # Only draw spine between extent of the data
 # ax.spines['left'].set_bounds(min(diff), max(diff))
 # ax.spines['bottom'].set_bounds(min(mean), max(mean))
 # Hide the right and top spines
 # ax.spines['right'].set_visible(False)
 # ax.spines['top'].set_visible(False)
 # tickLocs = ax.xaxis.get_ticklocs()
 # cadenceX = tickLocs[2] - tickLocs[1]
 # tickLocs = rangeFrameLocator(tickLocs, (min(mean), max(mean)))
 # ax.xaxis.set_major_locator(ticker.FixedLocator(tickLocs))
 # tickLocs = ax.yaxis.get_ticklocs()
 # cadenceY = tickLocs[2] - tickLocs[1]
 # tickLocs = rangeFrameLocator(tickLocs, (min(diff), max(diff)))
 # ax.yaxis.set_major_locator(ticker.FixedLocator(tickLocs))
 # plt.draw() # Force drawing to populate tick labels
 # labels = rangeFrameLabler(ax.xaxis.get_ticklocs(), [item.get_text() for item in ax.get_xticklabels()], cadenceX)
 # ax.set_xticklabels(labels)
 # labels = rangeFrameLabler(ax.yaxis.get_ticklocs(), [item.get_text() for item in ax.get_yticklabels()], cadenceY)
 # ax.set_yticklabels(labels)
 # ax.patch.set_alpha(0)
 ##
 # Save or draw
 ##

mean
diff
md
sd
percentage
limitOfAgreement
confidenceIntervals
detrend
title
ax
figureSize
dpi
savePath
figureFormat
meanColour
loaColour
pointColour