Created
May 10, 2021 12:21
-
-
Save Ethan00Si/86db6a87528f3caad0cee8bec3e7819b to your computer and use it in GitHub Desktop.
Revisions
-
Ethan00Si created this gist
May 10, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,68 @@ import pandas as pd import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots() data_list = [] w_list = [3,4,2,1,5] for i in w_list: x = [x for x in range(0,100,10)] y = [i*x for x in range(0, 100, 10)] data_list.append((x,y)) # plot results in a given order mapping_order = {"w0":0, "w1":1, "w2":2, "w3":3, "w4":4, "w5":5, "w6":6} marker = ['-,', '-o', '-^', '-v', '-s', '-p', '-*', '-h', '-+', '-x', '-1', '-2', '-3', '-4'] colors = ['b', 'g', 'r', 'c', 'orangered', 'rosybrown', 'black', 'crimson', 'navy', 'chocolate', 'maroon'] for i in range(len(data_list)): x,y = data_list[i] tmp = {mapping_order[x]:x for x in mapping_order} file_name = 'w'+str(w_list[i]) order = -1 ''' 注释处理根据读入数据的文件名排序画图顺序的情况 ''' for item in mapping_order: if item[1] == str(w_list[i]): order = mapping_order[item] break marker_control = marker[order] color_control = colors[order] ax.plot(x, y, marker_control,c=color_control, label=file_name, linewidth=1, ms=8) # plt.plot(range(1, 319, 10), X[i][0::10], c=colors[i], marker=marker[i], label=file_names[i], ls='--', lw=1, ms=5) ''' 调整legend中的顺序 下面是假设legend中的顺序是乱序的,需要调整为mapping order的顺序 ''' handles, labels = ax.get_legend_handles_labels() sorted_handles, sorted_labels = [0]*len(mapping_order), [0]*len(mapping_order) for i in range(len(labels)): order = -1 for item in mapping_order: if item == labels[i][:len(item)]: order = mapping_order[item] break sorted_labels[order] = labels[i] sorted_handles[order] = handles[i] ''' 下面是假设mapping order比现有的data_list长 即有一些mapping order里的东西没有用到,所以需要删除一下0 ''' res_labels = [] for item in sorted_labels: if item != 0: res_labels.append(item) res_handles = [] for item in sorted_handles: if item != 0: res_handles.append(item) ax.legend(res_handles, res_labels) # ax.legend() plt.tight_layout() plt.show()