简体   繁体   中英

Matplotlib does not show chart values

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 Python3 代码

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM