# python画折线图代码-用Python画论文折线图、曲线图？几个代码模板轻松搞定！

python+Matplotlib

NO.1

# -*- coding: utf-8 -*-

import numpy as np

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif']=['Arial']#如果要显示中文字体，则在此处设为：SimHei

plt.rcParams['axes.unicode_minus']=False#显示负号

x = np.array([3,5,7,9,11,13,15,17,19,21])

A = np.array([0.9708, 0.6429, 1, 0.8333, 0.8841, 0.5867, 0.9352, 0.8000, 0.9359, 0.9405])

B= np.array([0.9708, 0.6558, 1, 0.8095, 0.8913, 0.5950, 0.9352, 0.8000, 0.9359, 0.9419])

C=np.array([0.9657, 0.6688, 0.9855, 0.7881, 0.8667, 0.5952, 0.9361, 0.7848, 0.9244, 0.9221])

D=np.array([0.9664, 0.6701, 0.9884, 0.7929, 0.8790, 0.6072, 0.9352, 0.7920, 0.9170, 0.9254])

#label在图示(legend)中显示。若为数学公式，则最好在字符串前后添加"$"符号 #color：b:blue、g:green、r:red、c:cyan、m:magenta、y:yellow、k:black、w:white、、、 #线型：- -- -. : , #marker：. , o v < * + 1 plt.figure(figsize=(10,5)) plt.grid(linestyle = "--") #设置背景网格线为虚线 ax = plt.gca() ax.spines['top'].set_visible(False) #去掉上边框 ax.spines['right'].set_visible(False) #去掉右边框 plt.plot(x,A,color="black",label="A algorithm",linewidth=1.5) plt.plot(x,B,"k--",label="B algorithm",linewidth=1.5) plt.plot(x,C,color="red",label="C algorithm",linewidth=1.5) plt.plot(x,D,"r--",label="D algorithm",linewidth=1.5) group_labels=['dataset1','dataset2','dataset3','dataset4','dataset5',' dataset6','dataset7','dataset8','dataset9','dataset10'] #x轴刻度的标识 plt.xticks(x,group_labels,fontsize=12,fontweight='bold') #默认字体大小为10 plt.yticks(fontsize=12,fontweight='bold') plt.title("example",fontsize=12,fontweight='bold') #默认字体大小为12 plt.xlabel("Data sets",fontsize=13,fontweight='bold') plt.ylabel("Accuracy",fontsize=13,fontweight='bold') plt.xlim(3,21) #设置x轴的范围 #plt.ylim(0.5,1) #plt.legend() #显示各曲线的图例 plt.legend(loc=0, numpoints=1) leg = plt.gca().get_legend() ltext = leg.get_texts() plt.setp(ltext, fontsize=12,fontweight='bold') #设置图例字体的大小和粗细 plt.savefig('D:\filename.png') #建议保存为svg格式，再用inkscape转为矢量图emf后插入word中 plt.show() 效果图： NO.2 # coding=utf-8 import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['Arial'] # 如果要显示中文字体,则在此处设为：SimHei plt.rcParams['axes.unicode_minus'] = False # 显示负号 x = np.array([1, 2, 3, 4, 5, 6]) VGG_supervised = np.array([2.9749694, 3.9357018, 4.7440844, 6.482254, 8.720203, 13.687582]) VGG_unsupervised = np.array([2.1044724, 2.9757383, 3.7754183, 5.686206, 8.367847, 14.144531]) ourNetwork = np.array([2.0205495, 2.6509762, 3.1876223, 4.380781, 6.004548, 9.9298]) # label在图示(legend)中显示。若为数学公式,则最好在字符串前后添加"$"符号

# color：b:blue、g:green、r:red、c:cyan、m:magenta、y:yellow、k:black、w:white、、、

# 线型：- -- -. : ,

# marker：. , o v < * + 1

plt.figure(figsize=(10, 5))

plt.grid(linestyle="--") # 设置背景网格线为虚线

ax = plt.gca()

ax.spines['top'].set_visible(False) # 去掉上边框

