如何在 Matplotlib 中绘制等轴方图?
在本文中,我们将讨论如何使用matplotlib模块来说明等轴的正方形图。我们可以使用matplotlib.axes.Axes.set_aspect()和matplotlib.pyplot.axis()方法描绘方形图。
使用set_aspect()方法
Syntax: matplotlib.axes.Axes.set_aspect()
Parameters:
- aspect : This parameter accepts the following value {‘auto’, ‘equal’} or num.
- adjustable : This defines which parameter will be adjusted to meet the required aspect.
- anchor : This parameter is used to define where the Axes will be drawn if there is extra space due to aspect constraints.
- share: This parameter is used to apply the settings to all shared Axes.
示例 1:
我们可以使用matplotlib.axes.Axes.set_aspect()方法生成一个正方形图。我们将分配equal作为方面参数,将box分配为可调整参数。
Python3
# import required module
# import required modules
import numpy as np
import matplotlib.pyplot as plt
# adjust coordinates
x = y = [i for i in range(0, 6)]
# depict illustartion
fig = plt.figure()
ax = fig.add_subplot()
plt.plot(x, y)
# square plot
ax.set_aspect('equal', adjustable='box')
plt.show()
Python3
# import required modules
import numpy as np
import matplotlib.pyplot as plt
# adjust coordinates
x = y = [i for i in range(0, 6)]
# depict illustartion
fig = plt.figure()
ax = fig.add_subplot()
plt.plot(x, y)
# square plot
ax.set_aspect(1.0/ax.get_data_ratio(), adjustable='box')
plt.show()
Python3
# import required module
# import required modules
import numpy as np
import matplotlib.pyplot as plt
# adjust coordinates
x=y=[i for i in range(0,6)]
# plot coordinates
plt.plot(x,y)
# square plot
plt.axis('square')
# depict illustration
plt.show()
Python3
# importing module
import matplotlib.pyplot as plt
# assigning x and y coordinates
x = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
y = []
for i in range(len(x)):
y.append(max(0, x[i]))
# depicting the visualization
plt.plot(x, y, color='green')
plt.xlabel('X')
plt.ylabel('Y')
# square plot
plt.axis('square')
# displaying the title
plt.title('ReLU Function')
输出:
示例 2 :
当两个战斧的范围设置为等效时,上面的示例可能会产生一个方形图。为了在整体情况下生成方形图,我们需要使用伴随的顺序物理设置视点比例:
axes.set_aspect(1./axes.get_data_ratio())
蟒蛇3
# import required modules
import numpy as np
import matplotlib.pyplot as plt
# adjust coordinates
x = y = [i for i in range(0, 6)]
# depict illustartion
fig = plt.figure()
ax = fig.add_subplot()
plt.plot(x, y)
# square plot
ax.set_aspect(1.0/ax.get_data_ratio(), adjustable='box')
plt.show()
输出:
使用axis()方法
Syntax: matplotlib.pyplot.axis()
Parameters:
- xmin, xmax, ymin, ymax:These parameters can be used to set the axis limits on the graph.
- emit:Its a bool value used to notify observers of the axis limit change.
示例 1:
在这个例子中,我们将square作为参数传递给matplotlib.pyplot.axis() ,它说明了一个正方形图。
蟒蛇3
# import required module
# import required modules
import numpy as np
import matplotlib.pyplot as plt
# adjust coordinates
x=y=[i for i in range(0,6)]
# plot coordinates
plt.plot(x,y)
# square plot
plt.axis('square')
# depict illustration
plt.show()
输出:
示例 2:
这是使用axis()方法说明方形图的另一个示例。
蟒蛇3
# importing module
import matplotlib.pyplot as plt
# assigning x and y coordinates
x = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
y = []
for i in range(len(x)):
y.append(max(0, x[i]))
# depicting the visualization
plt.plot(x, y, color='green')
plt.xlabel('X')
plt.ylabel('Y')
# square plot
plt.axis('square')
# displaying the title
plt.title('ReLU Function')
输出: