Pyqtgraph 剪辑线
Posted
技术标签:
【中文标题】Pyqtgraph 剪辑线【英文标题】:Pyqtgraph clip line 【发布时间】:2015-02-13 07:19:19 【问题描述】:我正在尝试在 pyqtgraph 中绘制史密斯图。我想知道是否有剪辑的方法 椭圆项目表示虚圆,实圆半径 = 1。 这就是我到目前为止所做的:我使用了方法 start angle e span angle of QGraphicsEllipse,但以这种方式还绘制了圆的垂直和水平线。 matplotlib里面有个方法叫set_clip_path(),不知道pyqtgraph里面有没有这样的东西?
import pyqtgraph as pg
plot = pg.plot()
plot.setAspectLocked()
plot.addLine(y=0)
#vector for real circle
rline = [0.2, 0.5, 1.0, 2.0, 5.0]
#vector for imaginary
xline = [0.2, 0.5, 1, 2, 5]
circle1 = pg.QtGui.QGraphicsEllipseItem(1, -1, -2, 2)
circle1.setPen(pg.mkPen(1))
plot.addItem(circle1)
for r in rline:
raggio = 1./(1+r)
circle = pg.QtGui.QGraphicsEllipseItem(1, -raggio, -raggio*2, raggio*2)
circle.setPen(pg.mkPen(0.2))
plot.addItem(circle)
for x in xline:
#printing the imaginary circle
circle = pg.QtGui.QGraphicsEllipseItem(x + 1, 0, -x*2, x*2)
circle.setPen(pg.mkPen(0.2))
circle.setStartAngle(1440)
circle.setSpanAngle(1440)
plot.addItem(circle)
编辑
这是我的最终代码
plot.setAspectLocked()
plot.setXRange(-1,1, padding = 0)
plot.setYRange(-1,1, padding = 0)
#plot.addLine(y=0)
rline = [0.2, 0.5, 1.0, 2.0, 5.0]
xline = [0.2, 0.5, 1, 2, 5]
circle1 = pg.QtGui.QGraphicsEllipseItem(1, -1, -2, 2)
circle1.setPen(pg.mkPen('w', width=0))
circle1.setFlag(circle1.ItemClipsChildrenToShape)
plot.addItem(circle1)
pathItem = pg.QtGui.QGraphicsPathItem()
path = pg.QtGui.QPainterPath()
path.moveTo(1, 0)
for r in rline:
raggio = 1./(1+r)
path.addEllipse(1, -raggio, -raggio*2, raggio*2)
for x in xline:
path.arcTo(x + 1, 0, -x*2, x*2, 90, -180)
path.moveTo(1, 0)
path.arcTo(x + 1, 0, -x*2, -x*2, 270, 180)
pathItem.setPath(path)
pathItem.setPen(pg.mkPen('g', width = 0.2))
pathItem.setParentItem(circle1)
`
【问题讨论】:
【参考方案1】:支持剪辑,但可能不是最佳选择。几种可能性:
使用QGraphicsPathItem 结合QPainterPath.arcTo 绘制没有径向线的圆弧。这还允许您向单个项目添加多个弧,而不是添加许多项目,这应该会提高性能。
使用 PlotCurveItem 或 arrayToQPath 之类的东西手动绘制自己的弧线。如果您使用 connect
参数,您将再次能够在单个项目上生成多个单独的弧。
剪辑由 Qt 处理;见QGraphicsItem.itemClipsToShape and QGraphicsItem.itemClipsChildrenToShape。注意:如果你使用这个,你必须将剪辑对象的笔宽设置为 0(Qt 仅部分支持宽度 > 0 的化妆笔)。示例:
import pyqtgraph as pg
plot = pg.plot()
e1 = pg.QtGui.QGraphicsEllipseItem(0, 0, 4, 4)
# MUST have width=0 here, or use a non-cosmetic pen:
e1.setPen(pg.mkPen('r', width=0))
e1.setFlag(e1.ItemClipsChildrenToShape)
plot.addItem(e1)
e2 = pg.QtGui.QGraphicsEllipseItem(2, 2, 4, 4)
e2.setPen(pg.mkPen('g'))
e2.setParentItem(e1)
【讨论】:
好的,谢谢您的选择。我会试试的,我会告诉你的 我用第一种方法把所有的网格线(实部和虚部)都画出来,然后用module 1的圆圈剪掉完整的路径。谢谢以上是关于Pyqtgraph 剪辑线的主要内容,如果未能解决你的问题,请参考以下文章