简体   繁体   English

Plotly:如何更改 x 轴上的默认日期并从轴中删除年份?

[英]Plotly: How to change default date on x-axis and remove year from axis?

I'm working on creating a line graph via plotly.我正在通过 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). Plotly 默认在 x 轴上显示每 7 个值,它显示我的第一个 x 轴值的年份值(如屏幕截图所示)。 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!谢谢大家!

In order to make the year disappear you'll just have to make another specification for xaxis_tickformat , like xaxis_tickformat = '%b' to get:为了使年份消失,您只需为xaxis_tickformat制定另一个规范,例如xaxis_tickformat = '%b'即可获得:

在此处输入图片说明

Or you can go for xaxis_tickformat = '%B' and get:或者你可以去xaxis_tickformat = '%B'并得到:

在此处输入图片说明

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

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

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