Python3 : matplotlib. pyplot.plot_date() 日期标签溢出

标签 python python-3.x matplotlib

我正在绘制以下数据:

myDates = ['2017-05-17', '2017-05-24', '2017-05-25', '2017-05-26', '2017-05-27', '2017-05-29', '2017-05-31', '2017-06-01', '2017-06-03', '2017-06-04', '2017-06-05', '2017-06-06', '2017-06-07', '2017-06-08', '2017-06-10', '2017-06-11', '2017-06-12', '2017-06-13', '2017-06-15', '2017-06-16', '2017-06-17', '2017-06-18', '2017-06-19', '2017-06-20', '2017-06-21', '2017-06-22', '2017-06-23', '2017-06-24', '2017-06-25', '2017-06-26', '2017-06-27', '2017-06-28', '2017-06-29', '2017-06-30', '2017-07-01', '2017-07-02', '2017-07-04', '2017-07-05', '2017-07-07', '2017-07-08', '2017-07-10', '2017-07-13', '2017-07-15', '2017-07-16', '2017-07-17', '2017-07-18', '2017-07-19', '2017-07-20', '2017-07-23', '2017-07-24', '2017-07-25', '2017-07-27', '2017-07-28', '2017-07-29', '2017-07-31', '2017-08-01', '2017-08-02', '2017-08-03', '2017-08-04', '2017-08-06', '2017-08-07', '2017-08-08', '2017-08-09', '2017-08-10', '2017-08-11', '2017-08-12', '2017-08-14', '2017-08-15', '2017-08-16', '2017-08-17', '2017-08-18', '2017-08-19', '2017-08-20', '2017-08-21', '2017-08-22', '2017-08-23', '2017-08-24', '2017-08-25', '2017-08-26', '2017-08-27', '2017-08-28', '2017-08-29', '2017-08-30', '2017-08-31', '2017-09-01', '2017-09-02', '2017-09-03', '2017-09-04', '2017-09-05', '2017-09-06', '2017-09-07', '2017-09-08', '2017-09-10', '2017-09-11', '2017-09-12', '2017-09-13', '2017-09-14', '2017-09-15', '2017-09-16', '2017-09-17', '2017-09-18', '2017-09-19', '2017-09-20', '2017-09-21', '2017-09-22', '2017-09-23', '2017-09-24', '2017-09-25', '2017-09-26', '2017-09-27', '2017-09-28', '2017-09-29', '2017-09-30', '2017-10-01', '2017-10-02', '2017-10-03', '2017-10-04', '2017-10-05', '2017-10-06', '2017-10-07', '2017-10-08', '2017-10-09', '2017-10-10', '2017-10-12', '2017-10-13', '2017-10-14', '2017-10-15', '2017-10-16', '2017-10-18', '2017-10-19', '2017-10-20', '2017-10-21', '2017-10-22', '2017-10-23', '2017-10-25', '2017-10-26', '2017-10-27', '2017-10-28', '2017-10-30', '2017-10-31', '2017-11-01', '2017-11-03', '2017-11-04', '2017-11-05', '2017-11-08', '2017-11-09', '2017-11-10', '2017-11-11', '2017-11-12', '2017-11-13', '2017-11-14', '2017-11-15', '2017-11-18', '2017-11-19', '2017-11-20', '2017-11-21', '2017-11-22', '2017-11-23', '2017-11-24', '2017-11-25', '2017-11-26', '2017-11-27', '2017-11-28', '2017-11-29', '2017-11-30', '2017-12-01', '2017-12-02', '2017-12-03', '2017-12-04', '2017-12-05', '2017-12-06', '2017-12-07', '2017-12-08', '2017-12-09', '2017-12-10', '2017-12-11', '2017-12-12', '2017-12-13', '2017-12-14', '2017-12-15', '2017-12-16', '2017-12-17', '2017-12-18', '2017-12-19', '2017-12-20', '2017-12-21', '2017-12-22', '2017-12-23', '2017-12-24', '2017-12-25', '2017-12-26', '2017-12-27', '2017-12-28', '2017-12-29', '2017-12-30', '2017-12-31', '2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05', '2018-01-06', '2018-01-07', '2018-01-08', '2018-01-09', '2018-01-10', '2018-01-11', '2018-01-12', '2018-01-13', '2018-01-14', '2018-01-15', '2018-01-16', '2018-01-17', '2018-01-18', '2018-01-19', '2018-01-20', '2018-01-21', '2018-01-22', '2018-01-23', '2018-01-24', '2018-01-25', '2018-01-26', '2018-01-27', '2018-01-28', '2018-01-29', '2018-01-30', '2018-01-31', '2018-02-01', '2018-02-02', '2018-02-03', '2018-02-04', '2018-02-05', '2018-02-06', '2018-02-07', '2018-02-08', '2018-02-09', '2018-02-10', '2018-02-11', '2018-02-12', '2018-02-13', '2018-02-14', '2018-02-15', '2018-02-17', '2018-02-18', '2018-02-19', '2018-02-20', '2018-02-21', '2018-02-22', '2018-02-23', '2018-02-24', '2018-02-25', '2018-02-26', '2018-02-27', '2018-02-28', '2018-03-01', '2018-03-02', '2018-03-03', '2018-03-04', '2018-03-05', '2018-03-06', '2018-03-07', '2018-03-08', '2018-03-09', '2018-03-11', '2018-03-13', '2018-03-14', '2018-03-15', '2018-03-16', '2018-03-17', '2018-03-18', '2018-03-19', '2018-03-20', '2018-03-21']

