📜  在 Matplotlib 中隐藏轴、边界和空白

📅  最后修改于: 2022-05-13 01:54:25.303000             🧑  作者: Mango

在 Matplotlib 中隐藏轴、边界和空白

当我们使用 Matplotlib 绘制绘图时,也会绘制沿 x 轴和 y 轴的刻度和标签。为了绘制创意图形,很多时候我们隐藏了 x 轴和 y 轴。

如何在matplotlib图中隐藏轴?

matplotlib.pyplot.axis('off')命令用于隐藏 matplotlib 图中的轴(x 轴和 y 轴)。

例子:

让我们考虑下图,我们必须在其中隐藏轴。

Python3
# code
import numpy as np
import matplotlib.pyplot as plt
  
# Marks of RAM in different subjects out of 100.
x = ['Science', 'Maths', 'English', 'History', 'Geography']
y = [75, 85, 88, 78, 74]
  
plt.bar(x, y)
plt.xlabel("Subject")
plt.ylabel("Ram's marks out of 100")
plt.show()


Python3
# code
import numpy as np
import matplotlib.pyplot as plt
  
# Marks of RAM in different subjects out of 100.
x = ['Science', 'Maths', 'English', 'History', 'Geography']
y = [75, 85, 88, 78, 74]
  
plt.xlabel("Subject")
plt.ylabel("Ram's marks out of 100")
plt.bar(x, y)
plt.axis('off')  # command for hiding the axis.
  
plt.show()


Python3
# Hiding Y-axis label
import numpy as np
import matplotlib.pyplot as plt
  
# Marks of RAM in different subjects out of 100.
x = ['Science', 'Maths', 'English', 'History', 'Geography']
y = [75, 85, 88, 78, 74]
  
plt.bar(x, y)
plt.xlabel("Subject")
plt.ylabel("Ram's marks out of 100")
plt.yticks([])  # Command for hiding y-axis
  
plt.show()


Python3
# Hiding X-axis
import numpy as np
import matplotlib.pyplot as plt
  
# Marks of RAM in different subjects out of 100.
x = ['Science', 'Maths', 'English', 'History', 'Geography']
y = [75, 85, 88, 78, 74]
  
plt.bar(x, y)
plt.xlabel("Subject")
plt.ylabel("Ram's marks out of 100")
plt.xticks([])  # Command for hiding x-axis
  
plt.show()


Python3
# code
import numpy as np
import matplotlib.pyplot as plt
  
# Marks of RAM in different subjects out of 100.
x = ['Science', 'Maths', 'English', 'History', 'Geography']
y = [75, 85, 88, 78, 74]
  
fig = plt.bar(x, y)
plt.xlabel("Subject")
plt.ylabel("Ram's marks out of 100")
plt.axis('off')
# Command used for hiding whitespaces and border.
plt.savefig('image.png', bbox_inches='tight', pad_inches=0)
  
plt.show()


输出:

例子:

隐藏上图中的轴。

蟒蛇3

# code
import numpy as np
import matplotlib.pyplot as plt
  
# Marks of RAM in different subjects out of 100.
x = ['Science', 'Maths', 'English', 'History', 'Geography']
y = [75, 85, 88, 78, 74]
  
plt.xlabel("Subject")
plt.ylabel("Ram's marks out of 100")
plt.bar(x, y)
plt.axis('off')  # command for hiding the axis.
  
plt.show()

输出:

如果我们只想关闭 X 轴或 Y 轴,我们可以使用  plt.xticks() 或 plt.yticks()方法。

例子:

隐藏 Y 轴

蟒蛇3

# Hiding Y-axis label
import numpy as np
import matplotlib.pyplot as plt
  
# Marks of RAM in different subjects out of 100.
x = ['Science', 'Maths', 'English', 'History', 'Geography']
y = [75, 85, 88, 78, 74]
  
plt.bar(x, y)
plt.xlabel("Subject")
plt.ylabel("Ram's marks out of 100")
plt.yticks([])  # Command for hiding y-axis
  
plt.show()

输出:

例子:

隐藏 X 轴

蟒蛇3

# Hiding X-axis
import numpy as np
import matplotlib.pyplot as plt
  
# Marks of RAM in different subjects out of 100.
x = ['Science', 'Maths', 'English', 'History', 'Geography']
y = [75, 85, 88, 78, 74]
  
plt.bar(x, y)
plt.xlabel("Subject")
plt.ylabel("Ram's marks out of 100")
plt.xticks([])  # Command for hiding x-axis
  
plt.show()

输出:

隐藏 Matplotlib 图形中的空白和边框

当我们使用plt.axis('off')命令时,它会隐藏轴,但在保存图像时我们会在图像边框周围获得空白。要删除/隐藏边框周围的空白,我们可以在savefig()方法中设置bbox_inches='tight'

同样,当我们在savefig()方法中设置pad_inches = 0时删除图像周围的白色边框。

例子:

蟒蛇3

# code
import numpy as np
import matplotlib.pyplot as plt
  
# Marks of RAM in different subjects out of 100.
x = ['Science', 'Maths', 'English', 'History', 'Geography']
y = [75, 85, 88, 78, 74]
  
fig = plt.bar(x, y)
plt.xlabel("Subject")
plt.ylabel("Ram's marks out of 100")
plt.axis('off')
# Command used for hiding whitespaces and border.
plt.savefig('image.png', bbox_inches='tight', pad_inches=0)
  
plt.show()

输出:

注意:如果您注意到当我们使用plt.axis('off')时,它会自动隐藏轴、空格和边框。