我有这个非常简单的代码,它绘制了一个由 100 个点 (10,10) 组成的列表,这些点都是相同的。不幸的是,我收到一条警告和一张空白图表。
我的代码:
import matplotlib.pyplot as plt
mylist = list()
for i in range(100):
mylist.append(10)
def plot():
plt.subplot(111)
plt.hexbin(mylist,mylist,bins='log', cmap=plt.cm.YlOrRd_r)
plt.axis([0,50,0,50])
plt.show()
plot()
警告:
- 是否不可能在
hexbin
中绘制相同的数据? - 我做错了什么吗?
我的具体情况:
我知道这可能是一个奇怪的问题,但我的程序正在绘制大量点 (x,y)(当然是在 hexbin
中),有时这些点可能都是相同的。
如果我稍微改变上面的代码并在 list[i]
中放入不同的点 (x,y)(i 是任何索引)代码运行良好并绘制数据。
最佳答案
问题是它试图通过查看最大和最小 x
和 y
值来猜测网格的限制,并使步长 sx = (x_max - x_min)/num_x_bins
在此输入的情况下严格为零。解决方案是使用 extent
关键字告诉代码该数组有多大。
mylist = list()
for i in range(100):
mylist.append(10)
def plot():
plt.subplot(111)
plt.hexbin(mylist,mylist,bins='log', cmap=plt.cm.YlOrRd_r, extent=[0, 50, 0, 50])
plt.axis([0,50,0,50])
plt.show()
plot()
有一个 PR 可以解决这个问题(应该在 1.4 https://github.com/matplotlib/matplotlib/pull/3038 中)
与此同时我会使用类似的东西(未经测试,这里可能有一些微不足道的错误):
import matplotlib.transfroms as mtrans
def safe_hexbin(ax, x, y, *args, **kwargs):
if 'extent' not in kwargs:
xmin = np.amin(x)
xmax = np.amax(x)
ymin = np.amin(y)
ymax = np.amax(y)
# to avoid issues with singular data, expand the min/max pairs
xmin, xmax = mtrans.nonsingular(xmin, xmax, expander=0.1)
ymin, ymax = mtrans.nonsingular(ymin, ymax, expander=0.1)
kwargs['extent'] = (xmin, xmax, ymin, ymax)
return ax.hexbin(x, y, *args, **kwargs)
safe_hexbin(plt.gca(), x, y, ...)
关于python - 是否可以在 matplotlib hexbin 图上绘制相同点的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14780362/