Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否可以在matplotlib hexbin绘图上绘制相同点的列表?_Python_Matplotlib - Fatal编程技术网

Python 是否可以在matplotlib hexbin绘图上绘制相同点的列表?

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

我有一个非常简单的代码,它绘制了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
    中绘制相同的数据吗
  • 我做错什么了吗
  • 我的具体情况:

    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
    值都是10
  • 可能没有为您的用例提供所有必要的输入
  • 因此,我通过以下方式获得输出:

    import 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,就可以完全避免这个错误。