MATPLOTLIB - 增加绘图比例

时间:2017-01-11 16:43:11

标签: python matplotlib plot

我正在研究1996年至2015年龙卷风数据的分析。我将龙卷风的数量与各州的总财产损失进行比较。

我有一个基本的情节,但较小的数字紧密地聚集在一起

Tornado Plot

我想知道的是我如何扩展它以便将它们分散一点?

我的剧情代码是

count = tonadoes_1996_damage['count']
loss = tonadoes_1996_damage['total_loss']

max_tornado_count = tonadoes_1996_damage['count'].max()
max_tornado_cnt_label = tonadoes_1996_damage[tonadoes_1996_damage['count'] == max_tornado_count].index.tolist()[0]
max_tornado_cnt_x = tonadoes_1996_damage[tonadoes_1996_damage['count'] == max_tornado_count]['count']
max_tornado_cnt_y = tonadoes_1996_damage[tonadoes_1996_damage['count'] == max_tornado_count]['total_loss']

max_tornado_loss = tonadoes_1996_damage['total_loss'].max()
max_tornado_loss_label = tonadoes_1996_damage[tonadoes_1996_damage['total_loss'] == max_tornado_loss].index.tolist()[0]
max_tornado_loss_x = tonadoes_1996_damage[tonadoes_1996_damage['total_loss'] == max_tornado_loss]['count']
max_tornado_loss_y = tonadoes_1996_damage[tonadoes_1996_damage['total_loss'] == max_tornado_loss]['total_loss']

colors = np.random.rand(51)
area = count
plt.scatter(count, loss,s=area,c=colors,alpha=.5)

xlab = "Number of Tornadoes [in thousands]"
ylab = "Total Loss [in million USD]"
title = "Total Property Loss Since 1996"

plt.xlabel(xlab)
plt.xlim(0, 3500)

plt.ylabel(ylab)
plt.ylim(0, 6000)

plt.title(title)
plt.grid(True)

plt.text(max_tornado_cnt_x, max_tornado_cnt_y, max_tornado_cnt_label)
plt.text(max_tornado_loss_x, max_tornado_loss_y, max_tornado_loss_label)

plt.show()

我已经玩过设置x-ticks和y-ticks,但那些似乎没有调整比例,只是添加更多的刻度(除非我做错了。

1 个答案:

答案 0 :(得分:1)

您可以使用x轴的对数比例,这将改善可视化:http://matplotlib.org/examples/pylab_examples/log_demo.html

尝试类似:

fig = plt.figure(figsize=(11, 8))
ax = fig.add_subplot(1, 1, 1)
ax.set_xscale('log')
plt.scatter(count, loss,s=area,c=colors,alpha=.5)

然后相应地添加所有属性。