Matplotlib.pyplot-停用图中的轴./图形的轴与子图的轴重叠 [英] Matplotlib.pyplot - Deactivate axes in figure. /Axis of figure overlap with axes of subplot

查看:115
本文介绍了Matplotlib.pyplot-停用图中的轴./图形的轴与子图的轴重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

%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()

我的问题是,在由代码生成的绘图中,该图形位于图的轴上方,并且各子图似乎重叠(请参见图片中的红色矩形).我试图关闭图中的轴.但是,我无法弄清楚.

My problem is that in the plot generated by code above the axis of the figure and the sublpots seem to overlap (see red rectangles in picture). I tried to turn off the axes in the figure. However, I was not able to figure it out.

感谢您的帮助.谢谢.

代码/问题的输出:

Output of Code / Problem:

推荐答案

如果要完全删除轴,则应尝试:

If you want to remove the axes completely, you should try:

plt.axis('off')

如果这行不通(或者不执行您想要的操作),请尝试:

If that doesn't work (or doesn't do what you want it to), try:

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

这篇关于Matplotlib.pyplot-停用图中的轴./图形的轴与子图的轴重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