【问题标题】:Customize Python legend自定义 Python 图例
【发布时间】:2019-04-13 07:37:24
【问题描述】:

我在 python 中的图例方面遇到了一些麻烦。 我目前有这条线可以在 Julia 中绘制我想要的内容(使用 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)

上面的代码,通过下图......

如您所见,每种颜色(蓝色、黑色和红色)有 100 条曲线,我设法为每种颜色仅添加一个图例。(我为实现这一点苦苦挣扎了一段时间,因为自动图例通过100 个颜色图例)。

现在,我想在 python (import matplotlib.pyplot as plt) 中实现相同的结果。相关代码将类似于...

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 版本是:

在 [564] 中:导入系统

在 [565] 中:打印(sys.version) 2.7.12(默认,2017 年 12 月 4 日,14:50:18)[GCC 5.4.0 20160609]

在 [566] 中:导入 matplotlib

在[567]:matplotlib。版本在[567]:'1.5.1'

以及下面的可运行代码。

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)

抛出错误:

 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:解包的值太多

【问题讨论】:

  • 这不应该发生。请提供minimal reproducible example(可运行代码)并提及使用的matplotlib和python版本。
  • “a,b,c”后面的“,”是什么?
  • @DejanMarić 请参阅here 了解逗号的含义。
  • @ImportanceOfBeingErnest 首先,感谢您的快速回复。我已经编辑了原始问题并添加了一些可运行的代码...

标签: python matplotlib julia


【解决方案1】:

您有一个形状数组 (11,100) 作为 plots 的 y 值。这意味着每个绘图创建 100 条线。您正在尝试将 100 行解压缩为单个变量 a。这不起作用。因为我不认为你想输入 100 个用 99 逗号分隔的变量,所以最好不要解压结果。

而是从plot 返回的第一个元素创建图例。

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"])

【讨论】:

  • 确实,你成功了!非常感谢。这解决了我的问题!
猜你喜欢
  • 1970-01-01
  • 2014-11-02
  • 2015-04-28
  • 2014-10-08
  • 2014-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多