[英]I can't figure out how to make my Plotly charts show whole numbers only
image of plotly chart plotly 图表的图像
Hello, I'm really struggling to figure out how to format the axes on this chart.你好,我真的很想弄清楚如何格式化这张图表上的轴。 I've gone through the documentation and tried all sorts of different formatting suggestions from here and elsewhere but really not getting it.我已经阅读了文档并尝试了来自此处和其他地方的各种不同的格式建议,但实际上没有得到它。 As you can see, the bottom chart has a.5 number, I want that to be skipped altogether and only have whole numbers along the axis.如您所见,底部图表有一个 .5 数字,我希望将其完全跳过,轴上只有整数。
I've seen,d as a tickformat option to do this in about every answer, but I can't get that to work or I'm not seeing how to apply it to the second chart.我已经看到,d 作为一个 tickformat 选项可以在几乎每个答案中执行此操作,但我无法让它工作,或者我没有看到如何将它应用于第二个图表。
Can anyone with some Plotly charting experience help me out?有一些 Plotly 制图经验的人可以帮助我吗?
Here's the pertinent code:这是相关的代码:
def create_chart():
#Put data together into an interactive chart
fig.update_layout(height=500, width=800, yaxis_tickprefix = '$', hovermode='x unified', xaxis_tickformat =',d',
template=symbol_template, separators=".", title_text=(df.columns[DATA_COL_1]) + " & Units 2015-2019"
)
I believe what is happening is that the xaxis_tickformat
parameter is affecting only the first subplot, but not the second one.我相信正在发生的事情是xaxis_tickformat
参数只影响第一个子图,而不影响第二个子图。 To modify the formatting for each subplot, you can pass a dictionary with the tickformat
parameter to yaxis
, yaxis2
, .... and so on for however many subplots you have (in your case, you only have 2 subplots).要修改每个子图的格式,您可以将带有tickformat
参数的字典传递给yaxis
、 yaxis2
……等等,无论您有多少个子图(在您的情况下,您只有 2 个子图)。
import pandas as pd
from plotly.subplots import make_subplots
import plotly.graph_objects as go
## recreate the df
df = pd.DataFrame({'Year':[2015,2016,2017,2018,2019],
'Sales':[8.8*10**7,8.2*10**7,8.5*10**7,9.1*10**7,9.6*10**7],
'Units':[36200,36500,36900,37300,37700]})
def create_chart():
#Put data together into an interactive chart
fig = make_subplots(rows=2, cols=1)
fig.add_trace(go.Scatter(
x=df.Year,
y=df.Sales,
name='Sales',
mode='lines+markers'
), row=1, col=1)
fig.add_trace(go.Scatter(
x=df.Year,
y=df.Units,
name='Units',
mode='lines+markers'
), row=2, col=1)
fig.update_layout(
title_x=0.5,
height=500,
width=800,
yaxis_tickprefix = '$',
hovermode='x unified',
xaxis_tickformat =',d',
## this will change the formatting for BOTH subplots
yaxis=dict(tickformat ='d'),
yaxis2=dict(tickformat ='d'),
# template=symbol_template,
separators=".",
title={
'text':"MCD Sales & Units 2015-2019",
'x':0.5
}
)
fig.show()
create_chart()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.