I’m working on creating a line graph via plotly. I am running into two problems. Plotly is defaulting to show every 7th value on the x axis and it is showing a year value for my first x axis value (as seen in the screenshot). Here is the code running. If either could be fixed, that would be great!
from connect_to_db import get_df
import plotly.express as px
import os
df = get_df()
fig = px.bar(df, x='Date', y='Total', color='CUSTOMER_TYPE',
             color_discrete_sequence=["#E15759","#8CD17D"],  width=800, height=400)
fig.update_layout({'plot_bgcolor': 'rgba(0, 0, 0, 0)', 'paper_bgcolor': 'rgba(0, 0, 0, 0)', })
fig.update_layout(
    legend=dict(
        orientation="h",
        yanchor="bottom",
        y=1.02,
        xanchor="right",
        x=.4))
fig.update_layout({
    'legend_title_text': ''
},
    xaxis_title="",
    yaxis_title="Tested Count"
)
fig.update_xaxes(showline=True, linewidth=1, linecolor='#FBFBFB', mirror=False)
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor="#FBFBFB",showline=True, linewidth=2, linecolor='#FBFBFB', mirror=False)
fig.show()
Thanks all!
Advertisement
Answer
In order to make the year disappear you’ll just have to make another specification for xaxis_tickformat, like xaxis_tickformat = '%b' to get:
Or you can go for xaxis_tickformat = '%B' and get:
In both cases the year is removed, as per your request.
Complete code:
import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
fig = go.Figure(go.Scatter(
    x = df['Date'],
    y = df['AAPL.High'],
))
fig.update_layout(
    title = 'Time Series with Custom Date-Time Format',
    #xaxis_tickformat = '%d %B (%a)<br>%Y'
    xaxis_tickformat = '%d %B <br>%Y'
    #xaxis_tickformat = '%B'
)
fig.show()
 
						

