pyqtgraph setdata方法源码
时间: 2023-12-22 08:02:56 浏览: 108
PyQt计算器源码
5星 · 资源好评率100%
PyQtGraph的PlotCurveItem对象中的setData()方法用于更新绘图数据。下面是它的源代码实现:
```python
def setData(self, *args, **kargs):
if len(args) == 1:
data = args[0]
if isinstance(data, np.ndarray):
if data.ndim == 1:
data = data.reshape((data.size, 1))
if data.ndim != 2:
raise Exception("setData only accepts 1D or 2D arrays for now")
if data.shape[1] != 2:
raise Exception("setData expects an Nx2 array (or list of tuples)")
self.xData, self.yData = data.T
elif isinstance(data, list) or isinstance(data, tuple):
if len(data) == 2 and isinstance(data[0], list):
data = [np.array(data[0]), np.array(data[1])]
if len(data[0]) != len(data[1]):
raise Exception("setData called with x, y lists of different lengths")
self.xData = np.array(data[0])
self.yData = np.array(data[1])
else:
raise Exception("setData accepts only numpy arrays, or an x,y tuple/list")
elif len(args) == 2:
self.xData = np.array(args[0])
self.yData = np.array(args[1])
else:
raise Exception("setData must be called with an Nx2 array (or two 1D arrays) or with x,y lists")
if 'x' in kargs:
self.opts['x'] = kargs['x']
if 'y' in kargs:
self.opts['y'] = kargs['y']
if 'pen' in kargs:
self.opts['pen'] = kargs['pen']
if 'shadowPen' in kargs:
self.opts['shadowPen'] = kargs['shadowPen']
if 'fillLevel' in kargs:
self.opts['fillLevel'] = kargs['fillLevel']
if 'brush' in kargs:
self.opts['brush'] = kargs['brush']
if 'antialias' in kargs:
self.opts['antialias'] = kargs['antialias']
if 'name' in kargs:
self.opts['name'] = kargs['name']
if self.xData is not None:
self.updateItems()
```
setData()方法的参数可以是一个numpy数组,也可以是一个元组或列表,其中元组或列表的第一个元素是x轴数据,第二个元素是y轴数据。该方法首先检查数据类型,并根据需要将数据转换为numpy数组。然后,它检查是否有任何可选参数被传递,并根据需要更新PlotCurveItem的opts字典。最后,它调用updateItems()方法来更新绘图元素。
阅读全文