Ritesh Singh
Ritesh Singh

Reputation: 73

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. The labels in the x-axis is looking really messy . 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.

enter image description here

Upvotes: 0

Views: 6341

Answers (2)

Đỗ Văn Thắng
Đỗ Văn Thắng

Reputation: 35

I just found a solution

fig1.update_xaxes(type='category')
fig1.update_xaxes(nticks=12) 

But thank you very much

Upvotes: 0

AS11
AS11

Reputation: 1461

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. enter image description here

and here is what it looks like after specifying the number of ticks: enter image description here

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()

the important line, again is:

fig.update_xaxes(nticks=5) 

Upvotes: 5

Related Questions