【发布时间】:2021-05-10 23:29:10
【问题描述】:
我想从 pandas 数据框创建一个图表,其中轴刻度应该是百分比。
使用 matplotlib 有一个不错的轴格式化程序,它可以根据给定的最大值自动计算百分比刻度:
示例:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame( { 'images': np.arange(0, 355, 5) } ) # 70 items in total, max is 350
ax = df.plot()
ax.yaxis.set_major_formatter(pltticker.PercentFormatter(xmax=350))
loc = pltticker.MultipleLocator(base=50) # locator puts ticks at regular intervals
ax.yaxis.set_major_locator(loc)
由于 matplotlib 的使用相当乏味,我想对 Plotly 做同样的事情。 我只找到了将刻度标签格式化为百分比的选项 - 但没有计算对我来说刻度和百分比。 有没有办法使用自动百分比刻度,还是我必须每次都手动计算(呃)?
import plotly.express as px
import pandas as pd
fig = px.line(df, x=df.index, y=df.images, labels={'index':'num of users', '0':'num of img'})
fig.layout.yaxis.tickformat = ',.0%' # does not help
fig.show()
感谢您的任何提示。
【问题讨论】:
标签: python pandas matplotlib format plotly