0

I'm trying to create a chart of X & Y values, but the values are now showing on the chart. Others have encountered the same problem. Case in point:

matplotlib does not show my drawings although I call pyplot.show()

I tried following the suggestions but nothing works. I printed the backends:

['GTK3Agg', 'GTK3Cairo', 'MacOSX', 'nbAgg', 'Qt4Agg', 'Qt4Cairo', 'Qt5Agg', 'Qt5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo', 'agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template']

....however I'm not sure how to use them or find out if there is a missing dependency.

Output when running the code:

python3 plot_static.py
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/cbook/__init__.py", line 216, in process
    func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/animation.py", line 953, in _start
    self._init_draw()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/animation.py", line 1732, in _init_draw
    self._draw_frame(next(self.new_frame_seq()))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/animation.py", line 1755, in _draw_frame
    self._drawn_artists = self._func(framedata, *self._args)
  File "plot_static.py", line 25, in animate
    x, y = line.split(',') # Delimiter is comma
ValueError: not enough values to unpack (expected 2, got 1)

Plarform: MACOS Python3code

wiwinut
  • 65
  • 1
  • 6

1 Answers1

0

Can you please share the content of stock.txt file. The backend which you have printed how it is related to plot. Anyways I think below things may help you:

The original code from syntax:

def animate(i):
    graph_data = open('example.txt','r').read()
    lines = graph_data.split('\n')
    xs = []
    ys = []
    for line in lines:
        if len(line) > 1:
            x, y = line.split(',')
            xs.append(float(x))
            ys.append(float(y))
    ax1.clear()
    ax1.plot(xs, ys)

you are getting error at line x, y = line.split(',') it means the data in your stock.txt is not same as syntax's example.txt , It must be comma separated 2 values.Just to test put below lines in your stock.txt and make sure there is no blank lines in your stock.txt, if your have any blank lines there you should check for the condition if len(line) > 1: which you have missed.

1,5

2,3

3,4

4,7

5,4

6,3

7,5

8,7

9,4

10,4

  • Thanks. Tried the suggestion with the stock.txt and got the same result. Chart shows up, but data does not. I should also note that I'm having the same results with my Windows machine with the same versions. – wiwinut Jan 04 '20 at 21:39
  • Good to hear you got chart now, I did not understand what do you mean by `but data does not` , Are you not able to see the labels on the plot? – Abhishek Kumar Dubey Jan 04 '20 at 22:57
  • The chart was always present. That was never the problem. Sorry for the confusion. In simpler terms the plot lines (data) do not show up on the chart. – wiwinut Jan 04 '20 at 23:39
  • 1
    Probably better to write `xy = line.split(',')`, `if len(xy) != 2: print("Oops", xy)`, `else: xs.append(float(xy[0]); ys.append(float(xy[1])` – JohanC Jan 05 '20 at 00:07
  • 1
    yes chart (canvas) will be there even if matplotlib shows data error but the plot will not be there as there is an error in the data. Hope your problem is solved if not yet please post the content of stock.txt – Abhishek Kumar Dubey Jan 05 '20 at 00:17
  • Thanks Johan. That did it! The data on my chart shows up and my output is: Oops [''] Oops [''] Oops [''] Oops [''] Oops [''] Oops [''] – wiwinut Jan 05 '20 at 00:23