使用pyplot在熊猫的时间序列图中添加透明垂直区域

时间:2018-08-23 16:38:14

标签: python-3.x pandas matplotlib

我想在SP500索引时间序列生成的线图中显示衰退期。在我的示例中,感兴趣的衰退为2:

1)2001年3月至2001年11月 2)2007年6月6日至2009年6月

我希望能够控制应该没有边框的区域的颜色和不透明度。对于可重现的示例:

toy_data.to_json()
'{"886204800000":963.36,"888624000000":1023.74,"891302400000":1076.83,"893894400000":1112.2,"896572800000":1108.42,"899164800000":1108.39,"901843200000":1156.58,"904521600000":1074.62,"907113600000":1020.64,"909792000000":1032.47,"912384000000":1144.43,"915062400000":1190.05,"917740800000":1248.77,"920160000000":1246.58,"922838400000":1281.66,"925430400000":1334.76,"928108800000":1332.07,"930700800000":1322.55,"933379200000":1380.99,"936057600000":1327.49,"938649600000":1318.17,"941328000000":1300.01,"943920000000":1391.0,"946598400000":1428.68,"949276800000":1425.59,"951782400000":1388.87,"954460800000":1442.21,"957052800000":1461.36,"959731200000":1418.48,"962323200000":1461.96,"965001600000":1473.0,"967680000000":1485.46,"970272000000":1468.05,"972950400000":1390.14,"975542400000":1378.04,"978220800000":1330.93,"980899200000":1335.63,"983318400000":1305.75,"985996800000":1185.85,"988588800000":1189.84,"991267200000":1270.37,"993859200000":1238.71,"996537600000":1204.45,"999216000000":1178.5,"1001808000000":1044.64,"1004486400000":1076.59,"1007078400000":1129.68,"1009756800000":1144.93,"1012435200000":1140.21,"1014854400000":1100.67,"1017532800000":1153.79,"1020124800000":1111.93,"1022803200000":1079.25,"1025395200000":1014.02,"1028073600000":903.59,"1030752000000":912.55,"1033344000000":867.81,"1036022400000":854.63,"1038614400000":909.93,"1041292800000":899.18,"1043971200000":895.84,"1046390400000":837.03,"1049068800000":846.63,"1051660800000":890.03,"1054339200000":935.96,"1056931200000":988.0,"1059609600000":992.54,"1062288000000":989.53,"1064880000000":1019.44,"1067558400000":1038.73,"1070150400000":1049.9,"1072828800000":1080.64,"1075507200000":1132.52,"1078012800000":1143.36,"1080691200000":1123.98,"1083283200000":1133.36,"1085961600000":1102.78,"1088553600000":1132.76,"1091232000000":1105.85,"1093910400000":1088.94,"1096502400000":1117.66,"1099180800000":1117.21,"1101772800000":1168.94,"1104451200000":1199.21,"1107129600000":1181.41,"1109548800000":1199.63,"1112227200000":1194.9,"1114819200000":1164.43,"1117497600000":1178.28,"1120089600000":1202.25,"1122768000000":1222.24,"1125446400000":1224.27,"1128038400000":1225.92,"1130716800000":1191.96,"1133308800000":1237.37,"1135987200000":1262.07,"1138665600000":1278.73,"1141084800000":1276.65,"1143763200000":1293.74,"1146355200000":1302.17,"1149033600000":1290.01,"1151625600000":1253.17,"1154304000000":1260.24,"1156982400000":1287.15,"1159574400000":1317.74,"1162252800000":1363.38,"1164844800000":1388.64,"1167523200000":1416.42,"1170201600000":1424.16,"1172620800000":1444.8,"1175299200000":1406.95,"1177891200000":1463.64,"1180569600000":1511.14,"1183161600000":1514.19,"1185840000000":1520.71,"1188518400000":1454.62,"1191110400000":1497.12,"1193788800000":1539.66,"1196380800000":1463.39,"1199059200000":1479.22,"1201737600000":1378.76,"1204243200000":1354.87,"1206921600000":1316.94,"1209513600000":1370.47,"1212192000000":1403.22,"1214784000000":1341.25,"1217462400000":1257.33,"1220140800000":1281.47,"1222732800000":1216.95,"1225411200000":968.8,"1228003200000":883.04,"1230681600000":877.56,"1233360000000":865.58,"1235779200000":805.23,"1238457600000":757.13,"1241049600000":848.15,"1243728000000":902.41,"1246320000000":926.12,"1248998400000":935.82,"1251676800000":1009.73,"1254268800000":1044.55,"1256947200000":1067.66,"1259539200000":1088.07,"1262217600000":1110.38}'

toy_data.plot(kind = 'line')

我想要以下种类的东西:

enter image description here

您的建议将不胜感激。

0 个答案:

没有答案