python - 用 pandas 时间序列绘制 fill_between。值错误
问题描述
我试图从这里复制一个非常简单的例子:https ://python-graph-gallery.com/241-improve-area-chart/
我的数据。
价值观:
x = newgraph['values'].values
[141.65166667 117.97286957 130.14766667 130.444 127.9445
178.01919048 117.025 77.90055556 119.11394444 357.88333333
161.099 61.46366667 126.849 142.43683333 101.7946875
141.2142 292.74857143 102.59154167 85.747125 83.22307692
117.77190909 150.75015789 275.282 148.58395833 110.0824
93.09858824 131.0881 73.98646154 69.183 99.2175
84.78016667 157.7805 171.10766667 170.10535714 140.21433333
257.95166667 142.49668 120.20757143 115.42713913 138.1566
168.06071429 237.249 154.69242857 145.43288889 155.80169231
114.72075 72.18833333 160.4758 93.703 68.94159259
125.05733333 211.1284 129.60669231 81.67755 84.8684
85.923 102.71911364 110.54144 86.9905 138.85572222
146.96868 161.422 194.9203 84.93763636 155.35046667
123.97671429 133.49786667 134.605825 185.846 106.16564706
253.186 73.02047059 131.13469565 75.63633333 167.81266667
83.67825926 99.68016667 147.33016667 90.34742857 134.986
131.1215 181.683 89.59085714 68.14153846 83.3042069 ]
日期索引:
y = newgraph['values'].index
DatetimeIndex(['2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05',
'2020-01-06', '2020-01-07', '2020-01-08', '2020-01-09',
'2020-01-10', '2020-01-11', '2020-01-12', '2020-01-13',
'2020-01-14', '2020-01-15', '2020-01-16', '2020-01-17',
'2020-01-18', '2020-01-20', '2020-01-21', '2020-01-22',
'2020-01-23', '2020-01-24', '2020-01-25', '2020-01-27',
'2020-01-28', '2020-01-29', '2020-01-30', '2020-01-31',
'2020-02-01', '2020-02-02', '2020-02-03', '2020-02-04',
'2020-02-05', '2020-02-06', '2020-02-07', '2020-02-08',
'2020-02-10', '2020-02-11', '2020-02-12', '2020-02-13',
'2020-02-14', '2020-02-16', '2020-02-17', '2020-02-18',
'2020-02-19', '2020-02-20', '2020-02-21', '2020-02-22',
'2020-02-23', '2020-02-24', '2020-02-25', '2020-02-26',
'2020-02-27', '2020-02-28', '2020-02-29', '2020-03-01',
'2020-03-02', '2020-03-03', '2020-03-04', '2020-03-05',
'2020-03-06', '2020-03-07', '2020-03-09', '2020-03-10',
'2020-03-11', '2020-03-12', '2020-03-13', '2020-03-14',
'2020-03-15', '2020-03-16', '2020-03-17', '2020-03-18',
'2020-03-19', '2020-03-20', '2020-03-21', '2020-03-23',
'2020-03-24', '2020-03-25', '2020-03-26', '2020-03-27',
'2020-03-28', '2020-03-29', '2020-03-30', '2020-03-31',
'2020-04-08'],
dtype='datetime64[ns]', name='local_date', freq=None)
我尝试尝试本教程:
plt.fill_between( x, y, color="skyblue", alpha=0.4)
plt.show()
ValueError: view limit minimum -36876.15 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units
有没有办法在时间序列的线下填写?还是这只适用于 y 轴上的数字?
我只是想复制那些在时间序列线下有令人愉悦的阴影的股市图表。
解决方案
进口
import pandas as pd
import matplotlib.pyplot as plt
修复数据
价值观
- 使用提供的数据,制作一个
str
,用空格分隔值,然后过滤掉空白以创建一个列表。
values = "141.65166667 117.97286957 130.14766667 130.444 127.9445 \
178.01919048 117.025 77.90055556 119.11394444 357.88333333 \
161.099 61.46366667 126.849 142.43683333 101.7946875 \
141.2142 292.74857143 102.59154167 85.747125 83.22307692 \
117.77190909 150.75015789 275.282 148.58395833 110.0824 \
93.09858824 131.0881 73.98646154 69.183 99.2175 \
84.78016667 157.7805 171.10766667 170.10535714 140.21433333 \
257.95166667 142.49668 120.20757143 115.42713913 138.1566 \
168.06071429 237.249 154.69242857 145.43288889 155.80169231 \
114.72075 72.18833333 160.4758 93.703 68.94159259 \
125.05733333 211.1284 129.60669231 81.67755 84.8684 \
85.923 102.71911364 110.54144 86.9905 138.85572222 \
146.96868 161.422 194.9203 84.93763636 155.35046667 \
123.97671429 133.49786667 134.605825 185.846 106.16564706 \
253.186 73.02047059 131.13469565 75.63633333 167.81266667 \
83.67825926 99.68016667 147.33016667 90.34742857 134.986 \
131.1215 181.683 89.59085714 68.14153846 83.3042069"
values = values.split(' ')
values = list(filter(lambda x: x != "", values))
print(values[:5])
>>> ['141.65166667', '117.97286957', '130.14766667', '130.444', '127.9445']
日期
dates = ['2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05',
'2020-01-06', '2020-01-07', '2020-01-08', '2020-01-09',
'2020-01-10', '2020-01-11', '2020-01-12', '2020-01-13',
'2020-01-14', '2020-01-15', '2020-01-16', '2020-01-17',
'2020-01-18', '2020-01-20', '2020-01-21', '2020-01-22',
'2020-01-23', '2020-01-24', '2020-01-25', '2020-01-27',
'2020-01-28', '2020-01-29', '2020-01-30', '2020-01-31',
'2020-02-01', '2020-02-02', '2020-02-03', '2020-02-04',
'2020-02-05', '2020-02-06', '2020-02-07', '2020-02-08',
'2020-02-10', '2020-02-11', '2020-02-12', '2020-02-13',
'2020-02-14', '2020-02-16', '2020-02-17', '2020-02-18',
'2020-02-19', '2020-02-20', '2020-02-21', '2020-02-22',
'2020-02-23', '2020-02-24', '2020-02-25', '2020-02-26',
'2020-02-27', '2020-02-28', '2020-02-29', '2020-03-01',
'2020-03-02', '2020-03-03', '2020-03-04', '2020-03-05',
'2020-03-06', '2020-03-07', '2020-03-09', '2020-03-10',
'2020-03-11', '2020-03-12', '2020-03-13', '2020-03-14',
'2020-03-15', '2020-03-16', '2020-03-17', '2020-03-18',
'2020-03-19', '2020-03-20', '2020-03-21', '2020-03-23',
'2020-03-24', '2020-03-25', '2020-03-26', '2020-03-27',
'2020-03-28', '2020-03-29', '2020-03-30', '2020-03-31',
'2020-04-08']
数据框
# combine dates and values
data = list(zip(dates, values))
# create the DateFrame
df = pd.DataFrame(data, columns=['date', 'value'])
# convert date to datetime and value to float
df['date'] = pd.to_datetime(df['date'])
df['value'] = df['value'].astype('float')
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 85 entries, 0 to 84
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 date 85 non-null datetime64[ns]
1 value 85 non-null float64
dtypes: datetime64[ns](1), float64(1)
# df.head()
date value
2020-01-02 141.651667
2020-01-03 117.972870
2020-01-04 130.147667
2020-01-05 130.444000
2020-01-06 127.944500
阴谋
plt.fill_between(df['date'], df['value'], color="skyblue", alpha=0.4)
plt.xticks(rotation=45)
plt.show()
推荐阅读
- linux - 如何在没有 Auditd 的情况下找到在 Linux 中访问或修改文件的用户的 UID / GID?
- flutter - 在颤振测试中模拟 firebase user.reload()
- excel - DB PLSQL 发送电子表格
- javascript - Firebase 规则在模拟中传递,但在前端的相同操作中不传递
- java - 传播 AutoClosable 包装器的检查异常
- python - 装饰器和变量范围
- javascript - 烧瓶插座 | 使用 Flask 执行器从后台任务向 Html 脚本发送 pandas 数据帧
- php - 其他会话正在工作时不显示会话消息
- html - 十六进制代码不适用于 HTML 中的小于“<”符号,除非它旁边有空格
- google-cloud-platform - 如何通过 java SDK 在 GCP 中创建项目?