2

Here is the code i tried:

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

It was giving bars without any issue previously. But is not working with text. I am getting the following error:

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

Please let me know what I can do to display the texts according to the color and x values specified.

2 Answers 2

6

I don't quite get your issue. In the docs of plt.text() I can find nothing about the possibility to feed arrays of strings into the parameters:

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.

So I'd recommend simply

for x, t, c in zip(x1, tt, cc):
    ax1.text(x, 2, t, color=c)
Sign up to request clarification or add additional context in comments.

Comments

5

I'm not sure exactly what is your expected output, but if you want to have symbols with different colors below each bar you could set them as ticks like this:

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"]

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.bar(range(len(bb)), bb, color=cc, width=0.6)
ax1.set_xticks(range(len(bb)))
ax1.set_xticklabels(tt)
for xtick, color in zip(ax1.get_xticklabels(), cc):
    xtick.set_color(color)
plt.show()

Output:

Bars with colored ticks

Btw, note that in your code tt was missing a comma between the second and third elements.

3 Comments

That's a nice pattern putting a getter for the labels into a for loop to iteratively access properties of the listed objects it returns!
I am thankful for your edition sir. Yes the above output is what I am expecting. Just for information, May I know if I want it to change over time, do I need to clear the axes to avoid overlap or you have some thought over it
@JafferWilson You can clear the axes and draw again or you can edit the objects that you have already plotted. In general, in Matplotlib every plotting command (plot, bar, text, ...) returns one or more objects that can be manipulated, or you can retrieve the drawing primitives from the axes too I think (for example, if you use FuncAnimation this is much more efficient than clearing and redrawing on each frame).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.