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

Matplotlib图表X轴刻度顺序与等间距设置问题

解决Matplotlib时间轴刻度等间距且顺序正确的问题

需求

绘制Matplotlib图表时,X轴时间刻度需按指定序列(0, 6, 24, 114, 228)显示,且保证刻度等间距。

问题

  • 将time设为int类型时,X轴刻度顺序正确但间距不等(因时间数值本身差距大,导致轴上刻度分布不均)
  • 改为object类型时,刻度间距正常但顺序错误(字符串类型会按字典序排序,打乱指定的时间序列)

现有实现代码

# 计算每个时间点和类别下weighted_delta_13C的总和
sum_df = df.groupby(['time', 'Category'])['weighted_delta_13C'].sum().reset_index()

# 绘图
plt.figure(figsize=(10, 6))

# 遍历每个类别绘制其总和值
for category in sum_df['Category'].unique():
    category_data = sum_df[sum_df['Category'] == category]
    plt.plot(category_data['time'], category_data['weighted_delta_13C'], marker='o', linestyle='-', label=f'Category {category}')

plt.title('Sum of weighted_delta_13C at Each Time Point')
plt.xlabel('Time')
plt.ylabel('Sum of weighted_delta_13C')
plt.grid(True)
plt.xticks(sum_df['time'].unique())  # 设置X轴刻度为时间点
plt.legend()
plt.show()

已尝试的两种方案

方案1:索引映射实现等间距刻度

通过将离散数值映射为连续索引,用索引绘制图表后替换刻度标签:

n = 12
a = np.arange(n)
x = 2**a
y = np.random.rand(n)

fig = plt.figure(1, figsize=(7,7))
ax1  = fig.add_subplot(211)
ax2  = fig.add_subplot(212)

ax1.plot(x,y)
ax1.xaxis.set_ticks(x)

ax2.plot(a, y) # 将y绘制为索引a的函数
ax2.xaxis.set_ticks(a) # 设置刻度为索引值
ax2.xaxis.set_ticklabels(x) # 将刻度标签替换为原数值

方案2:指定分类顺序

针对分类类型轴,通过设置索引并按指定顺序选取数据来保证顺序:

field = "Day"
day_order = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
ax = df.set_index(field).loc[day_order].plot(kind="bar", legend=False)
ax.set_ylabel("Value")

最终解决方案

结合上述两种思路,针对需求修改代码如下:

import matplotlib.pyplot as plt
import pandas as pd

# 指定固定的时间序列顺序
time_order = [0, 6, 24, 114, 228]

# 计算每个时间点和类别下weighted_delta_13C的总和
sum_df = df.groupby(['time', 'Category'])['weighted_delta_13C'].sum().reset_index()

# 为每个时间值分配连续索引,保证顺序和等间距基础
sum_df['time_idx'] = sum_df['time'].map({t:i for i,t in enumerate(time_order)})

# 绘图
plt.figure(figsize=(10, 6))

# 遍历每个类别绘制折线
for category in sum_df['Category'].unique():
    category_data = sum_df[sum_df['Category'] == category]
    # 按时间索引排序,确保折线绘制顺序正确
    category_data_sorted = category_data.sort_values('time_idx')
    plt.plot(category_data_sorted['time_idx'], category_data_sorted['weighted_delta_13C'], 
             marker='o', linestyle='-', label=f'Category {category}')

plt.title('各时间点weighted_delta_13C总和')
plt.xlabel('时间')
plt.ylabel('weighted_delta_13C总和')
plt.grid(True)
# 设置刻度为索引位置,标签替换为指定的时间值
plt.xticks(ticks=range(len(time_order)), labels=time_order)
plt.legend()
plt.show()

核心逻辑说明

  1. 用time_idx将离散时间值映射为0-4的连续整数,确保X轴刻度天然等间距
  2. 强制按time_idx排序数据,避免因原始数据顺序问题导致折线错乱
  3. 通过plt.xticks替换刻度标签为指定时间序列,兼顾等间距和正确顺序

内容的提问来源于stack exchange,提问作者Musharaf Ahsan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 02:36:04