【问题标题】:Matplotlib.pyplot - Deactivate axes in figure. /Axis of figure overlap with axes of subplotMatplotlib.pyplot - 停用图中的轴。 /图形轴与子图轴重叠
【发布时间】:2016-10-16 13:52:02
【问题描述】:
%load_ext autoreload 
%autoreload 2
%matplotlib inline

import numpy as np
import datetime as dt
import pickle
import pandas as pd
import datetime
from datetime import timedelta, date
from datetime import date as dt
import math
import os
import matplotlib.pyplot as plt
plt.style.use('ggplot')
from pylab import plot,show
from matplotlib import ticker
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.dates as mdates
import pylab as pl

x_min_global=datetime.date(2010,1, 1)- timedelta(days=180)
x_max_global=datetime.date(2015,1, 1)+ timedelta(days=180)

d = pd.DataFrame(0, index=np.arange(155),columns=['Zeros'])
d = pd.DataFrame(0, index=np.arange(2),columns=['Zeros'])

wd=os.getcwd()

def format_date(x, pos=None):
            return pl.num2date(x).strftime('%Y-%m-%d')

with PdfPages(wd+'/Plots.pdf') as pdf:

    #Plot 1: All
    fig = plt.figure(facecolor='white',frameon=True,figsize=(30, 30))
    plt.title('Page One ', y=1.08)

    axes1 = fig.add_subplot(3,1,1,frameon=False)

    axes1.set_xlim(x_min_global,x_max_global)

    axes1.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))

    #Plot line a at 0-level
    axes1.plot([x_min_global,x_max_global],d.loc[0:1],color='k', linewidth=2.0, markersize=10.0)     

    # labels and legend
    axes1.set_title('Plot 1')
    plt.xlabel('Time', fontsize=10)
    plt.ylabel('Y-Values', fontsize=10)
    axes1.legend(loc='upper left')

    #-----------------------------------------------------------------------
    #Plot 2: 
    axes1 = fig.add_subplot(3,1,2,frameon=False)

    axes1.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))

    #Plot line a at 0-level
    axes1.plot([x_min_global,x_max_global],d.loc[0:1],color='k', linewidth=2.0, markersize=10.0)

    # labels and legend
    axes1.set_title('Plot 2')
    plt.xlabel('Time', fontsize=10)
    plt.ylabel('Y-Values', fontsize=10)
    axes1.legend(loc='upper left')

    #-----------------------------------------------------------------------
    #Plot 3:
    axes2 = fig.add_subplot(3,1,3,frameon=False)

    axes2.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))

    #Plot line a at 0-level
    axes2.plot([x_min_global,x_max_global],d.loc[0:1],color='k', linewidth=2.0, markersize=10.0)

    # labels and legend
    axes2.set_title('Plot 3')
    plt.xlabel('Time', fontsize=10)
    plt.ylabel('Y-Values', fontsize=10)
    axes2.legend(loc='upper left')

    pdf.savefig(frameon=False,transparent=False,papertype='a4')  # saves the current figure into a pdf page
    plt.show()
    plt.close()

我的问题是,在图形轴上方的代码生成的图中,sublpots 似乎重叠(参见图片中的红色矩形)。我试图关闭图中的轴。但是,我无法弄清楚。

感谢任何帮助。谢谢。

代码/问题的输出:

【问题讨论】:

    标签: python matplotlib axis figure


    【解决方案1】:

    如果你想完全移除轴,你应该尝试:

    plt.axis('off')
    

    如果这不起作用(或没有达到您想要的效果),请尝试:

    cur_axes = plt.gca()
    cur_axes.axes.get_xaxis().set_visible(False)
    cur_axes.axes.get_yaxis().set_visible(False)
    

    【讨论】:

    • 值得注意的是 plt.axis('off') 会同时移除 x 和 y 轴。
    • 非常感谢您的回答evtoh。但是,这两个建议都没有产生我想要的结果。如果我尝试上面的建议,只有从 0 到 1.0 的值保留在底部。但是,日期值 2010-01-01 到 2015-01-01 将被删除。您可能还有其他建议吗?
    • 哦,好的。无论如何,谢谢你,evtoh。其他人可能有建议吗?
    • 对不起,evtoh。实际上它确实有效。我只是把你的代码行放在错误的地方。在我上面的示例中,它必须将它直接放在 'fig = plt.figure(facecolor='white',frameon=True,figsize=(30, 30))' 行之后。非常感谢!
    • 很高兴能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 2016-08-25
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多