Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

在Jupiter中导入excel表格,以其中的数据做一个雷达图

  1. User

    在Jupiter中导入excel表格,以其中的数据做一个雷达图

  2. Assistant
    1. 首先,需要在Jupyter Notebook中安装pandas和matplotlib库。
    !pip install pandas
    !pip install matplotlib
    
    1. 然后,导入需要使用的库和excel文件。
    import pandas as pd
    import matplotlib.pyplot as plt
    
    data = pd.read_excel('filename.xlsx') # 请将文件名改为你自己的文件名
    
    1. 接着,将数据转化为可用于绘制雷达图的格式。
    categories = list(data.columns)[1:] # 第一列是名称,不需要
    values = data.values[:,1:].tolist() # 第一列是名称,不需要
    
    N = len(categories)
    
    angles = [n / float(N) * 2 * pi for n in range(N)]
    angles += angles[:1]
    
    fig, ax = plt.subplots(figsize=(6,6), subplot_kw={'projection': 'polar'})
    
    ax.set_theta_offset(pi / 2)
    ax.set_theta_direction(-1)
    
    plt.xticks(angles[:-1], categories)
    
    ax.set_rlabel_position(0)
    
    plt.yticks([10,20,30,40,50,60,70,80,90], ["10","20","30","40","50","60","70","80","90"], color="grey", size=7)
    plt.ylim(0,90)
    
    1. 最后,绘制雷达图并显示。
    for i in range(len(values)):
        data = values[i]
        data += data[:1]
    
        ax.plot(angles, data, linewidth=1, linestyle='solid', label=data[0])
        ax.fill(angles, data, 'b', alpha=0.1)
    
    plt.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1))
    
    plt.show()
    

    完整代码如下:

    # 导入库和数据
    import pandas as pd
    import matplotlib.pyplot as plt
    from math import pi
    
    data = pd.read_excel('filename.xlsx') # 请将文件名改为你自己的文件名
    
    # 转化数据格式
    categories = list(data.columns)[1:] # 第一列是名称,不需要
    values = data.values[:,1:].tolist() # 第一列是名称,不需要
    
    N = len(categories)
    
    angles = [n / float(N) * 2 * pi for n in range(N)]
    angles += angles[:1]
    
    # 绘制雷达图
    fig, ax = plt.subplots(figsize=(6,6), subplot_kw={'projection': 'polar'})
    
    ax.set_theta_offset(pi / 2)
    ax.set_theta_direction(-1)
    
    plt.xticks(angles[:-1], categories)
    
    ax.set_rlabel_position(0)
    
    plt.yticks([10,20,30,40,50,60,70,80,90], ["10","20","30","40","50","60","70","80","90"], color="grey", size=7)
    plt.ylim(0,90)
    
    for i in range(len(values)):
        data = values[i]
        data += data[:1]
    
        ax.plot(angles, data, linewidth=1, linestyle='solid', label=data[0])
        ax.fill(angles, data, 'b', alpha=0.1)
    
    plt.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1))
    
    plt.show()
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.