Need to display % sign along with the numeric value. Please check below attached graph. I there any way to add % sign with numeric value?
# Import pandas library
import pandas as pd
# initialize list of lists
data = [['2017/2018', 'Renewable',21], ['2018/2019', 'Renewable',42], ['2019/2020', 'Renewable',75],
['2017/2018', 'Non-Renewable',79], ['2018/2019', 'Non-Renewable',58], ['2019/2020', 'Non-Renewable',25]]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Year', 'Energy','Energy Mix %'])
# print dataframe.
df
fig = px.bar(df, x="Year", y="Energy Mix %", color="Energy", title="RENEWABLE ENERGY MIX",
text_auto=True,template='plotly_dark')
fig.show()
CodePudding user response:
You can use:
fig.for_each_trace(lambda t: t.update(texttemplate = t.texttemplate ' %'))
And get:
Complete code:
# Import pandas library
import pandas as pd
import plotly.express as px
# initialize list of lists
data = [['2017/2018', 'Renewable',21], ['2018/2019', 'Renewable',42], ['2019/2020', 'Renewable',75],
['2017/2018', 'Non-Renewable',79], ['2018/2019', 'Non-Renewable',58], ['2019/2020', 'Non-Renewable',25]]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Year', 'Energy','Energy Mix %'])
fig = px.bar(df, x="Year", y="Energy Mix %", color="Energy", title="RENEWABLE ENERGY MIX",
text_auto=True,template='plotly_dark')
fig.for_each_trace(lambda t: t.update(texttemplate = t.texttemplate ' %'))
fig.show()


