Python 是否可以在matplotlib hexbin绘图上绘制相同点的列表?
我有一个非常简单的代码,它绘制了100个点(10,10)的列表,这些点都是相同的。不幸的是,我收到了一个警告和一张空白的图表 我的代码:Python 是否可以在matplotlib hexbin绘图上绘制相同点的列表?,python,matplotlib,Python,Matplotlib,我有一个非常简单的代码,它绘制了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.Y
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
中绘制相同的数据吗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()
我知道这可能是一个奇怪的问题,但我的程序正在绘制大量的点(x,y)(当然是进入hexbin
),有时这些点可能都是相同的
如果我稍微修改上面的代码,并在
列表[I]
(I是任何索引)中插入一个不同的点(x,y),代码运行良好,并绘制数据。我发现您所做的有几个问题:
myList
值都是10import numpy as np
import matplotlib.pyplot as plt
x = np.logspace(-1, 2)
y = np.logspace(-1, 2)
x = np.hstack([x, x]) # duplicate all points
y = np.hstack([y, y]) # duplicate all points
xx, yy = np.meshgrid(x,y)
C = xx**2 + 10./yy**2
fig, ax = plt.subplots()
ax.hexbin(x, y, C, bins='log', cmap=plt.cm.YlOrRd_r)
plt.show()
问题是,它试图通过查看最大值和最小值
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中)
同时,我会使用类似的东西(未测试,这里可能有一些小错误):
关于你的第二点——这是有意的。我正在策划的要点很可能都是“(10,10)”。事实上,这是一个bug,我现在正在重新讨论这个问题。你对如何修复它有什么建议吗?也许我只需要在图的范围之外抛出一个任意的数据点,以便它总是绘制数据@Tcaswelsee编辑了我的答案。只要让用户使用
extent
kwarg,就可以完全避免这个错误。