Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

python - Problem with creating an approppriate legend in pyplot for errorbar plot

For my bachelor thesis I was given some python code that creates a figure out of some data using matplotlib.pyplot. The data set contains the variables (V, k, mu, t, b_avg, b_err). The figure should have k on the x axis and b_avg on the y axis and plot these values for different associated values of V. Every point with the same value of V gets a different colour. The figure is supposed to have a legend that shows which color corresponds to which value of V (either 08, 10, 12, 14, 16). But when I execute the python script like I was given, the legend lists a ton of different points. The code looks like this:

#!/usr/bin/python
from sequential import seq
from read_column import read_col
import matplotlib.pyplot as plt
import numpy as np
import array as arr
colors=['r','g','b','y','pink']
data=read_col('tau_critical_k.txt',0,1,2,3,4)
plt.figure()
for v,k,mu,tau,tau2 in zip(data[0],data[1],data[2],data[3],data[4]):
    x,y,y_e=[],[],[]
    V=int(v)
    binder=read_col('../collect/SU3/{:02d}x{:02d}x{:02d}/H_{:.5f}_mu_{:.5f}.metropolis'.format(V,V,V,k,mu),0,5,6)
    for t,b_avg,b_err in zip(binder[0],binder[1],binder[2]):
        if t == tau or t == tau2 :
            print('{:02d} {:.3f} {:.5f} {:.5f} {:.5e}({:.5e})'.format(V,k,mu,t,b_avg,b_err))
            x.append(k)
            y.append(b_avg)
            y_e.append(b_err)
            plt.errorbar(x,y,y_e,linestyle='None',marker='.',color=colors[int((V-8)/2)],label='V={:02d}'.format(V),elinewidth=1,markersize=7,capsize=3, capthick=1)
plt.axhline(1.604,linestyle='-',color='b')
plt.ylabel(r'$B_4(kappa(au_c))$')
plt.xlabel(r'$kappa$')
plt.ylim(bottom=0,top=3)
plt.legend()
plt.savefig('plot/binder_k.pdf')
plt.show()

And the legend can be seen here: enter image description here

I want a simple legend that lists every value of V once and shows the corresponding colour but I have no idea how to do it. As far as I know, I can tell python what to include in the legend by giving the "handles" of the thing I want to plot as an argument. I don't really know what the handles are that im searching for. In the code, every point is already given a lable that just says its V value.

question from:https://stackoverflow.com/questions/65889258/problem-with-creating-an-approppriate-legend-in-pyplot-for-errorbar-plot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...