Home > front end >  How to add currency symbol to marker label plotly python
How to add currency symbol to marker label plotly python

Time:08-30

I need to add currency symbol to marker label to a plotly graphic_objects chart in python. For example "€0.15" How do I do that?

from tkinter import *
from tkinter import ttk
import plotly.graph_objects as go

window = Tk()

monthly_comparison_result=[["","","","","","","","","","","","",],["","","","","","","","","","","","",],["","","","","","","","","","","","",],["","","","","","","","","","","","",]]
for i in range(4):
   for j in range(12): monthly_comparison_result[i][j] = dividend_monthly_comparison[j][i]
for i in range(12): monthly_comparison_result[1][i] = monthly_comparison_result[1][i][1:].replace(",", ".")
for i in range(12): monthly_comparison_result[2][i] = monthly_comparison_result[2][i][1:].replace(",", ".")
for i in range(12): monthly_comparison_result[3][i] = monthly_comparison_result[3][i][1:].replace(",", ".")

annual_bar_chart = go.Figure()
annual_bar_chart.add_trace(go.Bar(x=monthly_comparison_result[0], y=monthly_comparison_result[1], name='2021', text=monthly_comparison_result[1], textposition="outside", textfont=dict(family="sans serif",size=18,color="black")))
annual_bar_chart.add_trace(go.Bar(x=monthly_comparison_result[0], y=monthly_comparison_result[2], name='2022', text=monthly_comparison_result[2], textposition="outside", textfont=dict(family="sans serif",size=18,color="black")))
annual_bar_chart.add_trace(go.Bar(x=monthly_comparison_result[0], y=monthly_comparison_result[3], name='2023', text=monthly_comparison_result[3], textposition="outside", textfont=dict(family="sans serif",size=18,color="black")))
annual_bar_chart.update_layout(barmode='group', xaxis_tickangle=-45, autotypenumbers='convert types', margin=dict(l=00, r=00, b=0, t=0, pad=0), title_font_size=40, paper_bgcolor='#f0f0f0', plot_bgcolor="#f0f0f0", autosize=True, showlegend=True, width=1830, height=500, yaxis_tickprefix='€', yaxis_tickformat=',.')
annual_bar_chart.show()

The variable 'dividend_monthly_comparison' contains:

[['Ianuarie', '€0,03', '€0,49', ''], ['Februarie', '€0,11', '€1,01', ''], ['Martie', '€0,09', '€0,56', ''], ['Aprilie', '€0,08', '€0,85', ''], ['Mai', '€0,47', '€0,85', ''], ['Iunie', '€0,18', '€0,88', ''], ['Iulie', '€0,34', '€0,99', ''], ['August', '€0,82', '€1,05', ''], ['Septembrie', '€0,21', '', ''], ['Octombrie', '€0,62', '', ''], ['Noiembrie', '€1,09', '', ''], ['Decembrie', '€0,50', '', ''], ['Total', '€4,55', '€6,68', '€0,00']]

The variable 'monthly_comparison_result' is the transposed array

CodePudding user response:

Simply construct the text as you want it. Have used a list comprehension to achieve this. Have done a minor refactor of your code to reduce copy paste effect to create traces.

import plotly.graph_objects as go

dividend_monthly_comparison = [['Ianuarie', '€0,03', '€0,49', ''], ['Februarie', '€0,11', '€1,01', ''], ['Martie', '€0,09', '€0,56', ''], ['Aprilie', '€0,08', '€0,85', ''], ['Mai', '€0,47', '€0,85', ''], ['Iunie', '€0,18', '€0,88', ''], ['Iulie', '€0,34', '€0,99', ''], ['August', '€0,82', '€1,05', ''], ['Septembrie', '€0,21', '', ''], ['Octombrie', '€0,62', '', ''], ['Noiembrie', '€1,09', '', ''], ['Decembrie', '€0,50', '', ''], ['Total', '€4,55', '€6,68', '€0,00']]  # fmt: skip
monthly_comparison_result=[["","","","","","","","","","","","",],["","","","","","","","","","","","",],["","","","","","","","","","","","",],["","","","","","","","","","","","",]]  # fmt: skip
for i in range(4):
    for j in range(12):
        monthly_comparison_result[i][j] = dividend_monthly_comparison[j][i]
for i in range(12):
    monthly_comparison_result[1][i] = monthly_comparison_result[1][i][1:].replace(
        ",", "."
    )
for i in range(12):
    monthly_comparison_result[2][i] = monthly_comparison_result[2][i][1:].replace(
        ",", "."
    )
for i in range(12):
    monthly_comparison_result[3][i] = monthly_comparison_result[3][i][1:].replace(
        ",", "."
    )

annual_bar_chart = go.Figure()
for i, mcr in enumerate(monthly_comparison_result[1:]):
    annual_bar_chart.add_trace(
        go.Bar(
            x=monthly_comparison_result[0],
            y=mcr,
            name=f"{2021 i}",
            text=["€"   v if v != "" else "" for v in mcr],
            textposition="outside",
            textfont=dict(family="sans serif", size=18, color="black"),
        )
    )
annual_bar_chart.update_layout(
    barmode="group",
    xaxis_tickangle=-45,
    autotypenumbers="convert types",
    margin=dict(l=00, r=00, b=0, t=0, pad=0),
    title_font_size=40,
    paper_bgcolor="#f0f0f0",
    plot_bgcolor="#f0f0f0",
    autosize=True,
    showlegend=True,
    width=1830,
    height=500,
    yaxis_tickprefix="€",
    yaxis_tickformat=",.",
)
annual_bar_chart.show()

enter image description here

  • Related