[英]Using custom functions to format display of values in plotly
I am writing a small web-app to display prices over time for certain items in a game. 我正在编写一个小型网络应用程序,以显示游戏中某些物品随时间的价格。 The prices are integers but I want to format them to how the in game currency is displayed. 价格是整数,但我想将其格式化为游戏币的显示方式。 This is the function I am using. 这是我正在使用的功能。 It takes an integer and returns a string. 它需要一个整数并返回一个字符串。
def copper_to_price(copper): # Converts copper int to a normal price
copper = int(copper)
s, c = divmod(copper, 100)
g, s = divmod(s, 100)
if g == 0:
if s == 0:
return '{}c'.format(c)
else:
return '{}s{}c'.format(s, c)
else:
return '{}g{}s{}c'.format(g, s, c)
I am plotting these prices with plotly and want to have the y-axis ticks and y value hover text to be formatted with my function. 我正在用绘图方式绘制这些价格,并希望使用我的函数格式化y轴刻度和y值悬停文本。
With plotly you can specify an y-axis tick formatter using tickformat but it seems you can only specify predefined formatting options and cannot pass your own custom formatting function. 随着plotly您可以使用指定y轴的刻度格式tickformat但似乎你只能指定预先确定的格式选项,并不能通过自己的自定义格式功能。
It is also possible to specify your own tick values using tickvals and replace the text of these values with ticktext . 也可以使用指定自己的刻度值tickvals并更换这些值的文本ticktext 。 I could specify my own y values and then format it with my function and return that as my ticktext but then I would have to generate my own y value ticks that will fit my data. 我可以指定自己的y值,然后使用函数对其进行格式设置并将其返回为我的刻度文本,但是随后我将不得不生成自己的y值刻度以适合我的数据。 I haven't found any good python scripts that can generate y tick values that will fit your data well. 我还没有找到任何可以生成适合您的数据的y tick值的优秀python脚本。
How can I display my y values formatted with my own function using plotly? 如何使用plotly显示以自己的函数格式化的y值?
You already described the way to go. 您已经描述了要走的路。 Why not take your plotting data, extract minimum and maximum values, use tv = np.arrange(min, max, 0.25)
to create the sequence of tickvals
and then use ticktext = [copper_to_price(member) for member in tv]
to format them. 为什么不获取绘图数据,提取最小值和最大值,使用tv = np.arrange(min, max, 0.25)
创建tickvals
序列,然后使用ticktext = [copper_to_price(member) for member in tv]
格式化它们。 When you are done with that, just set the corresponding layout options 完成后,只需设置相应的布局选项
layout = go.Layout(
yaxis=dict(
tickmode='array',
tickvals=tv,
ticktext=tt
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.