Home > Blockchain >  Changes of the Y value of plt.text position doesn't affect to text position
Changes of the Y value of plt.text position doesn't affect to text position

Time:06-25

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,)

enter image description here

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

CodePudding user response:

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 enter image description here

  • Related