如何获得此图以在情节上显示图例? [英] How do I get this to show the legend on the plot?

查看:32
本文介绍了如何获得此图以在情节上显示图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取此代码以在上面显示图例,但是我尝试的所有方法均无法正常工作.这是我的代码.我过去曾尝试过 put.legend() 并且它对我有用,但我很困惑为什么这不起作用.

将 numpy 导入为 np导入matplotlib.pyplot作为plt导入matplotlib.animation作为动画#声明我的情节图1 = plt.figure()#声明xvaluesxes = np.arange(-10, 10, 0.01)xlen = len(xes)#zeros表示沿轴的yvalues是= np.zeros(xlen)#声明我的变量Efieldx = np.zeros((xlen, 1))Efieldy = np.zeros((xlen, 1))#我的两个粒子的位置p1x = 0;p1y = 1;p2x = 0;p2y = -1q = 1;Efieldx1 = q/(((xes-p1x)*(xes-p1x)+(yes-p1y)*(yes-p1y))**(1.5)*(xes-p1x)Efieldy1 = q/(((xes-p1x)*(xes-p1x)+(yes-p1y)*(yes-p1y))**(1.5)*(yes-p1y)Efieldx2 = q/(((xes-p2x)*(xes-p2x)+(yes-p2y)*(yes-p2y))**(1.5)*(xes-p2x)Efieldy2 = q/(((xes-p1x)*(xes-p1x)+(yes-p1y)*(yes-p1y))**(1.5)*(yes-p2y)Efieldx = Efieldx1 + Efieldx2Efieldy = Efieldy1 + Efieldy2#Efieldx = -1/(xs * xs + ys * ys)^(0.5)#让我们定义一个函数:def f_Efield(q,x,y,xs,ys):例如= q *((xs-x)*(xs-x)+(ys-y)*(ys-y))**(-1.5)*(xs-x)Ey = q/((xs-x)*(xs-x) + (ys-y)*(ys-y))**(1.5)*(ys-y)返回 Ex, Ey#使用我的新功能Exhere, Eyhere = f_Efield(2, 0, 0,xes, yes)#绘图:l, = plt.plot(xes, Efieldx, 'g-')l, = plt.plot(xes, Exhere, 'r--')plt.xlim(-10, 10)plt.ylim(-2,2)plt.xlabel('x')plt.title('沿 x 方向的电场 \n Andrew Richardson')#添加图例plt.legend()#显示情节plt.show()#保存情节fig1.savefig('Efield.pdf')Exhere, Eyhere = f_Efield(-1, 0, 0, xes, yes)

解决方案

您需要指定

I am trying to get this code to show a legend on it, but everything I try is not working. Here is my code. I have tried put.legend() in the past and it has worked for me and I am confused why this is not working.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

#declaring my plot
fig1 = plt.figure()

#declaring xvalues
xes = np.arange(-10, 10, 0.01)
xlen = len(xes)
#zeros for yvalues along the axis
yes = np.zeros(xlen)

#declaring my variables
Efieldx = np.zeros((xlen, 1))
Efieldy = np.zeros((xlen, 1))

#locations of my two particles
p1x = 0;
p1y = 1;
p2x = 0;
p2y = -1
q = 1;

Efieldx1  = q/((xes-p1x)*(xes-p1x) + (yes-p1y)*(yes-p1y))**(1.5)*(xes-p1x)
Efieldy1  = q/((xes-p1x)*(xes-p1x) + (yes-p1y)*(yes-p1y))**(1.5)*(yes-p1y)
Efieldx2  = q/((xes-p2x)*(xes-p2x) + (yes-p2y)*(yes-p2y))**(1.5)*(xes-p2x)
Efieldy2  = q/((xes-p1x)*(xes-p1x) + (yes-p1y)*(yes-p1y))**(1.5)*(yes-p2y)
Efieldx = Efieldx1 + Efieldx2
Efieldy = Efieldy1 + Efieldy2 
#Efieldx  = -1/(xs * xs + ys * ys)^(0.5)


#let's define a function instead:
def f_Efield(q, x, y, xs, ys):
    Ex  = q*((xs-x)*(xs-x) + (ys-y)*(ys-y))**(-1.5)*(xs-x)
    Ey  = q/((xs-x)*(xs-x) + (ys-y)*(ys-y))**(1.5)*(ys-y)
    return Ex, Ey

#using my new function
Exhere, Eyhere = f_Efield(2, 0, 0,xes, yes)

#plotting:
l, = plt.plot(xes, Efieldx, 'g-')
l, = plt.plot(xes, Exhere, 'r--')
plt.xlim(-10, 10)
plt.ylim(-2, 2)
plt.xlabel('x')
plt.title('Electric field along x-direction \n Andrew Richardson')
#adding a legend
plt.legend()
#displaying the plot
plt.show()

#saving the plot
fig1.savefig('Efield.pdf')

Exhere, Eyhere = f_Efield(-1, 0, 0, xes, yes)

解决方案

You need to either specify the label property for your plots or pass handles (optional but recommended) and labels to your call to legend otherwise matplotlib has no way of knowing what text to put in the legend

# Using label kwarg
plt.plot(xes, Efieldx, 'g-', label='Efieldx')
plt.plot(xes, Exhere, 'r--', label='Exhere')
plt.legend()

# Using explicit plot handles and labels
p1 = plt.plot(xes, Efieldx, 'g-')
p2 = plt.plot(xes, Exhere, 'r--')
plt.legend([p1, p2], ['Efieldx', 'Exhere'])

# Using just the labels (not recommended)
plt.plot(xes, Efieldx, 'g-')
plt.plot(xes, Exhere, 'r--')
plt.legend(['Efieldx', 'Exhere'])

这篇关于如何获得此图以在情节上显示图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