使用python导入Excel作图

使用python导入Excel作图

使用Excel数据作图:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from matplotlib.patches import ConnectionPatch

# 读取数据
x_axis_data = []
y1_axis_data = []
y2_axis_data = []
y3_axis_data = []
y4_axis_data = []
sheet = pd.read_excel(r"C:\Users\Think\Desktop\123.xlsx")

#数据赋值
def get():
    for row in sheet.index.values:
        doc = dict()
        doc['key1'] = sheet.iloc[row, 0]
        x_axis_data.append(doc['key1'])
        doc['key2'] = sheet.iloc[row, 1]
        y1_axis_data.append(doc['key2'])
        # doc['key3'] = sheet.iloc[row, 2]
        # y2_axis_data.append(doc['key3'])
        # doc['key4'] = sheet.iloc[row, 3]
        # y3_axis_data.append(doc['key4'])
        # doc['key5'] = sheet.iloc[row, 4]
        # y4_axis_data.append(doc['key5'])

# 进行绘图
def huitu():
    plt.figure(figsize=(15, 5))  # 设置画布的尺寸
    plt.title('The performance of the network under different data sets', fontsize=30)  # 标题,并设定字号大小
    plt.xlabel(u'epoch', fontsize=30)  # 设置x轴,并设定字号大小
    plt.tick_params(labelsize=28)  # 刻度字体的大小
    plt.ylabel(u'PSNR', fontsize=30)  # 设置y轴,并设定字号大小
    plt.grid(True)   # 是否显示网格线
    # 设置坐标轴范围
    plt.ylim((1, 33))
    plt.xlim((0, 100))

    # 设置坐标轴刻度
    y_ticks = np.arange(1, 33, 1)
    x_ticks = np.arange(0, 100, 10)
    plt.xticks(x_ticks)
    plt.yticks(y_ticks)

    # color:颜色,linewidth:线宽,linestyle:线条类型,label:图例,marker:数据点的类型
    plt.plot(x_axis_data, y1_axis_data, color="deeppink", linewidth=1, linestyle='--', label='Set5', marker='+')
    # plt.plot(x_axis_data, y2_axis_data, color="darkblue", linewidth=1.5, linestyle=':', label='Set14', marker='*')
    # plt.plot(x_axis_data, y3_axis_data, color="red", linewidth=1, linestyle='--', label='BSDS100', marker='+')
    # plt.plot(x_axis_data, y4_axis_data, color="green", linewidth=1, linestyle=':', label='urban', marker='+')

    plt.rcParams.update({'font.size': 22})  # 图例字体的大小
    plt.legend(loc=4)  # 图例展示位置,数字代表第几象限
    return plt.show()  # 显示图像

get()
huitu()  # 只进行绘图

对Excel局部的数据进行放大

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from matplotlib.patches import ConnectionPatch

# 读取数据
x_axis_data = []
y1_axis_data = []
y2_axis_data = []
y3_axis_data = []
y4_axis_data = []
sheet = pd.read_excel(r"C:\Users\Think\Desktop\123.xlsx")

#数据赋值
def get():
    for row in sheet.index.values:
        doc = dict()
        doc['key1'] = sheet.iloc[row, 0]
        x_axis_data.append(doc['key1'])
        doc['key2'] = sheet.iloc[row, 1]
        y1_axis_data.append(doc['key2'])
        # doc['key3'] = sheet.iloc[row, 2]
        # y2_axis_data.append(doc['key3'])
        # doc['key4'] = sheet.iloc[row, 3]
        # y3_axis_data.append(doc['key4'])
        # doc['key5'] = sheet.iloc[row, 4]
        # y4_axis_data.append(doc['key5'])

# 进行绘图
def huitu_plus():
    fig, ax = plt.subplots(1, 1)   # 其中fig, ax = plt.subplots(a,b)用来控制子图个数:a为行数,b为列数
    #ax.plot(x_axis_data, y_axis_data, color='#4169E1', alpha=0.8, label='$1*10^{-5}$')
    ax.plot(x_axis_data, y1_axis_data, color="deeppink", linewidth=2, linestyle=':', label='PSNR', marker='o')
    # ax.plot(x_axis_data, reward_demaddpg10, color='#848484', alpha=0.8, label='$5*10^{-6}$')
    # ax.plot(x_axis_data, reward_demaddpg15, color='#FF774A', alpha=0.8, label='$1*10^{-6}$')
    # ax.plot(x_axis_data, reward_demaddpg20, color='#575B20', alpha=0.8, label='$5*10^{-7}$')
    # ax.plot(x_axis_data, reward_demaddpg25, color='#B84D37', alpha=0.8, label='$1*10^{-7}$')
    plt.legend(loc=4)  # 图例展示位置,数字代表第几象限
    ax.set_xlabel('Episodes')  #横坐标的标签
    ax.set_ylabel('Total reward')   # 纵坐标的标签

    axins = inset_axes(ax, width="40%", height="30%", loc='lower left',
                       bbox_to_anchor=(0.3, 0.1, 1, 1),
                       bbox_transform=ax.transAxes)
    # ax父坐标系;
    # width, height:子坐标系的宽度和高度(百分比形式或者浮点数个数)
    # loc:子坐标系的位置;
    # bbox_to_anchor:边界框,四元数组(x0, y0, width, height) ;
    # axins:子坐标系

    #  axins = ax.inset_axes((0.2, 0.2, 0.4, 0.3))  和上面表达式的效果一样

    axins.plot(x_axis_data, y1_axis_data, color='#4169E1', alpha=0.8, label='$1*10^{-5}$')

    # 设置放大区间
    zone_left = 2
    zone_right = 5

    # 坐标轴的扩展比例(根据实际数据调整)
    x_ratio = 0  # x轴显示范围的扩展比例
    y_ratio = 0.05  # y轴显示范围的扩展比例

    # X轴的显示范围
    xlim0 = x_axis_data[zone_left]-(x_axis_data[zone_right]-x_axis_data[zone_left])*x_ratio
    xlim1 = x_axis_data[zone_right]+(x_axis_data[zone_right]-x_axis_data[zone_left])*x_ratio

    # Y轴的显示范围
    y = np.hstack(y1_axis_data[zone_left:zone_right])
    ylim0 = np.min(y)-(np.max(y)-np.min(y))*y_ratio
    ylim1 = np.max(y)+(np.max(y)-np.min(y))*y_ratio

    # 调整子坐标系的显示范围
    axins.set_xlim(xlim0, xlim1)
    axins.set_ylim(ylim0, ylim1)


    # 原图中画方框
    tx0 = xlim0
    tx1 = xlim1
    ty0 = ylim0
    ty1 = ylim1
    sx = [tx0,tx1,tx1,tx0,tx0]
    sy = [ty0,ty0,ty1,ty1,ty0]
    ax.plot(sx,sy,"blue")       #(tx0,ty0)为一个坐标点、类推

    # 画两条线
    xy = (xlim0,ylim0)
    xy2 = (xlim0,ylim1)
    con = ConnectionPatch(xyA=xy2,xyB=xy,coordsA="data",coordsB="data",
            axesA=axins,axesB=ax)
    axins.add_artist(con)

    xy = (xlim1,ylim0)
    xy2 = (xlim1,ylim1)
    con = ConnectionPatch(xyA=xy2,xyB=xy,coordsA="data",coordsB="data",
            axesA=axins,axesB=ax)
    axins.add_artist(con)

    return plt.show()

get()
huitu_plus()  # 只进行绘图

你可能感兴趣的:(excel,python)