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

Rit*_*ngh 0 python charts plotly seaborn ohlc

我正在使用plotly 制作 OHLC 图。我偶然发现了一个问题。x 轴上的标签看起来非常混乱。有没有办法让它更整洁。或者我们可以只显示极端日期值吗?例如,仅显示第一个日期值和最后一个日期值。日期范围本质上是动态的。我正在使用下面的查询来制作图表。谢谢您的帮助。

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)
Run Code Online (Sandbox Code Playgroud)

这里的tickerDf是包含股票相关数据的数据框。

在此输入图像描述

小智 5

One way that you can use is changing the nticks. This can be done by calling fig.update_xaxes() and passing nticks as the parameter. For example, here's a plot with the regular amount of ticks, with no change. 在此输入图像描述

and here is what it looks like after specifying the number of ticks: 在此输入图像描述

The code for the second 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()
Run Code Online (Sandbox Code Playgroud)

the important line, again is:

fig.update_xaxes(nticks=5) 
Run Code Online (Sandbox Code Playgroud)