首页 > 解决方案 > 去掉图中的次要框

问题描述

需要从 pyplot 中删除辅助框架(相信是从将图例添加到绘图中)。

首先,我已经阅读了其他关于类似问题的问答。Frameon=False 或 frameon=0 不起作用,我最近重新下载了 matplotlib。其他答案并没有完全解决我的具体问题。我还尝试摆脱 plt.legend(),它摆脱了框架和图例。所以然后我尝试了 plt.legend(frameon=False) 并没有改变。

#Arbitrary runnable code
from getpass import getpass
import numpy as np
import pylab as plt
import matplotlib as mpl
import healpy as hp
from matplotlib.backends.backend_pdf import PdfPages


with PdfPages('KimmiesHistogram.pdf') as pdf:

    mpl.rcParams['font.family']='serif'
    mpl.rcParams['font.size']=12

    fig = plt.figure(figsize= (12,12), frameon= False)

    plt.xlabel('MJD: Range')

    plt.title("MJD Ranges by Filter")

    y= [1, 2,3,45,6,4,34,76,4,34,65,23,34,3,3,9,5,5]
    y2=[1, 2,3,45,6,4,34,76,4,34,65,23,34,3,3,9,5,5]
    ax1 = fig.add_subplot(132)
    ax1.set_xscale('log')
    ax1.set_yscale('linear')

    plt.hist([y,y2], bins=10, histtype='step', cumulative=1)

    plt.show()

谢谢!

标签: pythonmatplotliblegend

解决方案


它不清楚你的真正意思,但我相信你看到了 3 个子图的边界。如果不是这种情况,请澄清您的问题。当我运行你的代码时,我得到了这个:

在此处输入图像描述

如果要删除该外框,则只需指定您只想添加 1 个子图,而不是使用 3 个子图的中间ax=fig.add_subplot(111)。那就是你的问题所在。但老实说,您不需要在示例中添加图形轴来绘制您想要的数据。

#Arbitrary runnable code
from getpass import getpass
import numpy as np
import pylab as plt
import matplotlib as mpl
from matplotlib.backends.backend_pdf import PdfPages


with PdfPages('KimmiesHistogram.pdf') as pdf:

    mpl.rcParams['font.family']='serif'
    mpl.rcParams['font.size']=12

    fig = plt.figure(figsize= (12,12), frameon= False)

    plt.xlabel('MJD: Range')

    plt.title("MJD Ranges by Filter")

    y= [1, 2,3,45,6,4,34,76,4,34,65,23,34,3,3,9,5,5]
    y2=[1, 2,3,45,6,4,34,76,4,34,65,23,34,3,3,9,5,5]
    ax1 = fig.add_subplot(111)
    ax1.set_xscale('log')
    ax1.set_yscale('linear')

    plt.hist([y,y2], bins=10, histtype='step', cumulative=1)

    plt.show()

在此处输入图像描述


推荐阅读