You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何设置时间格式:仅展示时间不含日期以优化图表显示

实现图表仅展示时间(隐藏日期)的解决方案

以下是主流可视化工具/库的具体实现方法,适配你需要的纯时间显示效果:

1. Matplotlib(Python)

通过自定义日期格式化器,强制只显示小时和分钟:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd

# 示例数据
df = pd.DataFrame({
    'datetime': pd.date_range('2024-05-20 08:30', periods=10, freq='30min'),
    'value': [12, 15, 18, 22, 20, 17, 14, 11, 9, 13]
})

fig, ax = plt.subplots()
ax.plot(df['datetime'], df['value'])

# 设置X轴仅显示小时:分钟
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
# 自动旋转标签避免重叠
plt.xticks(rotation=45)

plt.show()

2. Plotly(Python/JS)

在布局中配置时间轴的显示格式:

import plotly.express as px
import pandas as pd

df = pd.DataFrame({
    'datetime': pd.date_range('2024-05-20 08:30', periods=10, freq='30min'),
    'value': [12, 15, 18, 22, 20, 17, 14, 11, 9, 13]
})

fig = px.line(df, x='datetime', y='value')
fig.update_layout(
    xaxis=dict(
        tickformat='%H:%M',  # 仅显示小时和分钟
        tickmode='auto'
    )
)
fig.show()

3. ECharts(JavaScript)

在xAxis的axisLabel中指定时间格式化规则:

var myChart = echarts.init(document.getElementById('main'));

var option = {
    xAxis: {
        type: 'time',
        axisLabel: {
            formatter: function(value) {
                // 将时间戳转换为HH:MM格式
                var date = new Date(value);
                return `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
            }
        }
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        data: [
            [new Date('2024-05-20 08:30').getTime(), 12],
            [new Date('2024-05-20 09:00').getTime(), 15],
            // 更多数据...
        ],
        type: 'line'
    }]
};

myChart.setOption(option);

4. Excel图表

  1. 选中图表的时间轴(X轴),右键选择「设置坐标轴格式」
  2. 在「数字」选项卡中,选择「时间」类别,挑选HH:MM格式(或自定义格式输入HH:MM)
  3. 确认设置后,轴标签将仅显示时间部分

内容的提问来源于stack exchange,提问作者Bilbo Baggins

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 11:31:34