[英]How can I control the alignment of y axes in multiple subplots with pyqtgraph when the tick labels have different lengths?
I am using pyqtgraph to make a plot with a few subplots and shared X axes.我正在使用 pyqtgraph 制作带有一些子图和共享 X 轴的图。 However, when the tick labels have different lengths, the Y axes do not line up with each other.
但是,当刻度标签的长度不同时,Y 轴不会彼此对齐。 How can I make the plots line up properly, despite differences in the size of tick labels or axis labels?
尽管刻度标签或轴标签的大小不同,如何使绘图正确排列?
from PyQt4 import QtGui, QtCore
import pyqtgraph as pg
import numpy as np
# Make up some data ----------
x = np.linspace(0, 80, 81)
y1 = np.sqrt(x)
y2 = 10*x**2
# Set up the plot ----------
# Switch to using white background and black foreground
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
app = QtGui.QApplication([])
pw = pg.GraphicsWindow(title="plot test")
gv = pw
layout_ = pg.GraphicsLayout()
gv.setCentralItem(layout_)
gv.show()
layout = layout_.addLayout(colspan=1)
layout.setContentsMargins(10, 10, 10, 10)
# Top row: title
layout.addLabel('title', colspan=2)
# Next row: main plot with y axis label
layout.nextRow()
layout.addLabel('y1', angle=-90, rowspan=1)
p1 = layout.addPlot()
# Next row: secondary plot
layout.nextRow()
layout.addLabel('y2', angle=-90, rowspan=1)
p2 = layout.addPlot()
# Bottom row: X axis label
layout.nextRow()
layout.addLabel("x", col=1, colspan=1)
# Share X axes
p2.setXLink(p1)
# Add data to plot ---------
p1.plot(x, y1, pen=pg.mkPen((255, 0, 0, 255), width=3))
p2.plot(x, y2, pen=pg.mkPen((0, 0, 255, 255), width=3))
Just link the 'left' axis to a view that is registered.只需将“左”轴链接到已注册的视图。 Something like:
就像是:
Widths = [i.width() for i in xAxis]
maximum=Widths.index(max(Widths))
viewBox[maximum].register('largest')
for i in listPlots:
i.getAxis('left').linkToView(viewBox[maximum])
where listPlots is the list of plotDataItems其中 listPlots 是 plotDataItems 的列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.