我有如下2d numpy数组
import numpy as np
agents = np.array([[ 1. , -71.8, 41.2],
[ 1. , -71.8, 41.3],
[ 0. , -71.8, 41.4],
[ 0. , -71.7, 41.4],
[ 0. , -71.6, 41.4]])
现在我想形象化它。在matplotlib中,我可以执行类似的操作,因为我需要根据第一列的值使用不同的颜色。
import matplotlib.pyplot as plt
plt.scatter(agents [:,1],agents [:,2],c=agents [:,0])
但是如何在pyqtgraph中获得相同的结果?我试过了
app = QtGui.QApplication(sys.argv)
mw = QtGui.QMainWindow()
mw.resize(800, 800)
view = pg.GraphicsLayoutWidget()
mw.setCentralWidget(view)
mw.setWindowTitle('pyqtgraph example: ScatterPlot')
w1 = view.addPlot()
x = agents[:,1]
y = agents[:,2]
z = agents[:,0]
s = pg.ScatterPlotItem(x, y,pen=pg.mkPen(z))
w1.addItem(s)
mw.show()
sys.exit(QtGui.QApplication.exec_())
获得此例外
Exception: Not sure how to make a color from "(array([1., 0., 0., 0.]),)"
如果要为每个点设置颜色,则必须传递颜色列表,如果要传递给列表,则如果要设置边框的颜色,则必须传递QPen,如果要设置颜色,则必须传递QBrush您要设置填充颜色。
据我了解,您想要最新的:
s = pg.ScatterPlotItem(x, y, brush=[pg.mkBrush(v) for v in z])
# or
# s = pg.ScatterPlotItem(x, y, brush=list(map(pg.mkBrush, z)))
如果要设置边框颜色,请使用:
s = pg.ScatterPlotItem(x, y, pen=[pg.mkPen(v) for v in z])
# or
# s = pg.ScatterPlotItem(x, y, pen=list(map(pg.mkPen, z)))