import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt
from datetime import datetime
import numpy as np
path = r'C:\Users\bossd\OneDrive\Документы\datarn.csv'
df = pd.read_csv(path)
path2 = r'C:\Users\bossd\OneDrive\Документы\pipirka.csv'
df2 = pd.read_csv(path2)
x = (df2.loc[df2['timestamp'].str.startswith('2015')])
y = df2['cnt']
plt.scatter(x,y)
plt.show()
我想使用包含“2015”作为 x 轴和“cnt”参数的日期构建散点图,这意味着这一天有自行车出租。 但运行代码后我收到此错误
Cell In[47], line 14
12 x = (df2.loc[df2['timestamp'].str.startswith('2015')])
13 y = df2['cnt']
---> 14 plt.scatter(x,y)
15 plt.show()
17 display(df2)
...
File ~\venv\lib\site-packages\matplotlib\category.py:214, in UnitData.update(self, data)
212 # check if convertible to number:
213 convertible = True
--> 214 for val in OrderedDict.fromkeys(data):
215 # OrderedDict just iterates over unique values in data.
216 _api.check_isinstance((str, bytes), value=val)
217 if convertible:
218 # this will only be called so long as convertible is True.
TypeError: unhashable type: 'numpy.ndarray'
数据帧如下所示,包含时间戳作为日期和 cnt 作为当天自行车租赁的数量
data = {'timestamp': ['2015-01-04', '2015-01-05', '2015-01-06', '2015-01-07', '2015-01-08', '2016-12-27', '2016-12-28', '2016-12-29', '2016-12-30', '2016-12-31'],
'cnt': [9234, 20372, 20613, 21064, 15601, 10842, 12428, 14052, 11566, 11424]}
df2 = pd.DataFrame(data)
timestamp cnt
0 2015-01-04 9234
1 2015-01-05 20372
2 2015-01-06 20613
3 2015-01-07 21064
4 2015-01-08 15601
5 2016-12-27 10842
6 2016-12-28 12428
7 2016-12-29 14052
8 2016-12-30 11566
9 2016-12-31 11424
最佳答案
- 首先应使用
pd.to_datetime
将'timestamp'
列转换为日期时间数据类型,否则日期时间 x 刻度将无法正确定位和格式化。- 典型的流程应从清理数据开始,然后进行选择。
x = (df2.loc[df2['timestamp'].str.startswith('2015')])
是错误的原因,因为它选择整个数据帧,而不是数据框的单列。并且未选择所需年份的df2['cnt']
。pandas.DataFrame.plot
使用matplotlib
作为默认绘图后端,应用于绘制数据帧。
# load the sample dataframe from the OP
# convert timestamp to a datetime dtype
df2.timestamp = pd.to_datetime(df2.timestamp)
# select the data by year
df_2015 = df2[df2.timestamp.dt.year.eq(2015)]
# directly plot the dataframe, which uses matplotlib as the back end
ax = df_2015.plot(x='timestamp', marker='.', ls='')
- x 刻度和标签的格式取决于数据范围,可以通过以下答案进行更改
- x 轴限制可以设置如下:
关于python - 使用日期和 int 值构建散点图的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77090789/