绘制来自小数点的百分比

发布于 2025-02-10 22:05:08 字数 1337 浏览 1 评论 0原文

我有一个要在列图中绘制的数据框。数据帧显示了小数点的百分比(例如0.994400),我想在我的图表上显示它们为99.944%。

我试图多次更改图表的轴,但是值总是汇总到100%。如何获得轴和悬停数字以显示小数点?

例如,第一个数据点应显示99.944%,但显示100%。

我的dataframe:df_uptime_percentage

名称百分比
API 10.9994400
API 2API
0.999913API
40.999925
API 50.9999461

0.999914

cf.go_offline()

fig = df_uptime_percentage.iplot(kind='bar', 
        yTitle='Percent Uptime',
        xTitle='API Endpoint',
        asFigure = True)
fig.update_layout(yaxis_tickformat = '%')
fig.update_layout(hovermode="x unified")


fig.iplot()

3

我导入的库:

import time
from dateutil.relativedelta import relativedelta
import math
import requests
import re
import json
import pandas as pd 
import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
from plotly.graph_objs import *
init_notebook_mode()
%matplotlib inline

I have a dataframe that I want to plot in a column chart. The dataframe shows percentages in decimal points (such as 0.9994400) and I would like to show them on my chart as 99.944%.

I've tried to change the axis of the chart multiple times, but the values always round up to 100%. How can I get the axis and hover numbers to show the decimal points?

For example, the first data point should show 99.944%, but it shows 100%.

My dataframe: df_uptime_percentage

NamePercentage
API 10.9994400
API 20.999914
API 30.999913
API 40.999925
API 50.999461

My plot script:

cf.go_offline()

fig = df_uptime_percentage.iplot(kind='bar', 
        yTitle='Percent Uptime',
        xTitle='API Endpoint',
        asFigure = True)
fig.update_layout(yaxis_tickformat = '%')
fig.update_layout(hovermode="x unified")


fig.iplot()

My plot output:
enter image description here

My imported libraries:

import time
from dateutil.relativedelta import relativedelta
import math
import requests
import re
import json
import pandas as pd 
import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
from plotly.graph_objs import *
init_notebook_mode()
%matplotlib inline

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唔猫 2025-02-17 22:05:08

我不确定是什么原因导致数字被四舍五入,但是如果您在百分比表中添加Y轴刻度标记并添加数字数,则轴将有两个小数点,并且悬停文本也将有两个十进制位置。

import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
from plotly.graph_objs import *

cf.go_offline()

fig = df.iplot(kind='bar', 
        yTitle='Percent Uptime',
        xTitle='API Endpoint',
        asFigure=True)
fig.update_layout(yaxis_tickformat='.3%')
fig.update_layout(hovermode="x unified")

fig.iplot()

I am not sure what is causing the numbers to be rounded, but if you add the y-axis tick marks to the percent table and add the number of digits, the axis will have two decimal places, and the hover text will also have two decimal places.

import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
from plotly.graph_objs import *

cf.go_offline()

fig = df.iplot(kind='bar', 
        yTitle='Percent Uptime',
        xTitle='API Endpoint',
        asFigure=True)
fig.update_layout(yaxis_tickformat='.3%')
fig.update_layout(hovermode="x unified")

fig.iplot()

enter image description here

海未深 2025-02-17 22:05:08

您并没有完全捕捉到代码中的位置,您从数据框架中导入数字以用于图,但是从您描述问题的方式来看,我认为这与浮点数错误有关。我编写了一些基本代码来转换您获得的API1的值:

x = 0.9994400
x = x*100
x = float("{:.2f}".format(x))

print(x)

此代码打印99.94。如果要将代码中的第三行更改为

x = float("{:.3f}".format(x))

99.944

希望这有帮助!

Didn't quite catch where in your code you imported the numbers from your data frame to be used for your plot, but from how you describe your issue, I'm thinking this has something to do with a floating-point error. I wrote some basic code to convert the value you got for API1:

x = 0.9994400
x = x*100
x = float("{:.2f}".format(x))

print(x)

This code prints 99.94. If you were to change the 3rd line in the code to

x = float("{:.3f}".format(x))

it would give you 99.944.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文