我在两个matplotlib.pyplot
hexbin
图之间创建差异图时遇到了问题,这意味着首先获取每个对应hexbin
的值差异,然后创建差异{{1地图。
在这里给出一个简单的问题示例,假设地图1中一个hexbin
的值为3,地图2中相应hexbin
的值为2,我想要的是做的是首先获得差异3 - 2 = 1然后将其绘制在一个新的六边形图中,即差异图,与地图1和地图2位于相同的位置。
我的输入代码和输出图如下。有谁能请给我一个解决这个问题的方法?
谢谢你的时间!
hexbin
In [1]: plt.hexbin(lon_origin_df, lat_origin_df)
Out[1]: <matplotlib.collections.PolyCollection at 0x13ff40610>
答案 0 :(得分:2)
可以使用h=hexbin()
从h.get_values()
获取值,并使用h.set_values()
设置值,这样您就可以创建新的hexbin
并将其值设置为其他两个之间的区别。例如:
import numpy as np
import matplotlib.pylab as pl
x = np.random.random(200)
y1 = np.random.random(200)
y2 = np.random.random(200)
pl.figure()
pl.subplot(131)
h1=pl.hexbin(x, y1, gridsize=3, vmin=0, vmax=40, cmap=pl.cm.RdBu_r)
pl.colorbar()
pl.subplot(132)
h2=pl.hexbin(x, y2, gridsize=3, vmin=0, vmax=40, cmap=pl.cm.RdBu_r)
pl.colorbar()
pl.subplot(133)
# Create dummy hexbin using whatever data..:
h3=pl.hexbin(x, y2, gridsize=3, vmin=-10, vmax=10, cmap=pl.cm.RdBu_r)
h3.set_array(h1.get_array()-h2.get_array())
pl.colorbar()