【问题标题】:Polar coordinate system in pyqtgraphpyqtgraph中的极坐标系
【发布时间】:2019-12-02 02:11:30
【问题描述】:

我需要在笛卡尔坐标和极坐标中实现两个图形。 Cartesian 一切都清楚了,但是在pyqtgraph中是否可以制作极坐标系?

【问题讨论】:

  • 欢迎来到stackoverflow。如果没有一些示例代码或其他上下文,很难回答您的问题。请看stackoverflow.com/help/how-to-ask
  • @Simon.S.A.在这个问题中,代码不是必需的,因为他的代码中没有错误,但要求提供新功能。不是每个问题都需要代码。

标签: python pyqt pyqt5 pyqtgraph


【解决方案1】:

pyqtgraph 默认不提供制作极坐标图的功能,我已通过问题#452 请求了该功能,在该讨论中表明您可以通过给出示例here 轻松创建该类型图。

示例如下:

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_()

可能在未来的版本中 pyqtgraph 将提供该功能。

【讨论】:

    【解决方案2】:

    我可以让您使用 PyQt5.QtChart 中的 QPolarChart。这真的很容易。例如:

        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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 2011-04-17
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多