myValues = [100, 101, 105, 106, 109, 110, 111, 165, 189, 193, 195, 196, 203, 210, 211, 214, 223, 224, 225, 226, 230, 231, 240, 249, 257, 266, 270, 273, 274, 282, 285, 291, 298, 300, 301, 303, 304, 306, 307, 308, 310, 311, 314, 315, 320, 322, 325, 328, 330, 342, 343, 346, 348, 349, 373, 380, 387, 391, 392, 395, 397, 401, 403, 408, 414, 420, 421, 422, 475, 477, 481, 484, 487, 490, 492, 500, 503, 504, 510, 511, 512, 515, 520, 521, 524, 526, 529, 530, 531, 534, 538, 541, 548, 553, 561, 565, 569, 574, 581, 585, 588, 592, 593, 597, 599, 602, 605, 608, 612, 615, 617, 624, 626, 631, 635, 638, 640, 644, 649, 652, 654, 656, 663, 665, 667, 676, 682, 684, 685, 686, 688, 691, 692, 708, 711, 712, 713, 714, 716, 717, 718, 719, 749, 755, 757, 758, 761, 762, 763, 764, 771, 772, 774, 778, 781, 788, 821, 835, 840, 848, 855, 861, 869, 884, 894, 913, 919, 928, 933, 940, 946, 949, 957, 965, 974, 984, 989, 996, 1001, 1055, 1057, 1059, 1067, 1079, 1083, 1092, 1101, 1109, 1112, 1116, 1121, 1138, 1159, 1174, 1192, 1204, 1222, 1230, 1253, 1264, 1270, 1277, 1287, 1293, 1298, 1301, 1313, 1326, 1330, 1336, 1354, 1361, 1367, 1376, 1404, 1405, 1437, 1465, 1479, 1533, 1553, 1571, 1584, 1600, 1624, 1631, 1643, 1663, 1672, 1679, 1690, 1696, 1707, 1712, 1716, 1725, 1728, 1733, 1739, 1740, 1741, 1750, 1755, 1762, 1764, 1788, 1792, 1800, 1811, 1815, 1822, 1829, 1836, 1842, 1843, 1849, 1850, 1851, 1855, 1858, 1862, 1863, 1865, 1867, 1869, 1871, 1872, 1874, 1875, 1876, 1878, 1882, 1885]

myDates 是日期列表(使用datetime 格式化)。 myValues 是一个整数列表。我正在制作以下情节:

import matplotlib.pyplot as plt
plt.plot_date(x = myDates, y = myValues)
plt.xlabel('Dates')
plt.ylabel('Cumulative values')
plt.show()

但是,x 轴中的标签不清晰可见。如何仅对年月值进行勾选?具体来说,我想要以下刻度:2017-05、2017-06、2017-07、...、2018-02、2018-03。如果可能的话,我希望文本呈一定角度,这样它就不会出现在下一个刻度标签的顶部。

最佳答案

