Reputation: 529
I am having some troubles regarding legends in python. I currently have this lines which plots what I want in Julia (using PyPlot)...
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)
The above code, troughs the following image...
As you can see, there are 100 curves for each color (blue, black and red) and I manage to add only one legend for each color.(I was struggling for a while to achieve this, since the automatic legend through 100 legend for color).
Now, I want to achieve the same result in python (import matplotlib.pyplot as plt) . The relevant code would be something like...
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'])
which has NO legend at all. Only the outer frame. Can anybody help me with this issue?. Any help will be much appreciated.
EDIT: My python and matplotlib version are:
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'
and a runnable code below.
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)
Error thrown:
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
Upvotes: 0
Views: 747
Reputation: 339230
You have an array of shape (11,100) as y values for your plot
s. This means each plot creates 100 lines. You are trying to unpack the 100 lines into a single variable a
. This does not work. Because I don't think you want to type in 100 variables separated by 99 commma, you better do not unpack the result at all.
Instead create the legend from the first element of the plot
's return.
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"])
Upvotes: 1