另请参见下面的更新。
通过使用 plt.text
用来注释我的图表,我将其放在图:
plt.text(x=0, y=5,
s='Джерело: openbudget.gov.ua', ha='left', fontsize=9, alpha=0.7,)
当我更改 x
位置值时,它可以正常工作,但是更改为 y
值根本不会影响文本位置 - 聊天的底部边缘和注释文本(箭头)之间的空间不会更改。
问题是 - 为什么在给定图表的情况下, y
文本位置值的更改不会影响实际的文本位置(我以前有注释图表,并且效果很好)?
dataFrame源数据:
{"year":{"0":2021,"1":2021,"2":2021,"3":2021,"4":2021,"5":2021,"6":2021,"7":2021,"8":2021,"9":2021,"10":2021,"11":2021,"12":2022,"13":2022,"14":2022,"15":2022},"month":{"0":1,"1":2,"2":3,"3":4,"4":5,"5":6,"6":7,"7":8,"8":9,"9":10,"10":11,"11":12,"12":1,"13":2,"14":3,"15":4},"zn":{"0":8403533.0,"1":13708653.3,"2":12754794.0,"3":13003063.2,"4":13799415.0,"5":24882118.4,"6":7330157.5,"7":7360882.7,"8":13355428.8,"9":12990686.3,"10":13236689.3,"11":17756139.6,"12":11600703.4,"13":13643288.9,"14":12591057.9,"15":11387693.1},"mon":{"0":"січ","1":"лют","2":"бер","3":"кві","4":"тра","5":"чер","6":"лип","7":"сер","8":"вер","9":"жов","10":"лис","11":"гру","12":"січ","13":"лют","14":"бер","15":"кві"}}
图表代码源
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
plt.style.use('seaborn')
palette = {2021: '#55A868', 2022: '#8172B2'}
fontdict = {'size': 13,}
fig, ax = plt.subplots(nrows=1, ncols=1)
ax = sns.lineplot(
x='mon', y='zn',
data=data,
hue=data['year'], palette=palette,
ci=None, linewidth=3)
ax.legend().set_title("Рік", prop=fontdict)
yk = np.array([x/1000000 for x in list(ax.get_yticks())])
ax.set_yticks(ax.get_yticks())
ax.set_yticklabels(labels=yk)
ax.set_ylabel('млрд. грн.', fontdict={'size': 11})
ax.set_xlabel(None)
ax.set_title('Витрати місцевих бюджетів на оплату праці\n'
"(дошкільна, загальна середня освіта, бюджети міст, сіл, селищ)",
fontdict={'size': 15,}
)
plt.text(x=0,
y=60, # changes to this parameter has no effect
s='Джерело: openbudget.gov.ua', ha='left', fontsize=9, alpha=0.7,)
plt.show()
更新1
@Redox在评论中注意到我,该描述的行为无法复制,文本的位置与y值的变化相应地变化。
反过来,我尝试将代码与Ipython一起运行,并发现所需的文本注释根本不会出现 - 我只有图表,而没有任何注释。
我的库版本是:
matplotlib==3.5.2
matplotlib-inline==0.1.3
pandas==1.4.2
seaborn==0.11.2
See also updates below.
By using plt.text
to annotate my chart I would place it below the figure:
plt.text(x=0, y=5,
s='Джерело: openbudget.gov.ua', ha='left', fontsize=9, alpha=0.7,)

When I change the x
position value, it works correctly, but changes to y
value does not affect to text position at all -- space between bottom edge of the chat and annotation text (arrow) doesn't change.
The question is -- why changes of the y
text position value does not affect to actual text position in case of given chart (I had annotate charts this way before and it had worked fine)?
Dataframe source data:
{"year":{"0":2021,"1":2021,"2":2021,"3":2021,"4":2021,"5":2021,"6":2021,"7":2021,"8":2021,"9":2021,"10":2021,"11":2021,"12":2022,"13":2022,"14":2022,"15":2022},"month":{"0":1,"1":2,"2":3,"3":4,"4":5,"5":6,"6":7,"7":8,"8":9,"9":10,"10":11,"11":12,"12":1,"13":2,"14":3,"15":4},"zn":{"0":8403533.0,"1":13708653.3,"2":12754794.0,"3":13003063.2,"4":13799415.0,"5":24882118.4,"6":7330157.5,"7":7360882.7,"8":13355428.8,"9":12990686.3,"10":13236689.3,"11":17756139.6,"12":11600703.4,"13":13643288.9,"14":12591057.9,"15":11387693.1},"mon":{"0":"січ","1":"лют","2":"бер","3":"кві","4":"тра","5":"чер","6":"лип","7":"сер","8":"вер","9":"жов","10":"лис","11":"гру","12":"січ","13":"лют","14":"бер","15":"кві"}}
Chart code source
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
plt.style.use('seaborn')
palette = {2021: '#55A868', 2022: '#8172B2'}
fontdict = {'size': 13,}
fig, ax = plt.subplots(nrows=1, ncols=1)
ax = sns.lineplot(
x='mon', y='zn',
data=data,
hue=data['year'], palette=palette,
ci=None, linewidth=3)
ax.legend().set_title("Рік", prop=fontdict)
yk = np.array([x/1000000 for x in list(ax.get_yticks())])
ax.set_yticks(ax.get_yticks())
ax.set_yticklabels(labels=yk)
ax.set_ylabel('млрд. грн.', fontdict={'size': 11})
ax.set_xlabel(None)
ax.set_title('Витрати місцевих бюджетів на оплату праці\n'
"(дошкільна, загальна середня освіта, бюджети міст, сіл, селищ)",
fontdict={'size': 15,}
)
plt.text(x=0,
y=60, # changes to this parameter has no effect
s='Джерело: openbudget.gov.ua', ha='left', fontsize=9, alpha=0.7,)
plt.show()
Update 1
@Redox noticed me in the comment, that described behaviour cannot be reproduced and position of the text changes accordingly to changes of Y value.
In turn I try to run my code within clear VM with Ipython and found that desired text annotation would not appear at all -- I got chart only, without any annotation.
My libraries versions is:
matplotlib==3.5.2
matplotlib-inline==0.1.3
pandas==1.4.2
seaborn==0.11.2
发布评论
评论(1)
可以使用图形的坐标系统灵活地指定字符串作为注释。类型包括数据,轴和图形区域。有关更多详细信息。
如果坐标是在轴上指定的:
Adding a string as an annotation can be specified flexibly by using the coordinate system of the graph. Types include data, axis, and graph area. See here for more details.
If the coordinates are specified in axes:.