我的数据集看起来像这样:
{'Berlin': [[1, 333]],
'London': [[1, 111], [2, 555]],
'Paris': [[1, 444], [2, 222], [3, 999]]}
现在我需要为每个城市绘制散点图,例如,巴黎的散点图在x轴上应该是1 2 3,在y轴上应该是444 222 999。
我有一个非常大的数据集,所以我希望每个地块有5个城市。
我的代码看起来像这样,但在输出中我得到了空图。
for town in dictionary_town:
x=[]
y=[]
i+=1
save_plot = 'path'+str(i)+'.png'
for list in town:
x.append(list[0])
y.append(list[1])
plt.style.use('dark_background')
plt.figure()
plt.plot( kind='scatter',x = x, y=y,label=town, s=30,figsize=(50, 20), fontsize=25, alpha=0.5)
plt.ylabel("Population", fontsize=50)
plt.title(str(i), fontsize=50)
legend = plt.legend(fontsize=30, markerscale=0.5, ncol=1)
plt.savefig(save_plot)
plt.close()
发布于 2018-06-16 20:24:15
您可以简单地使用zip
转置您的列表,以便将x, y
对转换为列表:
import matplotlib.pyplot as plt
dictionary_town = {'Berlin': [[1, 333]],
'London': [[1, 111], [2, 555]],
'Paris': [[1, 444], [2, 222], [3, 999]]}
for i, town in enumerate(dictionary_town):
x, y = zip(*dictionary_town[town])
save_plot = 'path'+str(i)+'.png'
plt.style.use('dark_background')
fig = plt.figure(figsize=(50, 20))
plt.scatter(x, y, s=30,label=town, alpha=0.5)
plt.ylabel("Population", fontsize=50)
plt.title(str(i), fontsize=50)
plt.legend(fontsize=30, markerscale=0.5, ncol=1)
plt.savefig(save_plot)
plt.close(fig)
https://stackoverflow.com/questions/50886451
复制相似问题