highfestiva / finplot

Performant and effortless finance plotting for Python
MIT License
933 stars 187 forks source link

add text above all candlesticks #348

Closed Vankeee closed 2 years ago

Vankeee commented 2 years ago

I have 1 month data to plot. I already drew the candlestick chart. Now I wanna add text above all candlesticks. Here ia a question, the data length is about 40000, if I add text above all bars, that would be so slow and stucked in the UI rendering. Could I use ax.plot instead of fplt.add_text? How to realise it?

figure 1:

finplot_plottext_2

code:

class CandleText():

    def __init__(self, data, col_name, color):
        self.data = data
        self.col_name = col_name
        self.color = color
        self.high = self.data['high']

    def add_candle_text_all(self):
        date_ls = self.data
        print('add_candle_text_all index')
        date_index = self.data.index
        # print(date_index[-1])
        for i in tqdm(range(len(self.data))):
            fplt.add_text((date_index[i],self.high[i]), f'{self.high[i]}', self.color)
highfestiva commented 2 years ago

Take a look at examples/bfx.py, which uses text above or below each candle. It also automatically reduces the level of detail when you zoom out.

Vankeee commented 2 years ago

@highfestiva Love you so much, you saved me. Here is another question, how to remove them all at once?


I found this funciton clear_items(drop_keys) in ScatterLabelItem class. I am confused about the "drop_keys", what is drop_keys? How can I get the drop_keys?

highfestiva commented 2 years ago

You want to remove that whole "plot" at once. For that you use ax.removeItem(labels).

Vankeee commented 2 years ago

Could I use clear_items(drop_keys)? The drop_keys is like {ts1:v1,ts2:v2,ts3:v3...}, right?

I dont know how to use ax.removeItem(labels) in your case, it seems like does not work for me cause I did it in a wrong way.

highfestiva commented 2 years ago

After line 55 in examples/bfx.py, you can add:

        ax.removeItem(plots[2])
        ax.removeItem(plots[3])

This removes the label plots from the chart. GL!

Vankeee commented 2 years ago

It seems does not work. Fine, I give up, I'll try it later. Thanks any way