如何在 matplotlib 图形上绘制框架 [英] How to draw a frame on a matplotlib figure
问题描述
我想在此图中显示框架.我尝试运行下面的代码,但没有用:
I want to show the frame in this figure. I tried running the code below but it didnt work :
ax = self.canvas.figure.add_subplot(111)
ax.spines['top'].set_visible(True)
ax.spines['right'].set_visible(True)
ax.spines['bottom'].set_visible(True)
ax.spines['left'].set_visible(True)
ax.plot(diff)
我也试过:
plt.tight_layout()
但它会产生此错误:
> File "runme.py", line 54, in autocorr_function
> plt.tight_layout() File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line
> 1406, in tight_layout
> fig.tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect) File "/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py",
> line 1753, in tight_layout
> rect=rect) File "/usr/local/lib/python2.7/dist-packages/matplotlib/tight_layout.py",
> line 326, in get_tight_layout_figure
> max_nrows = max(nrows_list) ValueError: max() arg is an empty sequence
感谢您的帮助,谢谢.
这是画布的代码:
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt from PyQt4 import QtGui
class Canvas(FigureCanvas):
def __init__(self, parent=None):
self.figure = plt.figure() #plt.tight_layout(pad=4)
FigureCanvas.__init__(self, self.figure)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
推荐答案
边界(或刺)已经可见,但为白色.您需要按照此响应中的说明更改颜色: https://stackoverflow.com/a/43265998/1773344
The borders (or spines) are already visible, but white. You need to change the color as explained in this response: https://stackoverflow.com/a/43265998/1773344
例如某种灰色:
ax.spines['bottom'].set_color('0.5')
ax.spines['top'].set_color('0.5')
ax.spines['right'].set_color('0.5')
ax.spines['left'].set_color('0.5')
如果您想要轴周围的边框,则没有简单的解决方案.除了可能将您的轴放到具有调整边界的轴之内(如此答案部分所述): https://stackoverflow.com/a/22608025/1773344
If you want the borders AROUND the axes, there is no simple solution. Except perhaps putting your axis inside an axis with adjusted borders as partially explained in this answer: https://stackoverflow.com/a/22608025/1773344
这篇关于如何在 matplotlib 图形上绘制框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!