简体   繁体   中英

Multiple legends and multiple colors/shapes matplotlib

I want to plot data from about 20+ files at same time. I am trying to plot each set of data from each file in different color and each with different legend. I have seen some examples and also the matplotlib tutorial but I am little lost here. How to put legends and give different shapes for every set.

eg: The inputs are set of data from several files with separate thresholds. filenames: file1_th0, file1_th0.1 and so on. So i want to make all similar threshold data of different files of same shape/color. Also give proper legends. I can plot very well which ever data set I need but I am not able to put separate shapes for different threshold value. Any suggestion in this regards will be great.

Code:

import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
from pylab import*
import math
from matplotlib.ticker import LogLocator


for fname in ('file1_th0', 'file1_th0.1','file1_th0.01', 'file1_th0.001', 'file1_th0.001'):
    data=np.loadtxt(fname)
    X=data[:,2]
    sorted_data = np.sort(X)    
    cdf=np.arange(len(sorted_data))/float(len(sorted_data))
    ccdf = 1 - cdf  
    plt.plot(sorted_data,ccdf,'r-', label = 'label1')

for fname in ('file2_th0', 'file2_th0.1', 'file2_th0.01', 'file2_th0.001','file2_th0.0001'):
    data=np.loadtxt(fname)
    X=data[:,2]
    sorted_data = np.sort(X)    
    cdf=np.arange(len(sorted_data))/float(len(sorted_data))
    ccdf = 1 - cdf  
    plt.plot(sorted_data,cdf,'b-')

for fname in ('file3_th0','file3_th0.1','file3_th0.01','file3_th0.001', 'file3_th0.0001'):
    data=np.loadtxt(fname)
    X=data[:,4]
    sorted_data = np.sort(X)    
    cdf=np.arange(len(sorted_data))/float(len(sorted_data))
    ccdf = 1 - cdf  
    plt.plot(sorted_data,cdf,'m-')

for fname in ('file4_th0', 'file4_th0.1', 'file4_th0.01', 'file4_th0.001','file4_th0.0001'):
    data=np.loadtxt(fname)
    X=data[:,4]
    sorted_data = np.sort(X)    
    cdf=np.arange(len(sorted_data))/float(len(sorted_data))
    ccdf = 1 - cdf  
    plt.plot(sorted_data,cdf,'c--')

plt.xlabel('this is x!')
plt.ylabel('this is y!')
plt.gca().set_xscale("log")
#plt.gca().set_yscale("log")
plt.show()

First of all, you need to add labels and markers to your plot calls and add a legend call, eg:

b=np.arange(0,20,1)
c=b*0.5
d=b*2

plt.plot(b,d,color='r',marker='o',label='set 1')
plt.plot(b,c,color='g',marker='*',label='set 2')

plt.legend(loc='upper left')

在此处输入图片说明

However in your looped example you will end up with lots of identical legend entries, which I presume you don't want.

To get round it, you could:

n=0
for whatever in whatever: # e.g. your for loops
    # do stuff with whatever
    if n==0:
        plt.plot(sorted_data,cdf,color='r',marker='o',label='set 1')
    else:
        plt.plot(sorted_data,cdf,color='r',marker='o')
    n += 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM