简体   繁体   English

我不知道如何让我的 Plotly 图表只显示整数

[英]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参数的字典传递给yaxisyaxis2 ……等等,无论您有多少个子图(在您的情况下,您只有 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.

相关问题 我不知道如何让这个 function 正常工作 - I can't figure out how to make this function work properly 我想弄清楚怎么做 - I want to figure out how to make t 我正在尝试在我的 class 中创建一个用户输入字典以获得额外的信用,但我无法弄清楚如何正确结束程序 - I'm trying to make a user input dictionary for extra credit in my class and I can't figure out how to properly end the program 我无法弄清楚为什么我的 python 脚本可以运行,但是当尝试使其成为可执行文件时它不起作用 - I can't figure out why my python script works but when try to make it an executable it does not work 我似乎无法弄清楚如何生成多个随机数等于10分 - I can't seem to figure out how to generate multiple random numbers to equal a score of 10 我正在尝试为我的“情绪分析”项目制作前端,但不知道如何让我的“预测功能”在前端工作 - I'm trying to make Front-end for my “Sentiment Analysis” project but can't figure out how to make my “prediction_function” work at the Front-end 无法弄清楚如何使我的功能在Python中不返回None - Can't figure out how to make my funtion not return None in Python 我的代码中不断出现错误,无法弄清楚 - I keep getting an error in my code and can't figure out 我正在使用 python 3.9,但我不知道如何在我的 Windows 10 中安装 pyaudio - I am using python 3.9 and I can't figure out how to install pyaudio in my Windows 10 Python-我无法弄清的蛇游戏错误 - Python - A bug with my snake game that I can't figure out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM