首页 > 解决方案 > errorbar 函数错误 matplotlib.pyplot

问题描述

我正在尝试定义一个函数,该函数会为 y 的某些给定值及其错误以及 x 值及其输入的错误生成带有误差线的散点图。我对此比较陌生,无法完全破译回溯错误。我编写的函数适用于 scatter(),但不适用于 errorbar()。任何对正确方向的点头,即使它只是解释回溯,将不胜感激。

import matplotlib.pyplot as plt
import numpy as np
def definedscatter(yvalues,xax,yax):
  print('When entering data, type "done" to finish input for the current variable.')
  plt.figure(1)
  xvalues=[]
  xerrors=[]
  i=1
  xn=None
  while True:
      try:
          xn=input('x value #'+str(i)+': ')
          if xn=='done':
              break
          else:
              test1=float(xn)
              xnerr=input('Error in x value #'+str(i)+': ')
              if xnerr=='done':
                  break
              else:
                  test2=float(xnerr)
                  xnli=[xn]
                  xnerrli=[xnerr]
                  i=i+1
                  xvalues=xvalues+xnli #can't concatenate float to list, only list
                  xerrors=xerrors+xnerrli
      except:
          print('Please enter a numerical value, or type "done" if you have finished inputting data.')
  plt.errorbar(xvalues,yvalues, xerr=xerrors , yerr=yerrors , fmt='o') #scatter func program works fine, but errorbar doesn't, so something wrong with func args
  plt.xlabel(xax)
  plt.ylabel(yax)
  plt.show()
y=[1,2,4,9]
yerrors=[0.3,0.2,0.5,0.8]
xaxis=input('x axis: ')
yaxis=input('y axis: ')
definedscatter(y,xaxis,yaxis)

标签: pythonmatplotlibexceptionplot

解决方案


推荐阅读