相关疑难解决方法(0)

如何在matplotlib中一次分配多个标签?

我有以下数据集:

x = [0, 1, 2, 3, 4]
y = [ [0, 1, 2, 3, 4],
      [5, 6, 7, 8, 9],
      [9, 8, 7, 6, 5] ]
Run Code Online (Sandbox Code Playgroud)

现在我绘制它:

import matplotlib.pyplot as plt
plt.plot(x, y)
Run Code Online (Sandbox Code Playgroud)

但是,我想用这个命令标记3个y数据集,这会在.legend()调用时引发错误:

lineObjects = plt.plot(x, y, label=['foo', 'bar', 'baz'])
plt.legend()

File "./plot_nmos.py", line 33, in <module>
  plt.legend()
...
AttributeError: 'list' object has no attribute 'startswith'
Run Code Online (Sandbox Code Playgroud)

当我检查lineObjects:

>>> lineObjects[0].get_label()
['foo', 'bar', 'baz']
>>> lineObjects[1].get_label()
['foo', 'bar', 'baz']
>>> lineObjects[2].get_label()
['foo', 'bar', 'baz']
Run Code Online (Sandbox Code Playgroud)

是否有一种优雅的方法来通过使用 …

label matplotlib legend

48
推荐指数
5
解决办法
6万
查看次数

PathCollection' 对象没有属性 legend_elements''

我知道这里有人问这个确切的问题,但是当前的解决方案对我没有任何帮助。我似乎无法为每个标签生成一个具有不同颜色的图例。我已经尝试了Matplotlib上的当前文档,但无济于事。我不断收到我的错误PathCollection object has no attribute legend_elements

编辑:另外,我希望我的图例只是Years情节的独特年份,而不是现在的情况是每个数据点都映射到我的图例。这是我所拥有的

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
from matplotlib.pyplot import legend
import os

%config InlineBackend.figure_format = 'retina'

path = None
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        path = os.path.join(dirname, filename)

# Indexes to be removed
early_demo_dividend = 13
high_income = …
Run Code Online (Sandbox Code Playgroud)

matplotlib python-3.x

5
推荐指数
2
解决办法
8528
查看次数

Matplotlib set_data 使我的程序冻结(请检查“更新问题”)

我试图绘制两个更新图,一个是图表,另一个是从相机捕获的图像。

我在这一行收到一个错误:

"current_line.set_data(y_data)" in the "update" function.  
The error says: "AttributeError: 'list' object has no attribute 'set_data'".
Run Code Online (Sandbox Code Playgroud)

知道为什么我会收到此错误吗?如果我注释掉这一行,我将从相机中获取不断变化的图像,除第二个图外的所有内容看起来都很好(因为第二个图未更新)但我也需要更新第二个图。

y_data = [0]
# Capture intial frame
ret, initial_frame = lsd.cap.read()

# Function for making the initial figure
def makeFigure():
    fig = plt.figure()

    # Show frame
    ax1 = plt.subplot2grid((2, 2), (0, 0), colspan=2)
    plot_frame = ax1.imshow(initial_frame, animated=True)

    # Set the limits of the plot and plot the graph
    ax2 = plt.subplot2grid((2, 2), (1, 0), colspan=2)
    ax2.set_title('Title')
    ax2.set_ylabel('Y-Label')
    ax2.set_ylim(0, 100)
    ax2.set_xlim(0, 100)
    ax2.grid() …
Run Code Online (Sandbox Code Playgroud)

python opencv matplotlib python-multithreading

0
推荐指数
1
解决办法
3626
查看次数