我有一个看起来像这样的数据框:
Season Dist
0 '14 - '15 [120, 128, 175, 474, 615]
1 '15 - '16 [51, 305, 398, 839, 991, 1093, 1304]
2 '16 - '17 [223, 293, 404, 588, 661, 706, 964, 1049, 1206]
3 '17 - '18 [12, 37, 204, 229, 276, 349, 809, 845, 1072, 1...
4 '18 - '19 [210, 214, 259, 383, 652, 798, 1150]
5 '19 - '20 [182, 206, 221, 282, 283, 297, 1330, 1332]
我正在尝试使用matplotlib对其进行绘制,其中x轴是实例的范围,并且在y轴上的每个季节,该图都显示df['Dist']
的分布。我在下面绘制了一个非常糟糕的图表来说明我的观点。
有人知道我该怎么做吗?
答案 0 :(得分:1)
将每个列表分别绘制在同一张图上。列表值将用作x坐标,因此对于y坐标,将每个季节值映射到整数。即类似
Season Dist
0 0 [120, 128, 175, 474, 615]
1 1 [51, 305, 398, 839, 991, 1093, 1304]
2 ' 2 [223, 293, 404, 588, 661, 706, 964, 1049, 1206]
现在散点图将需要每个x坐标的y坐标。
因此,创建类似这样的
y x
[0,0,0,0,0] [120, 128, 175, 474, 615]
[1,1,1,1,1,1,1] [51, 305, 398, 839, 991, 1093, 1304]
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df = pd.DataFrame({'Season':['14 - 15','15 - 16','16 - 17'],'Dist':\
[[120, 128, 175, 474, 615],\
[51, 305, 398, 839, 991, 1093, 1304],\
[223, 293, 404, 588, 661, 706, 964, 1049, 1206]]})
y = np.arange(len(df)) #map the seasons
for i in range(len(df)):
plt.scatter(df['Dist'][i],[y[i] for j in range(len(df['Dist'][i]))]) #create a list of y coordinates for every x coordinate
plt.yticks(y,df['Season']) #show the actual seasons as xticks
plt.show()