小能豆

如何格式化pandas图上的日期时间 xticklabels

py

我想控制日期标签。我循环遍历数据的各种子集,有时绘制长间隔,每 10 年一次比较合适;有时绘制较短间隔,每 5 年或 2 年或 1 年一次比较合适;有时绘制几个月的间隔,月份标签比较合适;有时是混合情况。我知道如何将日期转换为字符串并提取年份并手动制作标签,但由于我的日期范围不断变化,我希望有一定程度的灵活性,并尽可能使用内置函数。我试过使用这些matplotlib.dates函数:AutoDateLocator,,,:请参阅下面的恐怖博物馆。我似乎遗漏了一些基本的东西。ConciseDateFormatter``YearLocator``DateFormatter

在下面的例子中,默认日期标签是 1949、1959 等,而我想要的是 1950、1960 等,因此标签的频率很好,但起始日期却不是。有没有办法自动将所选年份设为 10 或 5 的倍数?

默认地块

import matplotlib.pyplot as plt
import pandas as pd
from pandas import Timestamp
from numpy import nan

# Default plot:
f, ax = plt.subplots()
df.plot(ax=ax, x='Date')
plt.show()

1.png

# attempt 1
# https://matplotlib.org/stable/api/dates_api.html
from matplotlib.dates import YearLocator, DateFormatter
f, ax = plt.subplots()
df.plot(ax=ax, x='Date')
ax.xaxis.set_major_locator(YearLocator(10)) # <- ticks every 10 years
ax.xaxis.set_major_formatter(DateFormatter('%Y'))  # <- show only the year
plt.show()

2.png

# attempt 2
# https://matplotlib.org/stable/gallery/ticks/date_concise_formatter.html
from matplotlib.dates import AutoDateLocator, ConciseDateFormatter
f, ax = plt.subplots()
df.plot(ax=ax, x='Date')
locator = AutoDateLocator(minticks=4, maxticks=10)  # <- autodetect a good place to place ticks
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(ConciseDateFormatter(locator))
plt.show() # <- Months appear where they shouldn't

3.png

数据:

