简体   繁体   English

多图只成一个

[英]Multiple Plots into only one

I'm new into python and cannot solve my problem after googling for hours:我是 python 的新手,在谷歌搜索数小时后无法解决我的问题:

I adjusted an existing Python Script for Kano analysis, but want to plot all “outcome-dots” into only one combined plot instead of generating one for each Feature.我调整了现有的 Python 脚本以进行卡诺分析,但希望将所有“结果点”plot 合并为一个组合 plot,而不是为每个功能生成一个。 (My goal is to plot all Features into one graphic with listing feature names like legends) (我的目标是将 plot 的所有功能整合到一个图形中,并列出图例等功能名称)

Here is the data to run the code: https://wetransfer.com/downloads/f1815b3660dca105b3364085d36a99e420220901195108/512a6d这是运行代码的数据: https://wetransfer.com/downloads/f1815b3660dca105b3364085d36a99e420220901195108/512a6d

Thanks a lot for your help!!非常感谢你的帮助!!

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
#reading the Data-Export from Unipark
import pandas as pd
data = pd.read_excel (r'C:\Users\xxx\Nextcloud\03_Documents\xxx\data_kano.xlsx',header=0)

data.drop(index=0)
segment=list(range(0,len(data.columns)+1,2))

num=len(data.columns)/2
store=[]
for x in range(1,len(segment)):
    df=data.iloc[:,segment[x-1]:segment[x]].copy()
    store.append(df)

#creating the scoring to rate the answeres to categories
eval_matrix=pd.DataFrame({'#1':['Q','R','R','R','R'],'#2':['A','I','I','I','R'],
             '#3':['A','I','I','I','R'],'#4':['A','I','I','I','R'],
             '#5':['O','M','M','M','Q']})
eval_matrix.index=['#1','#2','#3','#4','#5']


#scoring to the different categories 
result=pd.DataFrame()
for x in range(0,len(store)):
    Kano_score=[]
    for y in range(0,len(list(store[x].iloc[:,0]))):
        Kano_score.append(eval_matrix.loc[store[x].iloc[y,0],store[x].iloc[y,1]])
    pos='Feature '+str(x+1)+"-1"  
    neg='Feature '+str(x+1)+"-2" 
    col_name3='Feature '+str(x+1)+"-Result" 
    result[pos]=store[x].iloc[:,0].copy()
    result[neg]=store[x].iloc[:,1].copy()
    result[col_name3]=Kano_score
    

import matplotlib.pyplot as plt
import numpy as np

# Create a function to calculate the satisfaction and dissatisfaction coefficients
def SI(A: int,O: int,M: int,I: int)->float:
    return float((A+O) / (A+O+M+I))

def DSI(A: int,O: int,M: int,I: int)->float:
    return float((O+M) / (A+O+M+I) * -1)


def showGraph(dsi: float, si:float,title:str,x: int, y: int, n:int)->str:
    ax=plt.axes()
    ax.scatter(si,dsi)

  
    ax.set(xlim=[0,1],ylim=[-1,0],xlabel='Functional (Satisfaction Coefficients CS+)',ylabel='Disfunctional (Dissatisfaction Coefficients CS-)',xticks=np.arange(0,1,0.1),yticks=np.arange(-1,0,0.1)) # set the ranges of x-axis y-axis
    ax.set_title(title,size=16)
    ax.grid(True)
    
    # Adjust the thickness of the two lines in the middle as a cross
    gridlines = ax.yaxis.get_gridlines()
    gridlines[5].set_color('k')
    gridlines[5].set_linewidth(2.5)
    gridlines = ax.xaxis.get_gridlines()
    gridlines[5].set_color('k')
    gridlines[5].set_linewidth(2.5)
    
    #colouring
    
    
    plt.fill([0.0,0.0,0.3,0.3],[-0.0,-0.5,-0.5,-0.0],alpha=0.25, color ="b")
    plt.fill([0.3,0.3,0.5,0.5],[-0.0,-0.5,-0.5,-0.0],alpha=0.25, color ="b")
    
    plt.fill([0.7,0.7,1.0,1.0],[-0.0,-0.5,-0.5,-0.0],alpha=0.25, color ="#036630")
    plt.fill([0.5,0.5,0.7,0.7],[-0.0,-0.5,-0.5,-0.0],alpha=0.25, color ="#036630")
    
    plt.fill([0.5,0.5,0.7,0.7],[-0.5,-1.0,-1.0,-0.5],alpha=0.25, color ="y")
    plt.fill([0.7,0.7,1.0,1.0],[-0.5,-1.0,-1.0,-0.5],alpha=0.25, color ="y")
    
    plt.fill([0.0,0.0,0.3,0.3],[-0.5,-1.0,-1.0,-0.5],alpha=0.25, color ="r")
    plt.fill([0.3,0.3,0.5,0.5],[-0.5,-1.0,-1.0,-0.5],alpha=0.25, color ="r")
    plt.savefig('./figures/'+title+'.jpg')
    
    #add a legend
    plt.plot(x, y, alpha=0.8, color ="b", label="Indifferent")
    plt.plot(x, y, alpha=0.8, color ="r", label="Must Be")
    plt.plot(x, y, alpha=0.8, color ="k", label="Neutral")
    plt.plot(x, y, alpha=0.8, color ="#036630", label="Attractive")
    plt.plot(x, y, alpha=0.8, color ="y", label="One-Dimensional")
    plt.legend(bbox_to_anchor =(0.225,-0.45,0.55, 0.5), loc='lower center', ncol=2, fontsize ="small", framealpha=1, shadow=True, borderpad=1)
    
    plt.show()
    

