Python中plot()画图添加数据标签,显示点对应的数值

创建子图的好处:

  1. 每个子图可以单独操作,想画啥就画啥
  2. 各个子图之间可以共享坐标轴,便于同一量纲去比较数值的大小
fig,((ax1,ax2,ax3,ax4,ax5,ax6)) = plt.subplots(1,6,figsize=(22,4),sharex=True,sharey=True)
##设定子图间距 , left < right, top > bottom, 数字表示窗口大小的比例(如下则子图间距为窗口大小的1%) 
plt.subplots_adjust(left=0, top= 0.96, right = 0.96, bottom = 0.04, wspace = 0.05, hspace = 0.1)

x1 = np.array(result.loc[result['dict_value']=='渠道1']['clue_time'])
y1 = np.array(result.loc[result['dict_value']=='渠道1']['only_mobiles'])
ax1.plot(x1,y1,'o-')

for x,y in zip(x1,y1):
    ax1.text(x,y,'%.0f' % y,fontdict={'fontsize':14})
    
ax1.set_title("渠道1",fontdict={'fontsize':16})

plt.text()可以实现在画图中增加数据标签:

  • x,y 是一组标量,可为数组对象,不可为Serise对象
  • x,y 提供了需要展示的文本对应的位置,即通过x,y坐标轴定位
  • s参数:是一个string对象,是需要展示在图上的内容

Signature: ax2.text(x, y, s, fontdict=None, withdash=, **kwargs)
Docstring:
Add text to the axes.
Add the text s to the axes at location x, y in data coordinates.
Parameters
x, y : scalars
The position to place the text. By default, this is in data
coordinates. The coordinate system can be changed using the
transform parameter.
s : str
The text.
fontdict : dictionary, optional, default: None
A dictionary to override the default text properties. If fontdict
is None, the defaults are determined by your rc parameters.
withdash : boolean, optional, default: False
Creates a ~matplotlib.text.TextWithDash instance instead of a
~matplotlib.text.Text instance.
Returns
text : .Text
The created .Text instance.
Other Parameter
**kwargs : ~matplotlib.text.Text properties.
Other miscellaneous text parameters.
Examples
Individual keyword arguments can be used to override any given
parameter::

text(x, y, s, fontsize=12)
The default transform specifies that text is in data coords,
alternatively, you can specify text in axis coords ((0, 0) is
lower-left and (1, 1) is upper-right). The example below places
text in the center of the axes::

text(0.5, 0.5, ‘matplotlib’, horizontalalignment=‘center’,
… verticalalignment=‘center’, transform=ax.transAxes)
You can put a rectangular box around the text instance (e.g., to
set a background color) by using the keyword bbox. bbox is
a dictionary of ~matplotlib.patches.Rectangle
properties. For example::

text(x, y, s, bbox=dict(facecolor=‘red’, alpha=0.5))
File: /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/axes/_axes.py
Type: method

Python中plot()画图添加数据标签,显示点对应的数值_第1张图片

关注微信公众号:“数据分析师之家”
Python中plot()画图添加数据标签,显示点对应的数值_第2张图片

你可能感兴趣的:(Python神器)