ax.spines['right'].set_visible(False) # 去掉右边框

plt.plot(x, VGG_supervised, marker='o', color="blue", label="VGG-style Supervised Network", linewidth=1.5)

plt.plot(x, VGG_unsupervised, marker='o', color="green", label="VGG-style Unsupervised Network", linewidth=1.5)

plt.plot(x, ourNetwork, marker='o', color="red", label="ShuffleNet-style Network", linewidth=1.5)

group_labels = ['Top 0-5%', 'Top 5-10%', 'Top 10-20%', 'Top 20-50%', 'Top 50-70%', ' Top 70-100%'] # x轴刻度的标识

plt.xticks(x, group_labels, fontsize=12, fontweight='bold') # 默认字体大小为10

plt.yticks(fontsize=12, fontweight='bold')

# plt.title("example", fontsize=12, fontweight='bold') # 默认字体大小为12

plt.xlabel("Performance Percentile", fontsize=13, fontweight='bold')

plt.ylabel("4pt-Homography RMSE", fontsize=13, fontweight='bold')

plt.xlim(0.9, 6.1) # 设置x轴的范围

plt.ylim(1.5, 16)

# plt.legend() #显示各曲线的图例

plt.legend(loc=0, numpoints=1)

leg = plt.gca().get_legend()

ltext = leg.get_texts()

plt.setp(ltext, fontsize=12, fontweight='bold') # 设置图例字体的大小和粗细

plt.savefig('./filename.svg', format='svg') # 建议保存为svg格式,再用inkscape转为矢量图emf后插入word中

plt.show()

NO.3

# coding=utf-8

import matplotlib.pyplot as plt

from matplotlib.pyplot import figure

import numpy as np

figure(num=None, figsize=(2.8, 1.7), dpi=300)

#figsize的2.8和1.7指的是英寸，dpi指定图片分辨率。那么图片就是（2.8*300）*（1.7*300）像素大小

test_mean_1000S_n = [0.7,0.5,0.3,0.8,0.7,0.5,0.3,0.8,0.7,0.5,0.3,0.8,0.7,0.5,0.3,0.8,0.7,0.5,0.3,0.8]

test_mean_1000S = [0.9,0.8,0.7,0.6,0.9,0.8,0.7,0.6,0.9,0.8,0.7,0.6,0.9,0.8,0.7,0.6,0.9,0.8,0.7,0.6]

plt.plot(test_mean_1000S_n, 'royalblue', label='without threshold')

plt.plot(test_mean_1000S, 'darkorange', label='with threshold')

#画图，并指定颜色

plt.xticks(fontproperties = 'Times New Roman', fontsize=8)

plt.yticks(np.arange(0, 1.1, 0.2), fontproperties = 'Times New Roman', fontsize=8)

#指定横纵坐标的字体以及字体大小，记住是fontsize不是size。yticks上我还用numpy指定了坐标轴的变化范围。

plt.legend(loc='lower right', prop={'family':'Times New Roman', 'size':8})

#图上的legend，记住字体是要用prop以字典形式设置的，而且字的大小是size不是fontsize，这个容易和xticks的命令弄混

plt.title('1000 samples', fontdict={'family' : 'Times New Roman', 'size':8})

#指定图上标题的字体及大小

plt.xlabel('iterations', fontdict={'family' : 'Times New Roman', 'size':8})

plt.ylabel('accuracy', fontdict={'family' : 'Times New Roman', 'size':8})

#指定横纵坐标描述的字体及大小

plt.savefig('./where-you-want-to-save.png', dpi=300, bbox_inches="tight")

#保存文件，dpi指定保存文件的分辨率

#bbox_inches="tight" 可以保存图上所有的信息，不会出现横纵坐标轴的描述存掉了的情况

plt.show()

#记住，如果你要show()的话，一定要先savefig，再show。如果你先show了，存出来的就是一张白纸。

Marker常见参数

10-28 745
10-28 1537
09-20
11-01 1424
09-20
09-16
04-09 1147
©️2020 CSDN 皮肤主题: 1024 设计师:白松林