简体   繁体   English

如何在plotly图表中整齐或以一定间隔的形式显示X轴日期刻度

[英]How to show the X-axis date ticks neatly or in a form of certain interval in a plotly chart

I am making an OHLC graph using plotly. I have stumbled across one issue.我正在使用 plotly 制作 OHLC 图。我偶然发现了一个问题。 The labels in the x-axis is looking really messy. x 轴上的标签看起来很乱。 Is there a way to make it more neat.有没有办法让它更整洁。 Or can we only show the extreme date values.或者我们可以只显示极端日期值吗? For example only the first date value and last date value is show.例如,仅显示第一个日期值和最后一个日期值。 The date range is a dynamic in nature.日期范围本质上是动态的。 I am using the below query to make the graph.我正在使用以下查询来制作图表。 Thanks for the help.谢谢您的帮助。

fig = go.Figure(data=go.Candlestick(x=tickerDf.index.date,
                    open=tickerDf.Open,
                    high=tickerDf.High,
                    low=tickerDf.Low,
                    close=tickerDf.Close) )

    fig.update_xaxes(showticklabels=True )    #Disable xticks 
    fig.update_layout(width=800,height=600,xaxis=dict(type = "category") ) # hide dates with no values
    
    st.plotly_chart(fig)

Here tickerDf is the dataframe which contains the stock related data.这里的 tickerDf 是包含股票相关数据的 dataframe。

在此处输入图像描述

One way that you can use is changing the nticks .您可以使用的一种方法是更改nticks This can be done by calling fig.update_xaxes() and passing nticks as the parameter.这可以通过调用fig.update_xaxes()并将nticks作为参数传递来完成。 For example, here's a plot with the regular amount of ticks, with no change.例如,这里有一个 plot,具有常规的滴答数,没有变化。 在此处输入图像描述

and here is what it looks like after specifying the number of ticks:这是指定刻度数后的样子: 在此处输入图像描述

The code for the second plot:第二个plot的代码:

import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv('./finance-charts-apple.csv')

fig = go.Figure([go.Scatter(x=df['Date'], y=df['AAPL.High'])])

fig.update_xaxes(nticks=5)

fig.show()

the important line, again is:重要的是:

fig.update_xaxes(nticks=5) 

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

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