Customize Python legend
关于Python中的传说,我遇到了一些麻烦。我现在有这几行画出了我在茱莉亚想要的东西(使用Pyplot)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | figure("Something in Julia",figsize=(10,10)) suptitle(L"Something in julia") # Add text in figure coordinates figtext(0.5, 0.04, L"Something in julia", ha="center", va="center") len=collect(-20:4:20); **legend() a,=plot(len,QF_r,color="black",linewidth=1.0)#,label="radon"); b,=plot(len,QF_s,color="blue",linewidth=1.0)#,label="sapick"); c,=plot(len,QF_e,color="red",linewidth=1.0)#,label="emd"); legend([a,b,c], ["legen1","legend2","legend3"],loc="lower right", ncol=1, fontsize=10, markerscale=10, markerfirst=true, frameon=false, fancybox=false, shadow=false)** ax = gca(); xmin, xmax = xlim() # return the current xlim ymin, ymax = ylim() # return the current xlim #ylim(ymin=1.0) # adjust the min leaving max unchanged #ylim(ymax=2.0) # adjust the min leaving max unchanged ylim(ymin=0.0) # adjust the min leaving max unchanged ylim(ymax=0.5) # adjust the min leaving max unchanged xlim(xmin=-20) # adjust the min leaving max unchanged xlim(xmax=20) # adjust the min leaving max unchanged xlabel(L"xlabael here $[some units]$", fontsize=14); ylabel(L"y label here", fontsize=14); tick_params(axis="both", width=1, length=1.5, direction="in", color="black", pad=1.5, labelsize=10, labelcolor="black", colors="black", zorder=10, bottom="on",top="off",left="on",right="off", labelbottom="on",labeltop="off",labelleft="on",labelright="off") title(L"$\bf{S1}$ $A$ $lot$ $of$ $curves$ $-$ $In$ $julia$", fontsize=12) |
上面的代码,如下图所示…。
如你所见,每种颜色有100条曲线(蓝色、黑色和红色),我设法只为每种颜色添加一个图例(我努力了一段时间,因为自动图例通过100个颜色图例)。
现在,我想在python中获得相同的结果(将matplotlib.pyplot导入为plt)。相关的代码应该是……
1 2 3 4 5 | plt.legend() a,=plt.plot(len,QF_r,color="black",linewidth=1.0)#,label="radon"); b,=plt.plot(len,QF_s,color="blue",linewidth=1.0)#,label="sapick"); c,=plt.plot(len,QF_e,color="red",linewidth=1.0)#,label="emd"); plt.legend([a, b, c], ['label1', 'label2', 'label3']) |
号
这给了我氧化镁
一点也没有传说。只有外框。有人能帮我解决这个问题吗?任何帮助都将不胜感激。
编辑:我的python和matplotlib版本是:
In [564]: import sys
In [565]: print(sys.version)
2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609]In [566]: import matplotlib
In [567]: matplotlib.version Out[567]: '1.5.1'
号
以及下面的可运行代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import numpy as np import matplotlib.pyplot as plt from matplotlib import rc, font_manager cos = np.cos pi = np.pi ##### PARAMETROS un_lim = 1.0e-8 nbins = 25 frec_ = 100.0; min_db_ = -20; max_db_ = 20; stp_db_ = 4; nexp_ = 100; nch_ = 8; xx_r = np.linspace(0,1, num = nbins); xx_a = np.linspace(-90,90,num = nbins); xx_i = np.linspace(-90,90,num = nbins); db_increment_ = np.arange(min_db_,max_db_+1,stp_db_); nu_increment_ = len(db_increment_); ArrayA = np.random.rand(11,100) ArrayB = np.random.rand(11,100) ArrayC = np.random.rand(11,100) len=db_increment_ print(np.shape(ArrayA)) print(np.shape(db_increment_)) pre_tit ="Something_in_python_Something_in_pyhton" plt.figure("Something in python",figsize=(10,10)) plt.suptitle(r"Something in python") # Add text in figure coordinates plt.figtext(0.5, 0.04, r"Something in python") plt.legend() a,=plt.plot(len,ArrayA,color="black",linewidth=1.0) b,=plt.plot(len,ArrayB,color="blue",linewidth=1.0) c,=plt.plot(len,ArrayC,color="red",linewidth=1.0) plt.legend([a,b,c], ["legen1","legend2","legend3"],loc="lower right", ncol=1, fontsize=10, markerscale=10, markerfirst=True, frameon=False, fancybox=False, shadow=False) xmin, xmax = plt.xlim() # return the current xlim ymin, ymax = plt.ylim() # return the current xlim plt.xlabel(r"xlabael here $[some units]$", fontsize=14); plt.ylabel(r"y label here", fontsize=14); plt.title(r"It doesnt work in python", fontsize=12) |
引发的错误:
1
2 44
45 plt.legend()---> 46 a,=plt.plot(len,ArrayA,color="black",linewidth=1.0)
47 b,=plt.plot(len,ArrayB,color="blue",linewidth=1.0)
48 c,=plt.plot(len,ArrayC,color="red",linewidth=1.0)ValueError: too many values to unpack
号
您有一个形状数组(11100)作为您的
相反,从
1 2 3 4 5 | a = plt.plot(db_increment_, ArrayA, color="black", linewidth=1.0) b = plt.plot(db_increment_, ArrayB, color="blue", linewidth=1.0) c = plt.plot(db_increment_, ArrayC, color="red", linewidth=1.0) plt.legend([a[0], b[0], c[0]], ["legend1","legend2","legend3"]) |