Matplotlib show one axis and no data

2019-09-03 02:10发布

I want to show just the x axis for a figure and have another figure below plotting the data (the lower one with the same x axis values as to top plot but not showing any ticks or labels).

I haven't seen any documentation of changing the actual plot dimensions, only the figure dimensions.

The code below gives some idea, but I want the top plot to show just a horizontal line and x values, the y axis should have no dimension. By setting subplots_adjust I can get the y axis as close as I can to zero, but setting bottom any larget (bottom=0.9 I get a ValueError(bottom cannot be >= top)

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QSizePolicy
import sys

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import numpy as np

qapp = QtGui.QApplication(sys.argv)
qWidget = QtGui.QWidget()
qWidget.setGeometry(QtCore.QRect(0, 0, 400, 400))
hBoxLayout = QtGui.QHBoxLayout(qWidget)
qWidget.setLayout(hBoxLayout)

qScroll = QtGui.QScrollArea(qWidget)
qScroll.setGeometry(QtCore.QRect(0, 0, 400, 400))
qScroll.setFrameStyle(QtGui.QFrame.NoFrame)
qScroll.setVisible(True)
hBoxLayout.addWidget(qScroll)

qScrollContents = QtGui.QWidget()
qScrollLayout = QtGui.QVBoxLayout(qScrollContents)
qScrollLayout.setGeometry(QtCore.QRect(0, 0, 800, 800))

qScroll.setWidget(qScrollContents)
qScroll.setWidgetResizable(True)

qFigWidget1 = QtGui.QWidget(qScrollContents)
fig1 = Figure(figsize=(5.0, 1.0), dpi=100)
canvas = FigureCanvas(fig1)
canvas.setParent(qFigWidget1)
canvas.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)

ax1 = fig1.add_subplot(111)
x = np.arange(0, 10.0, 0.1)
yData = np.zeros(100)  
ax1.plot(x, yData)
ax1.set_ylim(0, 0)
ax1.get_xaxis().tick_bottom() 
ax1.axes.get_yaxis().set_visible(False)
fig1.subplots_adjust(bottom=0.85)

plotLayout = QtGui.QVBoxLayout()
plotLayout.addWidget(canvas)
qFigWidget1.setLayout(plotLayout)
qScrollLayout.addWidget(qFigWidget1)

qFigWidget2 = QtGui.QWidget(qScrollContents)
fig2 = Figure(figsize=(5.0, 5.0), dpi=100)
canvas = FigureCanvas(fig2)
canvas.setParent(qFigWidget2)
canvas.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)

ax2 = fig2.add_subplot(111)
x = np.arange(0, 10.0, 0.1)
ax2.plot(x, x*x)
ax2.axes.get_xaxis().set_visible(False)

plotLayout = QtGui.QVBoxLayout()
plotLayout.addWidget(canvas)

qFigWidget2.setLayout(plotLayout)
qScrollLayout.addWidget(qFigWidget2)
qScroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
qScroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)

qScrollContents.setLayout(qScrollLayout)

qWidget.show()
exit(qapp.exec_()) 

0条回答
登录 后发表回答