pandas 如何在 matplotlib 图形上绘制框架
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45585183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw a frame on a matplotlib figure
提问by Sara Lussi
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)
I also tried :
我也试过:
plt.tight_layout()
but it generates this error :
但它会产生这个错误:
> 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
Your help would be appreciated, thank you.
您的帮助将不胜感激,谢谢。
EDIT : Here's the code of the canvas :
编辑:这是画布的代码:
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)
回答by Wli
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
边框(或刺)已经可见,但是是白色的。您需要按照此回复中的说明更改颜色:https: //stackoverflow.com/a/43265998/1773344
example for some kind of gray:
例如某种灰色:
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')
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
如果您想要轴周围的边框,则没有简单的解决方案。除了可能将您的轴放在带有调整边框的轴内,如本答案中部分所述:https: //stackoverflow.com/a/22608025/1773344
回答by ImportanceOfBeingErnest
The main point is that you seem to be using seaborn or at least the seaborn darkgrid style. If this is indeed desired, but you still want a border around the axes, you need to change the style.
主要的一点是你似乎在使用 seaborn 或至少是 seaborn darkgrid 风格。如果确实需要这样做,但您仍然希望轴周围有边框,则需要更改样式。
plt.rcParams["axes.edgecolor"] = "black"
plt.rcParams["axes.linewidth"] = 1
This is (as @Wli mentions) explained in this answer.
这是(正如@Wli 提到的)在这个答案中解释的。
This is all independent of tight_layout
. The reason why plt.tight_layout()
produces the error shown cannot be determined for sure with the information at hand. However, you might in general be better off calling the figure's method than using pyplot
in the GUI. SO you may try self.canvas.figure.tight_layout()
instead.
这一切都独立于tight_layout
. plt.tight_layout()
无法根据手头的信息确定产生所示错误的原因。但是,pyplot
与在 GUI 中使用相比,您通常最好调用图形的方法。所以你可以试试self.canvas.figure.tight_layout()
。