简体   繁体   English

pyqtgraph中的极坐标系

[英]Polar coordinate system in pyqtgraph

I need to implement two graphs in Cartesian and polar coordinates.我需要在笛卡尔坐标和极坐标中实现两个图形。 Everything is clear with Cartesian, but is it possible to make a polar coordinate system in pyqtgraph?使用笛卡尔坐标系一切都很清楚,但是是否可以在 pyqtgraph 中制作极坐标系?

pyqtgraph does not provide by default the ability to make polar plots, I have requested the feature through the issue #452 , in that discussion it is indicated that you can create that type plot easily by giving an example here .默认情况下,pyqtgraph 不提供制作极坐标图的能力,我已通过问题#452请求该功能,在该讨论中表明您可以通过在此处提供示例轻松创建该类型图。

The example is as follows:示例如下:

import numpy as np
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg

plot = pg.plot()
plot.setAspectLocked()

# Add polar grid lines
plot.addLine(x=0, pen=0.2)
plot.addLine(y=0, pen=0.2)
for r in range(2, 20, 2):
    circle = pg.QtGui.QGraphicsEllipseItem(-r, -r, r * 2, r * 2)
    circle.setPen(pg.mkPen(0.2))
    plot.addItem(circle)

# make polar data
theta = np.linspace(0, 2 * np.pi, 100)
radius = np.random.normal(loc=10, size=100)

# Transform to cartesian and plot
x = radius * np.cos(theta)
y = radius * np.sin(theta)
plot.plot(x, y)

if __name__ == "__main__":
    import sys

    if (sys.flags.interactive != 1) or not hasattr(QtCore, "PYQT_VERSION"):
        QtGui.QApplication.instance().exec_()

在此处输入图片说明

Probably in future release pyqtgraph will offer that feature.可能在将来的版本中 pyqtgraph 将提供该功能。

I can offer you to use the QPolarChart from PyQt5.QtChart.我可以让您使用 PyQt5.QtChart 中的 QPolarChart。 It's really easy.这真的很容易。 For example:例如:

    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtWidgets import QVBoxLayout
    from PyQt5.QtChart import QPolarChart, QChartView, QValueAxis, QScatterSeries

    self.polar = QPolarChart()      
    chartView = QChartView(self.polar)
    
    layout = QVBoxLayout()
    layout.addWidget(chartView)
    
    #Let's create container widget for our chart, for example QFrame
    #Instead the MainWindow you should to substitute your own Widget or Main Form
    
    self.MyFrame = QtWidgets.QFrame(MainWindow)
    self.MyFrame.setGeometry(QtCore.QRect(0, 0, 1000, 1000))
    self.MyFrame.setLayout(layout)
    
    #setting axis
    axisy = QValueAxis()
    axisx = QValueAxis()
    
    axisy.setRange(0,500)
    axisy.setTickCount(4)
    self.polar.setAxisY(axisy)
                  
    axisx.setRange(0,360)
    axisx.setTickCount(5)
    self.polar.setAxisX(axisx)
    
    #Let's draw scatter series
    self.polar_series = QScatterSeries()
    self.polar_series.setMarkerSize(5.0)        
    
    self.polar_series.append(0, 0);
    self.polar_series.append(360, 500); 
    
    #Why not draw archimedes spiral
    for i in range(0,360,10): 
        self.polar_series.append(i, i)
        
    self.polar.addSeries(self.polar_series)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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