python生成散点图、附加拟合要素:以生成两个散点图子图为例

# -*- coding: utf-8 -*-
"""
读取两个csv中的数据,创建子图,
YMJ 20230515 20:49
"""
import os
import numpy as np
import pandas as pd
import math
import pylab
from matplotlib import pyplot as plt
from matplotlib.ticker import MultipleLocator
import matplotlib.gridspec as gridspec
from pylab import mpl
 
mpl.rcParams['font.sans-serif'] = ["Times New Roman"]
 
file1 = "D:\\YMJ_file\\hahaha\\PTJPL.csv"
file2 = "D:\\YMJ_file\\hahaha\\PML.csv"
 
df1 = pd.read_csv(file1)
df2 = pd.read_csv(file2)
 
x1 = df1["LE_daytime1"]
x2 = df2["LE_daytime1"]
y1 = df1["E_daytime"]
y2 = df2["E_daytime"]
 
gs = gridspec.GridSpec(1, 2) # 创立1 * 2 网格
gs.update(wspace=0.3)
 
# 占位
ax1 = plt.subplot(gs[0, 0]) # gs(哪一行,绘制网格列的范围)
ax2 = plt.subplot(gs[0, 1])
 
# 开始子图的绘制
# PTJPL
ax1.plot(x1, y1, 'ko', label='PT-JPL', markersize=5)
ax1.plot((0, 1), (0, 1), linewidth=1, transform=ax1.transAxes, ls='--', c='k', label="1:1 line")
parameter = np.polyfit(x1, y1, 1)
f = np.poly1d(parameter)
ax1.plot(x1, f(x1), ls='-', c='r', lw=1)
corr = np.corrcoef(x1, y1)[0, 1]
if parameter[1] < 0:
    parameter[1] = abs(parameter[1])
    plt.text(0.05, 0.85, "$\mathdefault{R^2}$ = %.2f\ny = %.2fx - %.2f" % ((corr ** 2), parameter[0], parameter[1]),
             transform=ax1.transAxes, size=11)  # , bbox=bbox
else:
    plt.text(0.05, 0.85, "$\mathdefault{R^2}$ = %.2f\ny = %.2fx + %.2f" % ((corr ** 2), parameter[0], parameter[1]),
             transform=ax1.transAxes, size=11)
ax1.set_xlabel('Observed LE(W/$\mathdefault{m^2}$)', fontsize=12)
ax1.set_ylabel("Predicted LE(W/$\mathdefault{m^2}$)", fontsize=12)
# 设置图片title
ax1.tick_params(labelsize=10)
ax1.set_title("PT-JPL")
# 设置xy坐标范围
x_major_locator = MultipleLocator(100)
ax1.xaxis.set_major_locator(x_major_locator)
y_major_locator = MultipleLocator(100)
ax1.yaxis.set_major_locator(y_major_locator)
ax1.set(xlim=(-25, 800), ylim=(-25, 800))
 
# PML
ax2.plot(x2, y2, 'ko', label='PML', markersize=5)
ax2.plot((0, 1), (0, 1), linewidth=1, transform=ax2.transAxes, ls='--', c='k', label="1:1 line")
parameter = np.polyfit(x2, y2, 1)
f = np.poly1d(parameter)
ax2.plot(x2, f(x2), ls='-', c='r', lw=1)
corr = np.corrcoef(x2, y2)[0, 1]
if parameter[1] < 0:
    parameter[1] = abs(parameter[1])
    plt.text(0.05, 0.85, "$\mathdefault{R^2}$ = %.2f\ny = %.2fx - %.2f" % ((corr ** 2), parameter[0], parameter[1]),
             transform=ax2.transAxes, size=11)  # , bbox=bbox
else:
    plt.text(0.05, 0.85, "$\mathdefault{R^2}$ = %.2f\ny = %.2fx + %.2f" % ((corr ** 2), parameter[0], parameter[1]),
             transform=ax2.transAxes, size=11)
ax2.set_xlabel('Observed LE(W/$\mathdefault{m^2}$)', fontsize=12)
ax2.set_ylabel("Predicted LE(W/$\mathdefault{m^2}$)", fontsize=12)
# 设置图片title
ax2.tick_params(labelsize=10)
ax2.set_title("PML")
# 设置xy坐标范围
x_major_locator = MultipleLocator(100)
ax2.xaxis.set_major_locator(x_major_locator)
y_major_locator = MultipleLocator(100)
ax2.yaxis.set_major_locator(y_major_locator)
ax2.set(xlim=(-25, 800), ylim=(-25, 800))
 
outpath = os.path.join(file1.split(".csv")[0] + ".tiff")
print(outpath)
plt.savefig(outpath, dpi=400, bbox_inches='tight')  # tight设置是让图片裁剪的刚刚好

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值