data_dict = {'Date': {0: Timestamp('1946-01-01 00:00:00'), 1: Timestamp('1946-04-01 00:00:00'), 2: Timestamp('1946-07-01 00:00:00'), 3: Timestamp('1946-10-01 00:00:00'), 4: Timestamp('1947-01-01 00:00:00'), 5: Timestamp('1947-04-01 00:00:00'), 6: Timestamp('1947-07-01 00:00:00'), 7: Timestamp('1947-10-01 00:00:00'), 8: Timestamp('1948-01-01 00:00:00'), 9: Timestamp('1948-04-01 00:00:00'), 10: Timestamp('1948-07-01 00:00:00'), 11: Timestamp('1948-10-01 00:00:00'), 12: Timestamp('1949-01-01 00:00:00'), 13: Timestamp('1949-04-01 00:00:00'), 14: Timestamp('1949-07-01 00:00:00'), 15: Timestamp('1949-10-01 00:00:00'), 16: Timestamp('1950-01-01 00:00:00'), 17: Timestamp('1950-04-01 00:00:00'), 18: Timestamp('1950-07-01 00:00:00'), 19: Timestamp('1950-10-01 00:00:00'), 20: Timestamp('1951-01-01 00:00:00'), 21: Timestamp('1951-04-01 00:00:00'), 22: Timestamp('1951-07-01 00:00:00'), 23: Timestamp('1951-10-01 00:00:00'), 24: Timestamp('1952-01-01 00:00:00'), 25: Timestamp('1952-04-01 00:00:00'), 26: Timestamp('1952-07-01 00:00:00'), 27: Timestamp('1952-10-01 00:00:00'), 28: Timestamp('1953-01-01 00:00:00'), 29: Timestamp('1953-04-01 00:00:00'), 30: Timestamp('1953-07-01 00:00:00'), 31: Timestamp('1953-10-01 00:00:00'), 32: Timestamp('1954-01-01 00:00:00'), 33: Timestamp('1954-04-01 00:00:00'), 34: Timestamp('1954-07-01 00:00:00'), 35: Timestamp('1954-10-01 00:00:00'), 36: Timestamp('1955-01-01 00:00:00'), 37: Timestamp('1955-04-01 00:00:00'), 38: Timestamp('1955-07-01 00:00:00'), 39: Timestamp('1955-10-01 00:00:00'), 40: Timestamp('1956-01-01 00:00:00'), 41: Timestamp('1956-04-01 00:00:00'), 42: Timestamp('1956-07-01 00:00:00'), 43: Timestamp('1956-10-01 00:00:00'), 44: Timestamp('1957-01-01 00:00:00'), 45: Timestamp('1957-04-01 00:00:00'), 46: Timestamp('1957-07-01 00:00:00'), 47: Timestamp('1957-10-01 00:00:00'), 48: Timestamp('1958-01-01 00:00:00'), 49: Timestamp('1958-04-01 00:00:00'), 50: Timestamp('1958-07-01 00:00:00'), 51: Timestamp('1958-10-01 00:00:00'), 52: Timestamp('1959-01-01 00:00:00'), 53: Timestamp('1959-04-01 00:00:00'), 54: Timestamp('1959-07-01 00:00:00'), 55: Timestamp('1959-10-01 00:00:00'), 56: Timestamp('1960-01-01 00:00:00'), 57: Timestamp('1960-04-01 00:00:00'), 58: Timestamp('1960-07-01 00:00:00'), 59: Timestamp('1960-10-01 00:00:00'), 60: Timestamp('1961-01-01 00:00:00'), 61: Timestamp('1961-04-01 00:00:00'), 62: Timestamp('1961-07-01 00:00:00'), 63: Timestamp('1961-10-01 00:00:00'), 64: Timestamp('1962-01-01 00:00:00'), 65: Timestamp('1962-04-01 00:00:00'), 66: Timestamp('1962-07-01 00:00:00'), 67: Timestamp('1962-10-01 00:00:00'), 68: Timestamp('1963-01-01 00:00:00'), 69: Timestamp('1963-04-01 00:00:00'), 70: Timestamp('1963-07-01 00:00:00'), 71: Timestamp('1963-10-01 00:00:00'), 72: Timestamp('1964-01-01 00:00:00'), 73: Timestamp('1964-04-01 00:00:00'), 74: Timestamp('1964-07-01 00:00:00'), 75: Timestamp('1964-10-01 00:00:00'), 76: Timestamp('1965-01-01 00:00:00'), 77: Timestamp('1965-04-01 00:00:00'), 78: Timestamp('1965-07-01 00:00:00'), 79: Timestamp('1965-10-01 00:00:00'), 80: Timestamp('1966-01-01 00:00:00'), 81: Timestamp('1966-04-01 00:00:00'), 82: Timestamp('1966-07-01 00:00:00'), 83: Timestamp('1966-10-01 00:00:00'), 84: Timestamp('1967-01-01 00:00:00'), 85: Timestamp('1967-04-01 00:00:00'), 86: Timestamp('1967-07-01 00:00:00'), 87: Timestamp('1967-10-01 00:00:00'), 88: Timestamp('1968-01-01 00:00:00'), 89: Timestamp('1968-04-01 00:00:00'), 90: Timestamp('1968-07-01 00:00:00'), 91: Timestamp('1968-10-01 00:00:00'), 92: Timestamp('1969-01-01 00:00:00'), 93: Timestamp('1969-04-01 00:00:00'), 94: Timestamp('1969-07-01 00:00:00'), 95: Timestamp('1969-10-01 00:00:00'), 96: Timestamp('1970-01-01 00:00:00'), 97: Timestamp('1970-04-01 00:00:00'), 98: Timestamp('1970-07-01 00:00:00'), 99: Timestamp('1970-10-01 00:00:00'), 100: Timestamp('1971-01-01 00:00:00'), 101: Timestamp('1971-04-01 00:00:00'), 102: Timestamp('1971-07-01 00:00:00'), 103: Timestamp('1971-10-01 00:00:00'), 104: Timestamp('1972-01-01 00:00:00'), 105: Timestamp('1972-04-01 00:00:00'), 106: Timestamp('1972-07-01 00:00:00'), 107: Timestamp('1972-10-01 00:00:00'), 108: Timestamp('1973-01-01 00:00:00'), 109: Timestamp('1973-04-01 00:00:00'), 110: Timestamp('1973-07-01 00:00:00'), 111: Timestamp('1973-10-01 00:00:00'), 112: Timestamp('1974-01-01 00:00:00'), 113: Timestamp('1974-04-01 00:00:00'), 114: Timestamp('1974-07-01 00:00:00'), 115: Timestamp('1974-10-01 00:00:00'), 116: Timestamp('1975-01-01 00:00:00'), 117: Timestamp('1975-04-01 00:00:00'), 118: Timestamp('1975-07-01 00:00:00'), 119: Timestamp('1975-10-01 00:00:00'), 120: Timestamp('1976-01-01 00:00:00'), 121: Timestamp('1976-04-01 00:00:00'), 122: Timestamp('1976-07-01 00:00:00'), 123: Timestamp('1976-10-01 00:00:00'), 124: Timestamp('1977-01-01 00:00:00'), 125: Timestamp('1977-04-01 00:00:00'), 126: Timestamp('1977-07-01 00:00:00'), 127: Timestamp('1977-10-01 00:00:00'), 128: Timestamp('1978-01-01 00:00:00'), 129: Timestamp('1978-04-01 00:00:00'), 130: Timestamp('1978-07-01 00:00:00'), 131: Timestamp('1978-10-01 00:00:00'), 132: Timestamp('1979-01-01 00:00:00'), 133: Timestamp('1979-04-01 00:00:00'), 134: Timestamp('1979-07-01 00:00:00'), 135: Timestamp('1979-10-01 00:00:00'), 136: Timestamp('1980-01-01 00:00:00'), 137: Timestamp('1980-04-01 00:00:00'), 138: Timestamp('1980-07-01 00:00:00'), 139: Timestamp('1980-10-01 00:00:00'), 140: Timestamp('1981-01-01 00:00:00'), 141: Timestamp('1981-04-01 00:00:00'), 142: Timestamp('1981-07-01 00:00:00'), 143: Timestamp('1981-10-01 00:00:00'), 144: Timestamp('1982-01-01 00:00:00'), 145: Timestamp('1982-04-01 00:00:00'), 146: Timestamp('1982-07-01 00:00:00'), 147: Timestamp('1982-10-01 00:00:00'), 148: Timestamp('1983-01-01 00:00:00'), 149: Timestamp('1983-04-01 00:00:00'), 150: Timestamp('1983-07-01 00:00:00'), 151: Timestamp('1983-10-01 00:00:00'), 152: Timestamp('1984-01-01 00:00:00'), 153: Timestamp('1984-04-01 00:00:00'), 154: Timestamp('1984-07-01 00:00:00'), 155: Timestamp('1984-10-01 00:00:00'), 156: Timestamp('1985-01-01 00:00:00'), 157: Timestamp('1985-04-01 00:00:00'), 158: Timestamp('1985-07-01 00:00:00'), 159: Timestamp('1985-10-01 00:00:00'), 160: Timestamp('1986-01-01 00:00:00'), 161: Timestamp('1986-04-01 00:00:00'), 162: Timestamp('1986-07-01 00:00:00'), 163: Timestamp('1986-10-01 00:00:00'), 164: Timestamp('1987-01-01 00:00:00'), 165: Timestamp('1987-04-01 00:00:00'), 166: Timestamp('1987-07-01 00:00:00'), 167: Timestamp('1987-10-01 00:00:00'), 168: Timestamp('1988-01-01 00:00:00'), 169: Timestamp('1988-04-01 00:00:00'), 170: Timestamp('1988-07-01 00:00:00'), 171: Timestamp('1988-10-01 00:00:00'), 172: Timestamp('1989-01-01 00:00:00'), 173: Timestamp('1989-04-01 00:00:00'), 174: Timestamp('1989-07-01 00:00:00'), 175: Timestamp('1989-10-01 00:00:00'), 176: Timestamp('1990-01-01 00:00:00'), 177: Timestamp('1990-04-01 00:00:00'), 178: Timestamp('1990-07-01 00:00:00'), 179: Timestamp('1990-10-01 00:00:00'), 180: Timestamp('1991-01-01 00:00:00'), 181: Timestamp('1991-04-01 00:00:00'), 182: Timestamp('1991-07-01 00:00:00'), 183: Timestamp('1991-10-01 00:00:00'), 184: Timestamp('1992-01-01 00:00:00'), 185: Timestamp('1992-04-01 00:00:00'), 186: Timestamp('1992-07-01 00:00:00'), 187: Timestamp('1992-10-01 00:00:00'), 188: Timestamp('1993-01-01 00:00:00'), 189: Timestamp('1993-04-01 00:00:00'), 190: Timestamp('1993-07-01 00:00:00'), 191: Timestamp('1993-10-01 00:00:00'), 192: Timestamp('1994-01-01 00:00:00'), 193: Timestamp('1994-04-01 00:00:00'), 194: Timestamp('1994-07-01 00:00:00'), 195: Timestamp('1994-10-01 00:00:00'), 196: Timestamp('1995-01-01 00:00:00'), 197: Timestamp('1995-04-01 00:00:00'), 198: Timestamp('1995-07-01 00:00:00'), 199: Timestamp('1995-10-01 00:00:00'), 200: Timestamp('1996-01-01 00:00:00'), 201: Timestamp('1996-04-01 00:00:00'), 202: Timestamp('1996-07-01 00:00:00'), 203: Timestamp('1996-10-01 00:00:00'), 204: Timestamp('1997-01-01 00:00:00'), 205: Timestamp('1997-04-01 00:00:00'), 206: Timestamp('1997-07-01 00:00:00'), 207: Timestamp('1997-10-01 00:00:00'), 208: Timestamp('1998-01-01 00:00:00'), 209: Timestamp('1998-04-01 00:00:00'), 210: Timestamp('1998-07-01 00:00:00'), 211: Timestamp('1998-10-01 00:00:00'), 212: Timestamp('1999-01-01 00:00:00'), 213: Timestamp('1999-04-01 00:00:00'), 214: Timestamp('1999-07-01 00:00:00'), 215: Timestamp('1999-10-01 00:00:00'), 216: Timestamp('2000-01-01 00:00:00'), 217: Timestamp('2000-04-01 00:00:00'), 218: Timestamp('2000-07-01 00:00:00'), 219: Timestamp('2000-10-01 00:00:00'), 220: Timestamp('2001-01-01 00:00:00'), 221: Timestamp('2001-04-01 00:00:00'), 222: Timestamp('2001-07-01 00:00:00'), 223: Timestamp('2001-10-01 00:00:00'), 224: Timestamp('2002-01-01 00:00:00'), 225: Timestamp('2002-04-01 00:00:00'), 226: Timestamp('2002-07-01 00:00:00'), 227: Timestamp('2002-10-01 00:00:00'), 228: Timestamp('2003-01-01 00:00:00'), 229: Timestamp('2003-04-01 00:00:00'), 230: Timestamp('2003-07-01 00:00:00'), 231: Timestamp('2003-10-01 00:00:00'), 232: Timestamp('2004-01-01 00:00:00'), 233: Timestamp('2004-04-01 00:00:00'), 234: Timestamp('2004-07-01 00:00:00'), 235: Timestamp('2004-10-01 00:00:00'), 236: Timestamp('2005-01-01 00:00:00'), 237: Timestamp('2005-04-01 00:00:00'), 238: Timestamp('2005-07-01 00:00:00'), 239: Timestamp('2005-10-01 00:00:00'), 240: Timestamp('2006-01-01 00:00:00'), 241: Timestamp('2006-04-01 00:00:00'), 242: Timestamp('2006-07-01 00:00:00'), 243: Timestamp('2006-10-01 00:00:00'), 244: Timestamp('2007-01-01 00:00:00'), 245: Timestamp('2007-04-01 00:00:00'), 246: Timestamp('2007-07-01 00:00:00'), 247: Timestamp('2007-10-01 00:00:00'), 248: Timestamp('2008-01-01 00:00:00'), 249: Timestamp('2008-04-01 00:00:00'), 250: Timestamp('2008-07-01 00:00:00'), 251: Timestamp('2008-10-01 00:00:00'), 252: Timestamp('2009-01-01 00:00:00'), 253: Timestamp('2009-04-01 00:00:00'), 254: Timestamp('2009-07-01 00:00:00'), 255: Timestamp('2009-10-01 00:00:00'), 256: Timestamp('2010-01-01 00:00:00'), 257: Timestamp('2010-04-01 00:00:00'), 258: Timestamp('2010-07-01 00:00:00'), 259: Timestamp('2010-10-01 00:00:00'), 260: Timestamp('2011-01-01 00:00:00'), 261: Timestamp('2011-04-01 00:00:00'), 262: Timestamp('2011-07-01 00:00:00'), 263: Timestamp('2011-10-01 00:00:00'), 264: Timestamp('2012-01-01 00:00:00'), 265: Timestamp('2012-04-01 00:00:00'), 266: Timestamp('2012-07-01 00:00:00'), 267: Timestamp('2012-10-01 00:00:00'), 268: Timestamp('2013-01-01 00:00:00'), 269: Timestamp('2013-04-01 00:00:00'), 270: Timestamp('2013-07-01 00:00:00'), 271: Timestamp('2013-10-01 00:00:00'), 272: Timestamp('2014-01-01 00:00:00'), 273: Timestamp('2014-04-01 00:00:00'), 274: Timestamp('2014-07-01 00:00:00'), 275: Timestamp('2014-10-01 00:00:00'), 276: Timestamp('2015-01-01 00:00:00'), 277: Timestamp('2015-04-01 00:00:00'), 278: Timestamp('2015-07-01 00:00:00'), 279: Timestamp('2015-10-01 00:00:00'), 280: Timestamp('2016-01-01 00:00:00'), 281: Timestamp('2016-04-01 00:00:00'), 282: Timestamp('2016-07-01 00:00:00'), 283: Timestamp('2016-10-01 00:00:00'), 284: Timestamp('2017-01-01 00:00:00'), 285: Timestamp('2017-04-01 00:00:00'), 286: Timestamp('2017-07-01 00:00:00'), 287: Timestamp('2017-10-01 00:00:00'), 288: Timestamp('2018-01-01 00:00:00'), 289: Timestamp('2018-04-01 00:00:00'), 290: Timestamp('2018-07-01 00:00:00'), 291: Timestamp('2018-10-01 00:00:00'), 292: Timestamp('2019-01-01 00:00:00'), 293: Timestamp('2019-04-01 00:00:00'), 294: Timestamp('2019-07-01 00:00:00'), 295: Timestamp('2019-10-01 00:00:00'), 296: Timestamp('2020-01-01 00:00:00'), 297: Timestamp('2020-04-01 00:00:00'), 298: Timestamp('2020-07-01 00:00:00'), 299: Timestamp('2020-10-01 00:00:00'), 300: Timestamp('2021-01-01 00:00:00'), 301: Timestamp('2021-04-01 00:00:00'), 302: Timestamp('2021-07-01 00:00:00')}, 'Gross Domestic Product': {0: nan, 1: nan, 2: nan, 3: nan, 4: 243.164, 5: 245.968, 6: 249.585, 7: 259.745, 8: 265.742, 9: 272.567, 10: 279.196, 11: 280.366, 12: 275.034, 13: 271.351, 14: 272.889, 15: 270.627, 16: 280.828, 17: 290.383, 18: 308.153, 19: 319.945, 20: 336.0, 21: 344.09, 22: 351.385, 23: 356.178, 24: 359.82, 25: 361.03, 26: 367.701, 27: 380.812, 28: 387.98, 29: 391.749, 30: 391.171, 31: 385.97, 32: 385.345, 33: 386.121, 34: 390.996, 35: 399.734, 36: 413.073, 37: 421.532, 38: 430.221, 39: 437.092, 40: 439.746, 41: 446.01, 42: 451.191, 43: 460.463, 44: 469.779, 45: 472.025, 46: 479.49, 47: 474.864, 48: 467.54, 49: 471.978, 50: 485.841, 51: 499.555, 52: 510.33, 53: 522.653, 54: 525.034, 55: 528.6, 56: 542.648, 57: 541.08, 58: 545.604, 59: 540.197, 60: 545.018, 61: 555.545, 62: 567.664, 63: 580.612, 64: 594.013, 65: 600.366, 66: 609.027, 67: 612.28, 68: 621.672, 69: 629.752, 70: 644.444, 71: 653.938, 72: 669.822, 73: 678.674, 74: 692.031, 75: 697.319, 76: 717.79, 77: 730.191, 78: 749.323, 79: 771.857, 80: 795.734, 81: 804.981, 82: 819.638, 83: 833.302, 84: 844.17, 85: 848.983, 86: 865.233, 87: 881.439, 88: 909.387, 89: 934.344, 90: 950.825, 91: 968.03, 92: 993.337, 93: 1009.02, 94: 1029.956, 95: 1038.147, 96: 1051.2, 97: 1067.375, 98: 1086.059, 99: 1088.608, 100: 1135.156, 101: 1156.271, 102: 1177.675, 103: 1190.297, 104: 1230.609, 105: 1266.369, 106: 1290.566, 107: 1328.904, 108: 1377.49, 109: 1413.887, 110: 1433.838, 111: 1476.289, 112: 1491.209, 113: 1530.056, 114: 1560.026, 115: 1599.679, 116: 1616.116, 117: 1651.853, 118: 1709.82, 119: 1761.831, 120: 1820.487, 121: 1852.332, 122: 1886.558, 123: 1934.273, 124: 1988.648, 125: 2055.909, 126: 2118.473, 127: 2164.27, 128: 2202.76, 129: 2331.633, 130: 2395.053, 131: 2476.949, 132: 2526.61, 133: 2591.247, 134: 2667.565, 135: 2723.883, 136: 2789.842, 137: 2797.352, 138: 2856.483, 139: 2985.557, 140: 3124.206, 141: 3162.532, 142: 3260.609, 143: 3280.818, 144: 3274.302, 145: 3331.972, 146: 3366.322, 147: 3402.561, 148: 3473.413, 149: 3578.848, 150: 3689.179, 151: 3794.706, 152: 3908.054, 153: 4009.601, 154: 4084.25, 155: 4148.551, 156: 4230.168, 157: 4294.887, 158: 4386.773, 159: 4444.094, 160: 4507.894, 161: 4545.34, 162: 4607.669, 163: 4657.627, 164: 4722.156, 165: 4806.16, 166: 4884.555, 167: 5007.994, 168: 5073.372, 169: 5190.036, 170: 5282.835, 171: 5399.509, 172: 5511.253, 173: 5612.463, 174: 5695.365, 175: 5747.237, 176: 5872.701, 177: 5960.028, 178: 6015.116, 179: 6004.733, 180: 6035.178, 181: 6126.862, 182: 6205.937, 183: 6264.54, 184: 6363.102, 185: 6470.763, 186: 6566.641, 187: 6680.803, 188: 6729.459, 189: 6808.939, 190: 6882.098, 191: 7013.738, 192: 7115.652, 193: 7246.931, 194: 7331.075, 195: 7455.288, 196: 7522.289, 197: 7580.997, 198: 7683.125, 199: 7772.586, 200: 7868.468, 201: 8032.84, 202: 8131.408, 203: 8259.771, 204: 8362.655, 205: 8518.825, 206: 8662.823, 207: 8765.907, 208: 8866.48, 209: 8969.699, 210: 9121.097, 211: 9293.991, 212: 9411.682, 213: 9526.21, 214: 9686.626, 215: 9900.169, 216: 10002.179, 217: 10247.72, 218: 10318.165, 219: 10435.744, 220: 10470.231, 221: 10599.0, 222: 10598.02, 223: 10660.465, 224: 10783.5, 225: 10887.46, 226: 10984.04, 227: 11061.433, 228: 11174.129, 229: 11312.766, 230: 11566.669, 231: 11772.234, 232: 11923.447, 233: 12112.815, 234: 12305.307, 235: 12527.214, 236: 12767.286, 237: 12922.656, 238: 13142.642, 239: 13324.204, 240: 13599.16, 241: 13753.424, 242: 13870.188, 243: 14039.56, 244: 14215.651, 245: 14402.082, 246: 14564.117, 247: 14715.058, 248: 14706.538, 249: 14865.701, 250: 14898.999, 251: 14608.208, 252: 14430.901, 253: 14381.236, 254: 14448.882, 255: 14651.248, 256: 14764.611, 257: 14980.193, 258: 15141.605, 259: 15309.471, 260: 15351.444, 261: 15557.535, 262: 15647.681, 263: 15842.267, 264: 16068.824, 265: 16207.13, 266: 16319.54, 267: 16420.386, 268: 16629.05, 269: 16699.551, 270: 16911.068, 271: 17133.114, 272: 17144.281, 273: 17462.703, 274: 17743.227, 275: 17852.54, 276: 17991.348, 277: 18193.707, 278: 18306.96, 279: 18332.079, 280: 18425.306, 281: 18611.617, 282: 18775.459, 283: 18968.041, 284: 19153.912, 285: 19322.92, 286: 19558.693, 287: 19882.965, 288: 20143.716, 289: 20492.492, 290: 20659.102, 291: 20813.325, 292: 21001.591, 293: 21289.268, 294: 21505.012, 295: 21694.458, 296: 21481.367, 297: 19477.444, 298: 21138.574, 299: 21477.597, 300: 22038.226, 301: 22740.959, 302: 23202.344}}

