python - Matplotlib 文本不采用数组值

标签 python python-3.x matplotlib

这是我试过的代码:

import matplotlib.pyplot as plt
import numpy as np
bb = [1,2,3,4,5,6,7,8,9,10]
cc = ["red","red","yellow","red","green","red","red","green","red","red"]
tt = ["\u2714","\u2714""\u2718","\u2714","\u2718","\u2714","\u2714","\u2718","\u2714","\u2714"]
x1=np.arange(10)
x2=np.arange(10)
fig = plt.figure()
fig.set_size_inches(50,70)
ax1 = fig.add_subplot(331)

ax1.bar(np.arange(len(bb)), bb, color=cc,width=0.6)
text_applied = ax1.text(x1,2,tt,color=cc)

plt.show()

它以前没有任何问题地给出了酒吧。但不适用于文本。我收到以下错误:

Traceback (most recent call last):
  File "C:\Python35\lib\tkinter\__init__.py", line 1549, in __call__
    return self.func(*args)
  File "C:\Python35\lib\tkinter\__init__.py", line 596, in callit
    func(*args)
  File "C:\Python35\lib\site-packages\matplotlib\backends\_backend_tk.py", line 310, in idle_draw
    self.draw()
  File "C:\Python35\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 12, in draw
    super(FigureCanvasTkAgg, self).draw()
  File "C:\Python35\lib\site-packages\matplotlib\backends\backend_agg.py", line 433, in draw
    self.figure.draw(self.renderer)
  File "C:\Python35\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Python35\lib\site-packages\matplotlib\figure.py", line 1475, in draw
    renderer, self, artists, self.suppressComposite)
  File "C:\Python35\lib\site-packages\matplotlib\image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Python35\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Python35\lib\site-packages\matplotlib\axes\_base.py", line 2607, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "C:\Python35\lib\site-packages\matplotlib\image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Python35\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Python35\lib\site-packages\matplotlib\text.py", line 706, in draw
    bbox, info, descent = textobj._get_layout(renderer)
  File "C:\Python35\lib\site-packages\matplotlib\text.py", line 282, in _get_layout
    key = self.get_prop_tup(renderer=renderer)
  File "C:\Python35\lib\site-packages\matplotlib\text.py", line 862, in get_prop_tup
    x, y = self.get_unitless_position()
  File "C:\Python35\lib\site-packages\matplotlib\text.py", line 844, in get_unitless_position
    x = float(self.convert_xunits(self._x))
TypeError: only size-1 arrays can be converted to Python scalars

请告诉我如何根据指定的颜色和 x 值 显示文本。

最佳答案

我不太明白你的问题。在 plt.text() 的文档中我找不到任何关于将字符串数组提供给参数的可能性:

Definition : text(x, y, s, fontdict=None, withdash=False, **kwargs)

Type : Function of matplotlib.pyplot module

Add text to the axes.

Add the text s to the axes at location x, y in data coordinates.

Parameters

x, y :
scalars The position to place the text. By default, this is in data coordinates. The coordinate system can be changed using the transform parameter.
s : str The text.

所以我会简单地推荐

for x, t, c in zip(x1, tt, cc):
    ax1.text(x, 2, t, color=c)

关于python - Matplotlib 文本不采用数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54744772/

相关文章:

python - 为什么一个功能可以工作,而另一个却不能?

python - GAE的webapp2_extra.i18n无法在MacOSX上导入babel

python - Pandas 在由列表组成的元素上删除重复项

python - 重新排序列表,尽可能使所有先前数字的总和小于当前数字

matplotlib - 删除 matplotlib 边距

python - boost::python 从 numpy.ndarray 返回值中提取 C++ 值

python - 我可以向 Django auth_group 表添加一列吗?

python - 为什么同一行声明的两个类对象指向一个对象?

python - matplotlib 图中的 fig 和 ax 属性

Python matplotlib 限制为整数刻度位置