import collections
import os
os.makedirs('figures',exist_ok=True)
pos=list(range(2,len(result.columns),3))

count=collections.Counter(result.iloc[:,2])
df=pd.DataFrame.from_dict(count,orient='index')
df.columns=['Score']

si=SI(df.loc['A','Score'],df.loc['O','Score'],df.loc['M','Score'],df.loc['I','Score'])
dsi=DSI(df.loc['A','Score'],df.loc['O','Score'],df.loc['M','Score'],df.loc['I','Score'])
df['SI']=np.nan
df.iloc[0,1]=si
df['DSI']=np.nan
df.iloc[0,2]=dsi
df.to_excel('Kano_scoring_Ergebnisse.xlsx',sheet_name='Feature 1')
showGraph(dsi,si,'Feature 1',len(pos)/2,2,1)

with pd.ExcelWriter('Kano_scoring_Ergebnisse.xlsx', engine="openpyxl",mode='a') as writer:
    for x in range(1,len(pos)):
        count=collections.Counter(result.iloc[:,pos[x]])
        df=pd.DataFrame.from_dict(count,orient='index')
        df.columns=['Score']
        si=SI(df.loc['A','Score'],df.loc['O','Score'],df.loc['M','Score'],df.loc['I','Score'])
        dsi=DSI(df.loc['A','Score'],df.loc['O','Score'],df.loc['M','Score'],df.loc['I','Score'])
        df['SI']=np.nan
        df.iloc[0,1]=si
        df['DSI']=np.nan
        df.iloc[0,2]=dsi
        df.to_excel(writer,sheet_name='Feature '+str(x+1))
        showGraph(dsi,si,'Feature '+str(x+1),len(pos)/2,2,x+1)

In your code, I have made some small adjustments to read the dsi and si data into a dataframe called scatter_data .在您的代码中,我做了一些小的调整,将dsisi数据读入名为scatter_data的 dataframe 中。 Each time you use ShowGraph() , the date gets read into the dataframe instead.每次使用ShowGraph()时,日期都会被读取到 dataframe 中。 At the end, the same dataframe is passed to ShowGraph() and that will create just one graph.最后,同样的 dataframe 被传递给ShowGraph() ,这将只创建一个图。 Hope this is what you are looking for.希望这是您正在寻找的。

Note that I have only shown the last part of the code... rest is as you have it.请注意,我只显示了代码的最后一部分...... rest 就是你所拥有的。 Also, kept your code in comments, so you can see what is being changed.此外,将您的代码保留在注释中,以便您可以看到正在更改的内容。 Lastly, I have tried to make the minimal changes to the code, so you may be able to do a more efficient code as you know it better.最后,我尝试对代码进行最小的更改,以便您可以编写更高效的代码,因为您更了解它。

## From row 106 onwards...

#showGraph(dsi,si,'Feature 1',len(pos)/2,2,1) ##Don't plot
scatter_data = pd.DataFrame(columns = ['dsi', 'si']) ## Create empty dataframe
scatter_data = scatter_data.append({'dsi':dsi, 'si':si}, ignore_index=True) ##Append feature 1 to dataframe

with pd.ExcelWriter('Kano_scoring_Ergebnisse.xlsx', engine="openpyxl",mode='a') as writer:
    for x in range(1,len(pos)):
        count=collections.Counter(result.iloc[:,pos[x]])
        df=pd.DataFrame.from_dict(count,orient='index')
        df.columns=['Score']
        si=SI(df.loc['A','Score'],df.loc['O','Score'],df.loc['M','Score'],df.loc['I','Score'])
        dsi=DSI(df.loc['A','Score'],df.loc['O','Score'],df.loc['M','Score'],df.loc['I','Score'])
        df['SI']=np.nan
        df.iloc[0,1]=si
        df['DSI']=np.nan
        df.iloc[0,2]=dsi
        df.to_excel(writer,sheet_name='Feature '+str(x+1))
#        showGraph(dsi,si,'Feature '+str(x+1),len(pos)/2,2,x+1) ## Dont plot, read into df instead
        scatter_data = scatter_data.append({'dsi':dsi, 'si':si}, ignore_index=True) 

##Finally plot once (note this is outside the with loop
showGraph(scatter_data.dsi,scatter_data.si,'All Feature',len(pos)/2,2,1)

Plot Plot

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM