为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

泛舟湖上清波郎朗 2023-01-04 13:29:54
我尝试使用 plt.axis('off') 删除轴,但是当我添加此代码时,它会将所有图形变为透明,代码运行没有问题但轴没有 plt.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)    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 hereplt.axis('off') 的结果 enter image description here
查看完整描述

2 回答

?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

尝试将可见性设置为 false 而不是使用“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')


    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")


查看完整回答
反对 回复 2023-01-04
?
慕虎7371278

TA贡献1802条经验 获得超4个赞

我刚刚在https://repl.it/repls/DentalRunnyBlockchain#test.png上试过你的代码

它工作正常所以 axis off 做你想实现的


查看完整回答
反对 回复 2023-01-04
?
当年话下

TA贡献1890条经验 获得超9个赞

看起来它与您对“轴”的定义有关。如果你绕过它它会起作用:


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)


    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)


查看完整回答
反对 回复 2023-01-04
  • 2 回答
  • 0 关注
  • 150 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信