python matplot 纵坐标非均匀折线图

python matplot 纵坐标非均匀折线图_第1张图片


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 13 23:10:39 2022

@author: sun
"""

import matplotlib.pyplot as plt #导入Matplotlib
import csv
import numpy as np
import math
# 导入字体属性包
from matplotlib.font_manager import FontProperties
#plt.rcParams['font.sans-serif'] = ['wqy-microhei'] #用来正常显示中文标签
#plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号
#指定中文字体所在的位置
font = FontProperties(fname=r"/home/sun/simhei.ttf", size=20)
x  =['20','50','80','100','300','500','800','1000','3000','5000','8000','10000','20000','50000','80000','100000'] #此处也可数字
header=[]
data=[]

# 以读方式打开文件
with open("result.csv", mode="r", encoding="utf-8-sig") as f:
    # 基于打开的文件,创建csv.reader实例
    reader = csv.reader(f)

    header = next(reader)

    # 逐行获取数据,并输出
    for row in reader:
        data.append(row)
steps=[50,80,100,300,500,800,1000,3000,5000,8000,10000,20000,50000,80000,100000,200000,500000,800000,1000000] 
for step in steps:
    y1=[]
    y2=[]
    y3=[]
    y4=[]
    y5=[]
    y6=[]
    for row in data:
        if int(row[0])==step:
            y1.append(float(row[2]))
            y2.append(float(row[3]))
            y3.append(float(row[4]))
            y4.append(float(row[5]))
            #y5.append(float(row[6]))
            y6.append(float(row[7]))
    plt.figure(figsize = (12, 9)) #设置图像大小,当然可以设成方形(12,8)挺合适
    plt.plot(x,y1,linewidth =1.5, label ='no',color='k', linestyle='-',marker='o',markersize=10) #功能依次为线粗、标签(图例)、颜色、线类型、点形状
    plt.plot(x,y2,linewidth =1.5, label ='our1',color='k', linestyle='--',marker='X',markersize=10) #功能依次为线粗、标签(图例)、颜色、线类型、点形状
    plt.plot(x,y3,linewidth =1.5, label ='our2',color='k', linestyle='-.',marker='D',markersize=10) #功能依次为线粗、标签(图例)、颜色、线类型、点形状
    plt.plot(x,y4,linewidth =1.5, label ='kd-tree',color='k', linestyle=':',marker='^',markersize=10) #功能依次为线粗、标签(图例)、颜色、线类型、点形状
    #plt.plot(x,y5,linewidth =1.5, label ='r-tree',color='k', linestyle='-',marker='v') #功能依次为线粗、标签(图例)、颜色、线类型、点形状
    plt.plot(x,y6,linewidth =1.5, label ='quad-tree',color='k', linestyle=(0,[6, 2]),marker='P',markersize=10) #功能依次为线粗、标签(图例)、颜色、线类型、点形状
    
    
    ax = plt.subplot(111) #这是画布哦,说明只在一张图显示,也可分割多图
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    plt.xticks(fontsize=11)#嗯调调字体
    
    plt.yscale("log")
    yticks = [0.5,1,1.5,2,2.5,3]
    plt.yticks(ticks=yticks,labels=['0.5','1','1.5','2','2.5','3'],fontsize=11)
    plt.ylim(0, 3) #显示的y轴范围
    plt.xlabel('兴趣范围(m)', FontProperties=font) # x轴名称
    plt.ylabel('耗时(s)', FontProperties=font) # y轴名称
    #plt.title('间隔'+str(step)+'m',FontProperties=font) #标题
   
    plt.legend(loc='center right', borderaxespad=0,prop=font) #显示图例
    plt.savefig('pic-{}.png'.format(step),dpi=300,bbox_inches='tight')
    #plt.show() #显示作图结果

你可能感兴趣的:(python,matplotlib,开发语言)