简体   繁体   English

从 plot 图例中删除点

[英]Remove points from a plot legend

I have this code that shows the Lagrange interpolation between set of points(x,y cordination).我有这段代码显示了点集(x,y 坐标)之间的拉格朗日插值。 Using matplotlib:使用 matplotlib:

import numpy as np
from scipy.interpolate import lagrange
import matplotlib.pyplot as plt

x1 = [0.2, 0.4, 0.6, 0.8]
y1 = [1, 2, 4, 6]
x2 = [0.2, 0.4, 0.6, 0.8]
y2 = [3, 10, 19, 43]
x_new = np.arange(0.2, 0.8, 0.1)
x_new2 = np.arange(0.2, 0.8, 0.1)
f = lagrange(x1, y1)
f2 = lagrange(x2, y2)
fig = plt.figure(figsize=(8, 6))
plt.plot(x_new, f(x_new), 'y', x1, y1, 'ro', label='$r = 20')
plt.plot(x_new2, f2(x_new2), 'b', x2, y2, 'ro', label='$r = 40')
plt.legend(loc='best')
plt.title('Lagrange Polynomial')
plt.grid()
plt.xlabel('r')
plt.ylabel('cut size')
plt.show()

My output:我的 output:

在此处输入图像描述

I want to remove the red points in the legend.我想删除图例中的红点。 How can I do that?我怎样才能做到这一点?

Break markers into new lines将标记分成新行

import numpy as np from scipy.interpolate import lagrange import matplotlib.pyplot as plt导入 numpy 作为 np 从 scipy.interpolate 导入拉格朗日导入 matplotlib.pyplot 作为 plt

x1 = [0.2, 0.4, 0.6, 0.8]
y1 = [1, 2, 4, 6]
x2 = [0.2, 0.4, 0.6, 0.8]
y2 = [3, 10, 19, 43]
x_new = np.arange(0.2, 0.8, 0.1)
x_new2 = np.arange(0.2, 0.8, 0.1)
f = lagrange(x1, y1)
f2 = lagrange(x2, y2)
fig = plt.figure(figsize=(8, 6))
plt.plot(x_new, f(x_new), 'y',  label='$r = 20')
plt.plot(x1, y1, 'ro')
plt.plot(x_new2, f2(x_new2), 'b', label='$r = 40')
plt.plot( x2, y2, 'ro')
plt.legend(loc='best')
plt.title('Lagrange Polynomial')
plt.grid()
plt.xlabel('r')
plt.ylabel('cut size')
plt.show()

which now gives现在给出

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM