Matplotlib-Axes轴类

Matplotlib Axes轴类

Matplotlib Axes轴类

  Matplotlib Axes对象是具有数据空间的图像区域。一个给定的图形可以包含多个 Axes,但是一个给定的 Axes 对象只能在一个 Figure 中。Axes 包含两个(在 3D 情况下为三个)Axis 对象。Axes 类及其成员函数是使用 OO 接口的主要入口点。

Matplotlib Axes轴类
Matplotlib Axes轴类

  通过调用 add_axes() 方法将 Axes 对象添加到图中。它返回轴对象并在位置 rect [left, bottom, width, height] 添加一个轴,其中所有数量都是图形宽度和高度的分数。

  推荐:Matplotlib Figure Class图类

Parameter范围

  Matplotlib Axes的轴是图形中绘制数据的矩形区域。它具有许多可用于自定义绘图外观的属性,例如标题、轴标签、刻度线和网格。

  要创建一个轴,您可以使用该plt.subplots()函数。此函数采用多个参数,包括 Axes 的行数和列数、Axes 在图形中的位置以及图形大小

  以下是 Axes 类的参数 –

  rect – [left, bottom, width, height] 数量的 4 长度序列。

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

  轴类的以下成员函数添加不同的元素来绘制 –

  推荐:通过Python使用ChatGPT

Legend

  axes 类的legend ()方法为绘图添加了一个图例。它需要三个参数 –

ax.legend(handles, labels, loc)

  其中 labels 是一系列字符串并处理一系列 Line2D 或 Patch 实例。loc 可以是指定图例位置的字符串或整数。

  位置字符串位置代码

Location stringLocation code
Best0
upper right1
upper left2
lower left3
lower right4
Right5
Center left6
Center right7
lower center8
upper center9
Center10

  推荐:用于数据可视化5个Python包

axes.plot()

  这是轴类的基本方法,它将一个数组的值与另一个数组的值绘制成线条或标记。plot() 方法可以有一个可选的格式字符串参数来指定线条和标记的颜色、样式和大小。

  Color codes

CharacterColor
‘b’Blue
‘g’Green
‘r’Red
‘b’Blue
‘c’Cyan
‘m’Magenta
‘y’Yellow
‘k’Black
‘b’Blue
‘w’White

  Marker codes

CharacterDescription
‘.’Point marker
‘o’Circle marker
‘x’X marker
‘D’Diamond marker
‘H’Hexagon marker
‘s’Square marker
‘+’Plus marker

  Line styles

CharacterDescription
‘-‘Solid line
‘—‘Dashed line
‘-.’Dash-dot line
‘:’Dotted line
‘H’Hexagon marker

  推荐:将日志记录添加到Python库

  以下示例以线图的形式显示了电视和智能手机的广告费用和销售数字。代表电视的线是带有黄色和方形标记的实线,而智能手机线是带有绿色和圆形标记的虚线。

import matplotlib.pyplot as plt
y = [1, 4, 9, 16, 25,36,49, 64]
x1 = [1, 16, 30, 42,55, 68, 77,88]
x2 = [1,6,12,18,28, 40, 52, 65]
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
l1 = ax.plot(x1,y,'ys-') # solid line with yellow colour and square marker
l2 = ax.plot(x2,y,'go--') # dash line with green colour and circle marker
ax.legend(labels = ('tv', 'Smartphone'), loc = 'lower right') # legend placed at lower right
ax.set_title("Advertisement effect on sales")
ax.set_xlabel('medium')
ax.set_ylabel('sales')
plt.show()
Matplotlib Axes轴类
Matplotlib Axes轴类

  推荐:Matplotlib教程

  推荐:Python中的import语句

给文章评分

晓得博客,版权所有丨如未注明,均为原创
晓得博客 » Matplotlib Axes轴类

转载请保留链接:https://www.pythonthree.com/matplotlib-axes/

Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
滚动至顶部