df = pd.DataFrame(data_dict)

当我循环时,我会访问像这样的小子集df.loc[(df['Date'] >= "2019") & (df['Date'] <= "2021")]

编辑:链接的问题报告了一个ValueError,但我没有得到,所以我的问题不是重复的。接受的答案建议使用compat=True,这也是此处解决方案的关键。但这并不意味着问题重复。


阅读 17

收藏
2025-01-09

共1个答案

小能豆

To ensure that the x-axis labels in your plot align with your desired starting point, such as multiples of 10 or 5 (e.g., 1950, 1960, etc.), you can adjust the YearLocator and modify it slightly to force it to round the start year to the nearest multiple of 10 (or whatever your desired interval is). Here’s a solution that provides the flexibility you need, allowing the starting year to be adjusted automatically based on the data range:

Key Points:

  1. Use YearLocator to control the interval between tick marks (e.g., every 10 years).
  2. Use DateFormatter to format the tick labels as years.
  3. Dynamically adjust the start year to a multiple of your desired interval (e.g., nearest 10).

Example Code:

import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.dates import YearLocator, DateFormatter
import numpy as np

# Example data (this can be your actual dataset)
data_dict = {'Date': pd.date_range('1946-01-01', periods=100, freq='Q')}
df = pd.DataFrame(data_dict)

