Matplotlib:尝试删除轴,但 plt.axis('off') 将所有图形变为透明,为什么?

我尝试使用 plt.axis('off') 删除轴,但是当我添加此代码时,它会将所有图形变为透明,代码运行没有问题但轴没有 plt.axis('off')。


这是代码


import matplotlib as mpl

import matplotlib.pyplot as plt


import numpy as np


def plot_net(line, dot, name = "test.png"):

    mpl.rcParams['agg.path.chunksize'] = 10000


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

    axiss = fig.add_axes([0, 0, 1, 1])


    axiss.set_aspect('equal', adjustable = 'datalim')

    plt.axis('off')


    axiss.scatter(dot[:, 0], dot[:, 1], color = 'red', s = 4)

    axiss.plot(line[:, 0], line[:, 1], 'r.', ls = '-', color = '#0063ba', markersize = 2)


    plt.savefig(name, bbox_inches = 'tight', pad_inches = 0, dpi = 200)

    plt.close()


a = np.random.rand(4, 2)

b = np.random.rand(4, 2)


plot_net(a, b, name = "test2.png")

没有 plt.axis('off') 的结果 enter image description here

plt.axis('off') 的结果 enter image description here


泛舟湖上清波郎朗
浏览 214回答 2
2回答

陪伴而非守候

尝试将可见性设置为 false 而不是使用“off”:import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as npdef plot_net(line, dot, name = "test.png"):    mpl.rcParams['agg.path.chunksize'] = 10000    fig = plt.figure(figsize = (5, 5), frameon = False)    axiss = fig.add_axes([0, 0, 1, 1])    axiss.set_aspect('equal', adjustable = 'datalim')    axiss.get_yaxis().set_visible(False)    axiss.get_xaxis().set_visible(False)    axiss.scatter(dot[:, 0], dot[:, 1], color = 'red', s = 4)    axiss.plot(line[:, 0], line[:, 1], 'r.', ls = '-', color = '#0063ba', markersize = 2)    plt.savefig(name, bbox_inches = 'tight', pad_inches = 0, dpi = 200)    plt.close()a = np.random.rand(4, 2)b = np.random.rand(4, 2)plot_net(a, b, name = "c:/temp/test2.png")

慕虎7371278

我刚刚在https://repl.it/repls/DentalRunnyBlockchain#test.png上试过你的代码它工作正常所以 axis off 做你想实现的

当年话下

看起来它与您对“轴”的定义有关。如果你绕过它它会起作用:import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as npdef plot_net(line, dot, name = "test.png"):    mpl.rcParams['agg.path.chunksize'] = 10000    fig = plt.figure(figsize = (5, 5), frameon = False)    plt.axes().set_aspect('equal', adjustable = 'datalim')    plt.scatter(dot[:, 0], dot[:, 1], color = 'red', s = 4)    plt.plot(line[:, 0], line[:, 1], 'r.', ls = '-', color = '#0063ba', markersize = 2)    plt.axis('off')    plt.savefig(name, bbox_inches = 'tight', pad_inches = 0, dpi = 200)    plt.close()a = np.random.rand(4, 2)b = np.random.rand(4, 2)plot_net(a, b)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python