1

I'm currently working on my project but I'm stuck on how to make annotation using multiprocessing or threading in matplotlib.

I want to display an interval of values inside each annotation that keep counting from the beginning of interval up to the top, I'm sure that there is a simple way to do this but I just can't figure it out.

I also have a problem with the ranges where the start value > end value cause the range doesn't work on it!

Here is what I've tried to do so far.

data0=rd.sample(range(100),20)
data1=rd.sample(range(100),20)
print(data0,'\n',data1)
Ranges = [range(y, n) for y,n in zip(data0, data1)]
fig = plt.figure(figsize=(11,6))#, facecolor="skyblue")
axes = plt.gca()
x = arange(20)
y = data0
bars = plt.barh(x, y, height=.8)

def worker(item):
    for j in item:
        ann=axes.text(bar.get_width(), bar.get_y(), '', ha='left', va='bottom',fontsize=10)
        ann.set_text(j)
        #plt.pause(.0000000000001)
pool = ThreadPool(20)
for bar, ran in zip(bars, Ranges):
    Result=pool.map(worker, (ran,))
pool.close()
pool.join()
plt.show()

I tried to use plt.clear() and plt.pause() inside of the worker but it just keep throwing the msg the main thread is not on the main loop.

What should I do to solve this problem?

  • @William Miller thank you! any idea about what I'm trying to do ? – Aziz Outighli Dec 02 '19 at 20:03
  • I'm not sure, almost all of my experience with multithreading is in C++, but I think you might have a scope issue with accessing the global `axes` object in your function. Not sure if that's the problem but [this](https://stackoverflow.com/questions/58787960/how-to-correctly-refer-to-fig-and-ax-with-moviepy-animation/59013405#59013405) answer about python variable scoping might help fix your scoping – William Miller Dec 02 '19 at 20:07
  • 1
    It is a very common restriction in multithreaded GUI systems that only the main thread can use the GUI. What would it mean to have different threads showing different plots, for instance? You just have to send the information to one thread and draw it there. – Davis Herring Dec 03 '19 at 08:27
  • @DavisHerring thank your for your feedback:) do you mean using something like 'PIPE' or something like 'Producer-consumer ' if so how to tell other thread that each output is related to each couple(range, bar)? – Aziz Outighli Dec 03 '19 at 09:59
  • @AzizOutighli: Producer-consumer is a design, not an implementation, but a good amount of threaded programming can be done with little other than the `queue` type. – Davis Herring Dec 03 '19 at 14:14
  • @DavisHerring can you please brief me on this cause I've already try queue but doesn't work for me ? maybe I've set it by the wrong way ! maybe if you can show the best way to do it using my exemple above i'll be grateful. – Aziz Outighli Dec 03 '19 at 18:39
  • @AzizOutighli: That’s not what the question says, nor is a general briefing on-topic. Edit, or ask a new question, as appropriate. – Davis Herring Dec 04 '19 at 01:15
  • @DavisHerring the question was edited can you please brief me on this? i'm quiet new in multiprocessing and threading and ive spend days to figure a solution for that but in vain – Aziz Outighli Dec 05 '19 at 11:34
  • @AzizOutighli: You can’t just edit the title! Show (a simplified version of) your **attempt** with (say) queues and what went wrong. – Davis Herring Dec 05 '19 at 14:16

0 Answers0