简体   繁体   English

plotly快递| 更改 y 轴上的刻度单位

[英]plotly express | change tick units on y axis

I want to add units to my yaxis of my bar chart.我想将单位添加到我的条形图的 y 轴。 Im using plotly.express for that but didnt found a working solution inside the documentation.我为此使用 plotly.express ,但在文档中没有找到可行的解决方案。 text_auto() and fig.update_layout() are not working for me right now. text_auto() 和 fig.update_layout() 现在不适合我。 (Tried that thread without success -> Changing Text Inside Plotly Express Bar Charts ) (尝试了该线程但没有成功 -> 更改 Plotly Express Bar Charts 内的文本

Im not using panda data format right now, rather a own dictionary i feed plotly.我现在不使用熊猫数据格式,而是使用自己的字典,我提供 plotly。

Please bear with me as im still new to analysing data with plotly.请耐心等待,因为我对使用 plotly 分析数据还是新手。

如果没有每个刻度的单位,它现在的样子。

import json
import requests
from operator import itemgetter
import plotly.express as px

#hyperlinks = xaxis with description and link to the game
#times = yaxis total playtime (<- where i want to use "xx.xh")
#titles = simple hover text
df = {
    "x" : hyperlinks,
    "y" : times,
    "titles" : titles,
    }   

fig = px.bar(
    df,
    x="x", 
    y="y",
    hover_data=["titles"],
    color="y",
    color_continuous_scale="Plotly3_r",
    title=f"Top 30 games with most playtime",
    text_auto=".h",
    labels={"y" : "entire playtime of steam games"},
    )

fig.update_layout(
    yaxis={
        "tickformat" : '.h'
        }
    )
fig.show()
fig.write_html("My_most_played_games.html")

I have generated some random values for the example.我为示例生成了一些随机值。

Since recently you can have access to figure parameters of plotly using fig.full_figure_for_development() from there you can extract element to check where plotly added ticks and regenerate them adding to them any string you want从最近开始,您可以使用fig.full_figure_for_development()从那里访问 plotly 的图形参数,您可以提取元素以检查 plotly 在哪里添加了刻度并重新生成它们,并将它们添加到您想要的任何字符串

import plotly.express as px
import numpy as np
#hyperlinks = xaxis with description and link to the game
#times = yaxis total playtime (<- where i want to use "xx.xh")
#titles = simple hover text
df = {
    "x" : ['black desert', 'arma 3', 'borderland 2', 'Cyberpunk'],
    "y" : [420, 350, 310, 180],
    "titles" : ['black desert', 'arma 3', 'borderland 2', 'Cyberpunk'],
    }   

fig = px.bar(
    df,
    x="x", 
    y="y",
    hover_data=["titles"],
    color="y",
    color_continuous_scale="Plotly3_r",
    title=f"Top 30 games with most playtime",
    text_auto=".h",
    labels={"y" : "entire playtime of steam games"},
    )

# Important part to recover infor from the figure
full_fig = fig.full_figure_for_development()  # recover data from figure
range_vl = full_fig.layout.yaxis.range  # get range of y axis
distance_tick = full_fig.layout.yaxis.dtick  # get distance between ticks

number_ticks = range_vl[1]//full_fig.layout.yaxis.dtick + 1 # calculate number of ticks of your figure

tick_vals = [range_vl[0]+distance_tick*num for num in range(int(number_ticks))] # generate your ticks
tick_text = [f"{val} h" for val in tick_vals] #generate text for your ticks

fig.update_layout(
    # set tick mode to array, tickvals to your vals calculated and tick text  to the text genrated
    yaxis={"tickmode":"array","tickvals":tick_vals, "ticktext": tick_text}
        
    )
fig.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM