Python - 绘制列表列表与另一列表的关系
我有一个车站的列表,里面都是车站的名字,像这样:
station_list=[station1, station2, station3, ..., station63]
然后我还有一个列表,里面是每个车站的测量数据,但每个车站的测量数据数量不一样。所以,我有这样的内容:
measure_list=[[200.0, 200.0, 200.0, 200.0, 200.0, 300.0], [400.0, 400.0, 300.0, 300.0, 300.0, 300.0, 300.0, 300.0, 300.0], [300.0, 400.0, 400.0, 400.0, 400.0], ..., [1000.0, 1000.0, 1000.0, 1000.0, 1000.0], [7000.0]]
这个测量列表里有63个“子列表”,每个子列表对应一个车站。
最后,我想画一个图,把车站放在横轴(x轴),测量数据放在纵轴(y轴),这样可以比较所有车站的测量数据。
谢谢你的帮助。(对我的英语不好表示抱歉;)
1 个回答
3
我建议你参考这个例子...
这里是一个改编后的版本,结果如下:
import numpy as np
import matplotlib.pyplot as plt
station_list=['station1', 'station2', 'station3', 'station63']
measure_list=[
[200.0, 200.0, 200.0, 200.0, 200.0, 300.0],
[400.0, 400.0, 300.0, 300.0, 300.0, 300.0, 300.0, 300.0, 300.0],
[300.0, 400.0, 400.0, 400.0, 400.0],
[1000.0, 1000.0, 1000.0, 1000.0, 1000.0],
]
x = range(len(station_list))
assert len(station_list) == len(measure_list) == len(x)
for i, label in enumerate(station_list):
y_list = measure_list[i]
x_list = (x[i],) * len(y_list)
plt.plot(x_list, y_list, 'o')
# You can specify a rotation for the tick labels in degrees or with keywords.
plt.xticks(x, station_list, rotation='vertical')
# Pad margins so that markers don't get clipped by the axes
# plt.margins(0.2)
plt.xlim(np.min(x) - 0.5, np.max(x) + 0.5)
# Tweak spacing to prevent clipping of tick-labels
plt.subplots_adjust(bottom=0.15)
plt.show()
这个代码运行后会得到:
