【问题标题】:Remove points from a plot legend从图例中删除点
【发布时间】:2021-05-05 10:46:36
【问题描述】:

我有这段代码显示点集(x,y 坐标)之间的拉格朗日插值。使用 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()

我的输出:

我想删除图例中的红点。我该怎么做?

【问题讨论】:

  • 最好的方法是调用plot 两次,一次有标签,一次没有。 plt.plot(x_new, f(x_new), 'y', label='$r = 20')plt.plot('x1, y1, 'ro')
  • 谢谢!现在可以了

标签: python numpy matplotlib legend


【解决方案1】:

将标记换行

将 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()

现在给出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-29
    • 2018-05-31
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 2016-04-09
    • 2022-06-07
    • 1970-01-01
    相关资源
    最近更新 更多