# Adjust the starting year dynamically to the nearest 10
start_year = df['Date'].dt.year.min()
adjusted_start_year = (start_year // 10) * 10  # Round down to the nearest 10

# Create the plot
f, ax = plt.subplots()

# Plot the data
df.plot(ax=ax, x='Date')

# Set the YearLocator to have ticks every 10 years
ax.xaxis.set_major_locator(YearLocator(10, offset=adjusted_start_year - start_year))

# Format the ticks as years
ax.xaxis.set_major_formatter(DateFormatter('%Y'))

# Automatically adjust the layout to prevent clipping of labels
plt.tight_layout()

# Show the plot
plt.show()

Explanation:

  1. Adjusting Start Year: The starting year is calculated using start_year = df['Date'].dt.year.min(). This ensures that you are starting from the earliest year in your dataset. The adjustment to the nearest multiple of 10 is done by (start_year // 10) * 10.
  2. YearLocator: The YearLocator(10, offset=adjusted_start_year - start_year) ensures that the first tick is aligned with the nearest multiple of 10.
  3. DateFormatter: The DateFormatter('%Y') ensures that only the year is displayed in the tick labels.

Result:

This approach automatically adjusts the starting year to a multiple of 10, while keeping the tick frequency every 10 years. If your data range changes, the plot will adjust accordingly, and the start year will always be rounded to the nearest 10.

You can modify the 10 in the YearLocator and adjusted_start_year calculation to control the frequency of the ticks and the rounding behavior (e.g., 5 for multiples of 5 years).

2025-01-09