【问题标题】:remove white background from the matplotlib graph从 matplotlib 图中删除白色背景
【发布时间】:2020-07-26 22:43:00
【问题描述】:

我正在尝试绘制透明图,但背景中出现了一些框,并且外框未出现。如何从绘图中删除白色背景并使其透明并放置外框?

from matplotlib import pyplot
pyplot.scatter(Neural_Net, y_test)
pyplot.xlabel('Actual', fontsize=15)
pyplot.ylabel('Predicted', fontsize=15)
pyplot.show()

【问题讨论】:

  • 要保存图片吗?
  • 只是为了形象化。首先,必须在没有白色背景的情况下将其可视化。
  • 在你的代码某处,你使用了 plt.style.use()

标签: python numpy matplotlib graph-visualization


【解决方案1】:

默认matplotlibstyleclassic

你可以看到可用的matplotlib styles

import matplotlib.pyplot as plt

#To list all available styles, use:

print(plt.style.available)
['Solarize_Light2', '_classic_test_patch', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10']

将while背景改回默认值:

plt.style.use('classic')

希望对您有所帮助!

【讨论】:

  • 是否可以改为透明背景而不是白色?
  • 当然,在 plt.savefig() 中设置 transparent=True