【问题标题】:Set gradient color for bar graphs in pyqtgraph在 pyqtgraph 中为条形图设置渐变颜色
【发布时间】:2020-08-04 22:40:49
【问题描述】:

我正在尝试在 pyqtgraph 中为我的条形图设置颜色渐变 - 目标是制作在 y 方向上改变颜色的渐变。

到目前为止,我有这段代码 sn-p 来尝试制作渐变,但没有绘制(或刷)条:

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

# Make gradient for bar plot
grad = QtGui.QLinearGradient(0, 0, 0, 3)
grad.setColorAt(0.1, pg.mkColor('#000000'))
grad.setColorAt(0.9, pg.mkColor('b'))
brush = QtGui.QBrush(grad)

# Attempt to add gradient to bar plot
self.bar = pg.BarGraphItem(x=data_x, height=data_y, width=700, brush=brush)
self.win = pg.plot()
self.win.addItem(self.bar, ignoreBounds=False)

【问题讨论】:

    标签: python-3.x user-interface plot pyqt5 pyqtgraph


    【解决方案1】:

    QGradients 有不同的coordinate modes

    默认模式是LogicalMode,这意味着为该渐变设置并用于绘画的坐标使用逻辑值(如“像素”)。

    由于 pyqtgraph 项目通常显示很小的数据值并使用相对坐标,这会导致您的渐变不完全可见,可能是因为这些条的值太小。

    将坐标限制在0-1范围内,然后设置相对坐标模式:

    grad = QtGui.QLinearGradient(0, 0, 0, 1)
    grad.setCoordinateMode(QtGui.QGradient.ObjectBoundingMode)
    

    请注意,由于 Qt5.12 ObjectBoundingMode 已被弃用,应改用ObjectMode

    【讨论】:

      猜你喜欢
      • 2016-10-01
      • 2019-09-04
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多