简体   繁体   中英

Adding Text to Seaborn Graph when x-axis is dates and y-axis is logs

I have a lineplot about stocks and my x-axis contains dates and y-axis contains some values on log.

Here is little cut of my data:

     DATE          STOCK CODE  HIGHEST VALUE
0    2019-01-02    GENTS.E     1.80
882  2019-01-02    IHLGM.E     1.09
1197 2019-01-02    ISGYO.E     4.60
126  2019-01-02    ALBRK.E     3.80
504  2019-01-02    DOHOL.E     2.87

Here is my seaborn code:

sns.set(font_scale=1.5)
sns.set_theme(style="ticks", palette="bright")
ax= sns.relplot(
    data=part1_19H, 
    x="DATE", y="HIGHEST PRICE", hue="STOCK CODE", 
    height=10, aspect=2, 
    kind="line",
    legend=True,
    marker="o"
).set(title="First 20 Stocks of Q1 2019",  ylabel="Highest Price", xlabel=None)
plt.yscale('log')
plt.xticks(rotation=45)

Here is my graph:

在此处输入图像描述

Anyway, when I try add some text with using plt.text() function everytime i am getting error because of coordination problems.

Like:

plt.text("2019-03-29", 4.10, 'Some text')

Failed to convert value(s) to axis units: '2019-03-29'

Also, probably I'll get error on y axis too because in my graph y axis is log styled but i'm trying to coordinate with non-log style.

So, how can I coordinate my text on graph with date and non-log coordinates?

I figured out.

We need to give date as a datetime because seaborn tooks like that so;

from datetime import datetime as datetime

plt.text(datetime(2019,3,29), 1.2,"some text")

This will be take our datetime like 2019-03-29 and it will work. Also, when we gave original value of z seaborn locates it to logarithmic coordinate, so there is no problem about z coordination.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM