图例被情节覆盖 - matplotlib
Legend overwritten by plot - matplotlib
我的情节如下所示:
我想将线图和标记都标为红色。但是图例没有出现,因为它的情节正在删除它的 space.
更新
原来我不能在plt.legend()
中放入多个字符串
我使用以下方法使图形变大了:
fig = plt.gcf()
fig.set_size_inches(18.5, 10.5)
但是现在我在图例中只有一个标签,标记出现在线图中,而我更想要两个:一个单独用于标记,另一个单独用于线条:
更新代码:
plt.plot(range(len(y)), y, '-bD', c='blue', markerfacecolor='red', markeredgecolor='k', markevery=rare_cases, label='%s' % target_var_name)
fig = plt.gcf()
fig.set_size_inches(18.5, 10.5)
# changed this over here
plt.legend()
plt.savefig(output_folder + fig_name)
plt.close()
您想要做的事情(为单个对象设置两个标签)并非完全不可能,但单独绘制直线和稀有值要容易得多,例如
# boilerplate
import numpy as np
import matplotlib.pyplot as plt
# synthesize some data
N = 501
t = np.linspace(0, 10, N)
s = np.sin(np.pi*t)
rare = np.zeros(N, dtype=bool); rare[:20]=True; np.random.shuffle(rare)
plt.plot(t, s, label='Curve')
plt.scatter(t[rare], s[rare], label='rare')
plt.legend()
plt.show()
更新
[...] it turns out I cannot put several strings in plt.legend()
嗯,你可以,只要 ① 几个字符串在一个可迭代对象(元组或列表)中并且 ② 字符串(即标签)的数量等于艺术家(即 thingies)的数量情节。
plt.legend(('a', 'b', 'c'))
我的情节如下所示:
我想将线图和标记都标为红色。但是图例没有出现,因为它的情节正在删除它的 space.
更新
原来我不能在plt.legend()
我使用以下方法使图形变大了:
fig = plt.gcf()
fig.set_size_inches(18.5, 10.5)
但是现在我在图例中只有一个标签,标记出现在线图中,而我更想要两个:一个单独用于标记,另一个单独用于线条:
更新代码:
plt.plot(range(len(y)), y, '-bD', c='blue', markerfacecolor='red', markeredgecolor='k', markevery=rare_cases, label='%s' % target_var_name)
fig = plt.gcf()
fig.set_size_inches(18.5, 10.5)
# changed this over here
plt.legend()
plt.savefig(output_folder + fig_name)
plt.close()
您想要做的事情(为单个对象设置两个标签)并非完全不可能,但单独绘制直线和稀有值要容易得多,例如
# boilerplate
import numpy as np
import matplotlib.pyplot as plt
# synthesize some data
N = 501
t = np.linspace(0, 10, N)
s = np.sin(np.pi*t)
rare = np.zeros(N, dtype=bool); rare[:20]=True; np.random.shuffle(rare)
plt.plot(t, s, label='Curve')
plt.scatter(t[rare], s[rare], label='rare')
plt.legend()
plt.show()
更新
[...] it turns out I cannot put several strings in
plt.legend()
嗯,你可以,只要 ① 几个字符串在一个可迭代对象(元组或列表)中并且 ② 字符串(即标签)的数量等于艺术家(即 thingies)的数量情节。
plt.legend(('a', 'b', 'c'))