您可以采取偷懒的方式并使用 pandas.to_datetime将 x 轴值转换为日期时间(而不是字符串),并让 matplotlib 担心使用 fig.autofmt_xdate 获得更好的间距/旋转`

import matplotlib.pyplot as plt
import pandas as pd

myDates = ['2017-05-17', '2017-05-24', '2017-05-25', '2017-05-26', '2017-05-27', '2017-05-29', '2017-05-31', '2017-06-01', '2017-06-03', '2017-06-04', '2017-06-05', '2017-06-06', '2017-06-07', '2017-06-08', '2017-06-10', '2017-06-11', '2017-06-12', '2017-06-13', '2017-06-15', '2017-06-16', '2017-06-17', '2017-06-18', '2017-06-19', '2017-06-20', '2017-06-21', '2017-06-22', '2017-06-23', '2017-06-24', '2017-06-25', '2017-06-26', '2017-06-27', '2017-06-28', '2017-06-29', '2017-06-30', '2017-07-01', '2017-07-02', '2017-07-04', '2017-07-05', '2017-07-07', '2017-07-08', '2017-07-10', '2017-07-13', '2017-07-15', '2017-07-16', '2017-07-17', '2017-07-18', '2017-07-19', '2017-07-20', '2017-07-23', '2017-07-24', '2017-07-25', '2017-07-27', '2017-07-28', '2017-07-29', '2017-07-31', '2017-08-01', '2017-08-02', '2017-08-03', '2017-08-04', '2017-08-06', '2017-08-07', '2017-08-08', '2017-08-09', '2017-08-10', '2017-08-11', '2017-08-12', '2017-08-14', '2017-08-15', '2017-08-16', '2017-08-17', '2017-08-18', '2017-08-19', '2017-08-20', '2017-08-21', '2017-08-22', '2017-08-23', '2017-08-24', '2017-08-25', '2017-08-26', '2017-08-27', '2017-08-28', '2017-08-29', '2017-08-30', '2017-08-31', '2017-09-01', '2017-09-02', '2017-09-03', '2017-09-04', '2017-09-05', '2017-09-06', '2017-09-07', '2017-09-08', '2017-09-10', '2017-09-11', '2017-09-12', '2017-09-13', '2017-09-14', '2017-09-15', '2017-09-16', '2017-09-17', '2017-09-18', '2017-09-19', '2017-09-20', '2017-09-21', '2017-09-22', '2017-09-23', '2017-09-24', '2017-09-25', '2017-09-26', '2017-09-27', '2017-09-28', '2017-09-29', '2017-09-30', '2017-10-01', '2017-10-02', '2017-10-03', '2017-10-04', '2017-10-05', '2017-10-06', '2017-10-07', '2017-10-08', '2017-10-09', '2017-10-10', '2017-10-12', '2017-10-13', '2017-10-14', '2017-10-15', '2017-10-16', '2017-10-18', '2017-10-19', '2017-10-20', '2017-10-21', '2017-10-22', '2017-10-23', '2017-10-25', '2017-10-26', '2017-10-27', '2017-10-28', '2017-10-30', '2017-10-31', '2017-11-01', '2017-11-03', '2017-11-04', '2017-11-05', '2017-11-08', '2017-11-09', '2017-11-10', '2017-11-11', '2017-11-12', '2017-11-13', '2017-11-14', '2017-11-15', '2017-11-18', '2017-11-19', '2017-11-20', '2017-11-21', '2017-11-22', '2017-11-23', '2017-11-24', '2017-11-25', '2017-11-26', '2017-11-27', '2017-11-28', '2017-11-29', '2017-11-30', '2017-12-01', '2017-12-02', '2017-12-03', '2017-12-04', '2017-12-05', '2017-12-06', '2017-12-07', '2017-12-08', '2017-12-09', '2017-12-10', '2017-12-11', '2017-12-12', '2017-12-13', '2017-12-14', '2017-12-15', '2017-12-16', '2017-12-17', '2017-12-18', '2017-12-19', '2017-12-20', '2017-12-21', '2017-12-22', '2017-12-23', '2017-12-24', '2017-12-25', '2017-12-26', '2017-12-27', '2017-12-28', '2017-12-29', '2017-12-30', '2017-12-31', '2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05', '2018-01-06', '2018-01-07', '2018-01-08', '2018-01-09', '2018-01-10', '2018-01-11', '2018-01-12', '2018-01-13', '2018-01-14', '2018-01-15', '2018-01-16', '2018-01-17', '2018-01-18', '2018-01-19', '2018-01-20', '2018-01-21', '2018-01-22', '2018-01-23', '2018-01-24', '2018-01-25', '2018-01-26', '2018-01-27', '2018-01-28', '2018-01-29', '2018-01-30', '2018-01-31', '2018-02-01', '2018-02-02', '2018-02-03', '2018-02-04', '2018-02-05', '2018-02-06', '2018-02-07', '2018-02-08', '2018-02-09', '2018-02-10', '2018-02-11', '2018-02-12', '2018-02-13', '2018-02-14', '2018-02-15', '2018-02-17', '2018-02-18', '2018-02-19', '2018-02-20', '2018-02-21', '2018-02-22', '2018-02-23', '2018-02-24', '2018-02-25', '2018-02-26', '2018-02-27', '2018-02-28', '2018-03-01', '2018-03-02', '2018-03-03', '2018-03-04', '2018-03-05', '2018-03-06', '2018-03-07', '2018-03-08', '2018-03-09', '2018-03-11', '2018-03-13', '2018-03-14', '2018-03-15', '2018-03-16', '2018-03-17', '2018-03-18', '2018-03-19', '2018-03-20', '2018-03-21']

myValues = [100, 101, 105, 106, 109, 110, 111, 165, 189, 193, 195, 196, 203, 210, 211, 214, 223, 224, 225, 226, 230, 231, 240, 249, 257, 266, 270, 273, 274, 282, 285, 291, 298, 300, 301, 303, 304, 306, 307, 308, 310, 311, 314, 315, 320, 322, 325, 328, 330, 342, 343, 346, 348, 349, 373, 380, 387, 391, 392, 395, 397, 401, 403, 408, 414, 420, 421, 422, 475, 477, 481, 484, 487, 490, 492, 500, 503, 504, 510, 511, 512, 515, 520, 521, 524, 526, 529, 530, 531, 534, 538, 541, 548, 553, 561, 565, 569, 574, 581, 585, 588, 592, 593, 597, 599, 602, 605, 608, 612, 615, 617, 624, 626, 631, 635, 638, 640, 644, 649, 652, 654, 656, 663, 665, 667, 676, 682, 684, 685, 686, 688, 691, 692, 708, 711, 712, 713, 714, 716, 717, 718, 719, 749, 755, 757, 758, 761, 762, 763, 764, 771, 772, 774, 778, 781, 788, 821, 835, 840, 848, 855, 861, 869, 884, 894, 913, 919, 928, 933, 940, 946, 949, 957, 965, 974, 984, 989, 996, 1001, 1055, 1057, 1059, 1067, 1079, 1083, 1092, 1101, 1109, 1112, 1116, 1121, 1138, 1159, 1174, 1192, 1204, 1222, 1230, 1253, 1264, 1270, 1277, 1287, 1293, 1298, 1301, 1313, 1326, 1330, 1336, 1354, 1361, 1367, 1376, 1404, 1405, 1437, 1465, 1479, 1533, 1553, 1571, 1584, 1600, 1624, 1631, 1643, 1663, 1672, 1679, 1690, 1696, 1707, 1712, 1716, 1725, 1728, 1733, 1739, 1740, 1741, 1750, 1755, 1762, 1764, 1788, 1792, 1800, 1811, 1815, 1822, 1829, 1836, 1842, 1843, 1849, 1850, 1851, 1855, 1858, 1862, 1863, 1865, 1867, 1869, 1871, 1872, 1874, 1875, 1876, 1878, 1882, 1885]

fig, ax = plt.subplots()
plt.xlabel('Dates')
plt.ylabel('Cumulative values')
ax.plot_date(x = pd.to_datetime(myDates), y = myValues)
fig.autofmt_xdate() 
plt.show()

如果您想完全控制 xticksxticklabels 的数量、位置和精确旋转,您需要使用 ax.set_xticklabels 正如其他答案所建议的。

关于Python3 : matplotlib. pyplot.plot_date() 日期标签溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49427683/

相关文章:

python - matplotlib:相对于其他轴放置轴,自动更新

python - 保存许多不同长度的数组

python - 类型错误 : first argument must be string or compiled pattern

python - python 3.x 中的递归 is_prime 函数

python - 直接保存时,Matplotlib 无法正确渲染曲线下的渐变

python - 如何使用 matplotlibs 颜色图 alpha 值使三色图淡入透明?

python - Tkinter FileTypes(askopenfilename),如何添加文件夹选项

python - 在 linux 中导入错误,但在 windows 中导入成功

python - 在索引地理数据框时维护地理结构

python-3.x - 使用谷歌分析 api 检查有多少 session 低于 10 秒?