Python plotly go.bar textposition is not working

Viewed 51

I want to display text value on the group bar plot and the gross profit and net profit on line plot from the code below but it doesn't work.

I have added textposition to go.bar but it isn't working. I don't understand why it is not working.

enter image description here

create_visual is a function create visualization of the all stock profit loss in filter-HK-CODE2.csv and it creates the html file and the png file.

from utils import *
import plotly.graph_objects as go
import plotly.io as pio
from plotly.subplots import make_subplots

# y folder

df_stock = pd.read_csv('filter-HK-CODE2.csv')



def create_visual(filename):
    
    createDir_if_not_exist('out/y/img')
    createDir_if_not_exist('out/y/interactive')
    
    print('read file: ' ,filename)
    
    df = pd.read_csv('data/y/'+ filename)
    
    df.replace('-', '0', inplace=True)
    df['Total Turnover'] = pd.to_numeric(df['Total Turnover'])
    df['Net Profit'] = pd.to_numeric(df['Net Profit'])
    try:
        df['Gross Profit'] = pd.to_numeric(df['Gross Profit'])
    except:
        print('No GP')
    stockcode = filename[0:5]
    
    #print('stockcode: ', stockcode)

    #print(df['Total Turnover'])
    #print(df['Net Profit'])
    
    fig = make_subplots(specs=[[{"secondary_y": True}]])

    fig.add_trace(go.Bar(name='Revenue', x=df['Closing Date'], y=df['Total Turnover'],base = 0,textposition="inside",),secondary_y=False)
    fig.add_trace(go.Bar(name='Net Profit', x=df['Closing Date'], y=df['Net Profit'],base = 0,textposition="inside",),secondary_y=False)
    
    try:
        fig.add_trace(go.Scatter(x=df['Closing Date'], y=df['Gross Profit']*100/df['Total Turnover'], name="GPM%",textposition="bottom center"),secondary_y=True)
    except:
        print('No GP')
        
    fig.add_trace(go.Scatter(x=df['Closing Date'], y=df['Net Profit']*100/df['Total Turnover'], name="NPM%",textposition="bottom center"),secondary_y=True)
    
    
    fig.update_layout(barmode='group',template="seaborn",
        title_text=str(df_stock[df_stock['StockCode'] == int(stockcode)]['Name of Securities'].values[0]) + '    ' + stockcode +'.HK',
        xaxis_title="date",
        title_font_color="red",
        yaxis_title=df['Unit'].values[0] +'    '+ df['Currency'].values[0],
        font=dict(
            size=18,
        ))
    #fig.show()
    pio.write_image(fig,'out/y/img/'+str(stockcode)+'.png', width=1480, height=780,format ='png')
    fig.write_html('out/y/interactive/'+str(stockcode)+'.html')
    
if __name__ == '__main__':
    
    count = 0 
    '''
    for file in os.listdir('data/y'):
        print('file No : ',count)
        create_visual(file)
        count += 1 
    '''
    create_visual('00001PL-Y.csv')
0 Answers
Related