Python Plotly format axis numbers as %

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 16:31:22

问题


Here's the dataframe

df = pd.DataFrame({"col_1":[0.00143,0.00653,0.00732],"col_2":[0.00984,0.00192,0.00751]},index=['A','B','C'])

Here's my plotly code:

trace0 = go.Bar(
x=[df.columns[0],df.columns[1]],
y=[df.ix[0,0],df.ix[0,1]],
name=df.index[0],
# marker=dict(color=cof_pal[0]
)


trace1 = go.Bar(
x=[df.columns[0],df.columns[1]],
y=[df.ix[1,0],df.ix[1,1]],
name=df.index[1],
# marker=dict(color=cof_pal[1]
)



trace2 = go.Bar(
x=[df.columns[0],df.columns[1]],
y=[df.ix[2,0],df.ix[2,1]],
name=df.index[2],
# marker=dict(color=cof_pal[2]
)


layout = go.Layout(showlegend=True, title="Title", font=dict(size=Chart_Title_Font_Size),
               yaxis=dict(title='Percentage Change(%)', titlefont=dict(size=yaxis_font_size), tickfont=dict(size=yaxis_font_size)),
               xaxis=dict(title='Time', titlefont=dict(size=xaxis_font_size), tickfont=dict(size=yaxis_font_size))
              )


data = [trace0,trace1,trace2]

fig = go.Figure(data=data, layout=layout)
iplot(fig, filename='fig1')

I need the y axis to be x.xx% format (or n digits after the decimal point).

Another question is: the y axis title "Percentage Change(%)" is too close to the y axis numbers. How to move it further?


回答1:


You could set tickformat in the layout options.

layout = go.Layout(yaxis=dict(tickformat=".2%"))



回答2:


You can use ticksuffix in the layout options.

yaxis=dict(ticksuffix=".00%")

layout = go.Layout(                
                yaxis=dict(ticksuffix=".00%", tickangle=45, title='Percentage Change(%)')

https://plot.ly/python/reference/#layout-yaxis



来源:https://stackoverflow.com/questions/41582305/python-plotly-format-axis-numbers-as

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!