python绘制1:1对角线(1:1 line)

本文基于python绘制1:1对角线并x、y数据之间的R方。

效果图:

在这里插入图片描述

蓝色线表示拟合线,黑色线表示1:1线。

详细代码:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pylab import mpl
from matplotlib.pyplot import MultipleLocator

mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False

fn = u'D://aaa.xlsx'
df = pd.read_excel(fn,'Sheet1',index_col=0)
a = df['a']

fn2 = u'E://ccc.csv'
df2 = pd.read_csv(fn2)
c = df2['c']

a.index = range(len(a))
b.index = range(len(a))
c.index = range(len(a))


fig, ax = plt.subplots(1, 1,figsize=(7,7),dpi=300)
## 绘制1:1对角线
ax.plot((0, 1), (0, 1), transform=ax.transAxes, ls='--',c='k', label="1:1 line")

ax.plot(c,a,'o',c='royalblue')

parameter = np.polyfit(c,a,1)
f = np.poly1d(parameter)
ax.plot(c,f(c),'b--',lw=3)

corr = np.corrcoef(c,a)[0,1]
bbox = dict(boxstyle="round", fc='1',alpha=0.5)
plt.text(0.05, 0.9, '$R^2=%.2f$' % (corr**2), 
         transform=ax.transAxes, size=25, bbox=bbox)

ax.set_xlabel('修改后',fontsize=30)
ax.set_ylabel('观测值',fontsize=30)
# 设置图片title
ax.tick_params(labelsize=27)

x_major_locator=MultipleLocator(10)
ax.xaxis.set_major_locator(x_major_locator)
y_major_locator=MultipleLocator(10)
ax.yaxis.set_major_locator(y_major_locator)
ax.set(xlim=(-22,25),ylim=(-22,25))

plt.show()

版权声明:本文为qq_32832803原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。