如何限制matplotlib图上的边界大小

如何限制matplotlib图上的边界大小

本文介绍了如何限制matplotlib图上的边界大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一些非常漂亮的图表,并且边框中的空白占用了很多像素,可以更好地用于数据。看起来边框随着图形的增长而增长。

I'm making some pretty big graphs, and the whitespace in the border is taking up a lot of pixels that would be better used by data. It seems that the border grows as the graph grows.

以下是我的图形代码的内容:

Here are the guts of my graphing code:

        import matplotlib
        from pylab import figure

        fig = figure()
        ax = fig.add_subplot(111)
        ax.plot_date((dates, dates), (highs, lows), '-', color='black')
        ax.plot_date(dates, closes, '-', marker='_', color='black')

        ax.set_title('Title')
        ax.grid(True)
        fig.set_figheight(96)
        fig.set_figwidth(24)

有没有办法减小边界的大小?也许一个设置的地方,可以让我保持边界不变2英寸左右?

Is there a way to reduce the size of the border? Maybe a setting somewhere that would allow me to keep the border at a constant 2 inches or so?

推荐答案

由于您看起来只是使用单个子图,因此您可能需要跳过 add_subplot 并直接前往。这将允许您给出轴的大小(以相对于图的坐标),因此您可以在图中将其设置得尽可能大。在你的情况下,这将意味着你的代码看起来像

Since it looks like you're just using a single subplot, you may want to skip add_subplot and go straight to add_axes. This will allow you to give the size of the axes (in figure-relative coordinates), so you can make it as large as you want within the figure. In your case, this would mean your code would look something like

    import matplotlib.pyplot as plt

    fig = plt.figure()

    # add_axes takes [left, bottom, width, height]
    border_width = 0.05
    ax_size = [0+border_width, 0+border_width,
               1-2*border_width, 1-2*border-width]
    ax = fig.add_axes(ax_size)
    ax.plot_date((dates, dates), (highs, lows), '-', color='black')
    ax.plot_date(dates, closes, '-', marker='_', color='black')

    ax.set_title('Title')
    ax.grid(True)
    fig.set_figheight(96)
    fig.set_figwidth(24)

如果你愿意,你甚至可以把参数设置为 set_figheight / set_figwidth 直接在 figure()调用中。

If you wanted, you could even put the parameters to set_figheight/set_figwidth directly in the figure() call.

这篇关于如何限制matplotlib图上的边界大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:53