lyzj13 2021-08-16 20:58 采纳率: 100%
浏览 28
已结题

显示和理想的差距。。For循环逻辑上哪里出错了吗?

import pandas as pd
# noinspection PyUnresolvedReferences
import numpy as np
import os
# noinspection PyUnresolvedReferences
import matplotlib.pyplot as plt
# noinspection PyUnresolvedReferences
from sklearn.linear_model import LinearRegression

#input the excel file address and the data to be plotted.
#refer to the excel header for groupKey, LocXKey, LocYKey, valueXKey and valueKKey.
fileAdd_x = r"C:\Users\z\PycharmProjects\Test\Excel\Material\X.Meas.csv"
fileAdd_y = r"C:\Users\z\PycharmProjects\Test\Excel\Material\Y.Meas.csv"
groupKey = 'WorkSet'

#data import
data_x = pd.read_csv(fileAdd_x).dropna( how = 'all' )
data_y = pd.read_csv(fileAdd_y).dropna( how = 'all' )
groups_x = data_x[ groupKey ].unique()
groups_y = data_y[ groupKey ].unique()


##################################################################################################
S=[13,7,15,18,19]
labels = S
x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

#########  Correlation  #########
fig_Correlation,ax = plt.subplots()
for group in groups_x:
    groupIndex = data_x[ groupKey ] == group
    groupData_x = data_x[ groupIndex ]
    x1 = groupData_x['X_Meas']
    y1 = groupData_x['X']
    #print(x1)
    # 将 x,y 分别增加一个轴,以满足 sklearn 中回归模型认可的数据
    x1 = x1[:, np.newaxis]
    y1 = y1[:, np.newaxis]


    model = LinearRegression() # 构建线性模型
    model.fit(x1, y1) # Overlayx自变量在前,因变量在后
    R2_X = model.score(x1, y1) # 拟合程度 x R2
    print('R2_x = %.3f' % R2_X) # 输出 R2

    rects5 = ax.bar(x - width/2, R2_X , width, label='X_Rsq')


for group in groups_y:
    groupIndex = data_y[ groupKey ] == group
    groupData_y = data_y[ groupIndex ]

    x2 = groupData_y['Y_Meas']
    y2 = groupData_y['Y']

    # 将 x,y 分别增加一个轴,以满足 sklearn 中回归模型认可的数据
    x2 = x2[:, np.newaxis]
    y2 = y2[:, np.newaxis]


    model = LinearRegression() # 构建线性模型
    model.fit(x2, y2) # Overlayx自变量在前,因变量在后
    R2_Y = model.score(x2, y2) # 拟合程度 y R2
    print('R2_y = %.3f' % R2_Y) # 输出 R2
    rects6 = ax.bar(x + width/2, R2_Y , width, label='Y_Rsq')

#########  Graph Correlation  #########




# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.set_ylim((0,1.3))                       #定义y轴的取值范围
ax.legend()


def autolabel(rects):

        # attach some text labels

    for rect in rects:

        height = rect.get_height()

        ax.text(rect.get_x()+rect.get_width()/2.0, 1.0*height,

                    '%.3f'%float(height), ha='center', va='bottom')

            #‘%.2f’%float(height)这个设置是让显示的数值精度为小数点后两位小数


autolabel(rects5)
autolabel(rects6)

fig_Correlation.tight_layout()
#plt.savefig('./RMSE.jpg')   #把图片保存在当前路径下,必须放在plt.show()之前,否则将是空白


plt.show()**实际
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/474965811926134.png 'image.png')
**
想法

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/144166811926148.png 'image.png')
  • 写回答

2条回答 默认 最新

  • 程序媛一枚~ Python领域新星创作者 2021-08-16 22:23
    关注

    主要在俩个问题:绘图的时候传参数值不对;刻度太大,值差别太小,所以看不出来。

    img

    
    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib import ticker
    
    S = [13, 7, 15, 18, 19]
    labels = S
    x = np.arange(len(labels))  # the label locations
    width = 0.35  # the width of the bars
    
    fig_Correlation, ax = plt.subplots()
    zsx = [0.999, 0.999, 0.998, 0.998, 0.998]
    zsy = [0.998, 0.998, 0.997, 0.998, 0.998]
    ax.bar(x - width / 2, zsx, width, label='X_Rsq')
    ax.bar(x + width / 2, zsy, width, label='Y_Rsq')
    ax.set_xticks(x)
    ax.set_xticklabels(labels)
    ax.set_ylim((0.8, 1.0))  # 定义y轴的取值范围
    ax.yaxis.set_major_locator(ticker.MultipleLocator(0.01)) # y轴刻度
    ax.yaxis.set_minor_locator(ticker.MultipleLocator(0.001))  # y最小刻度精度
    ax.legend()
    plt.show()
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月24日
  • 已采纳回答 8月16日
  • 创建了问题 8月16日

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型