Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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中创建范围条形图?_Python_Numpy_Matplotlib - Fatal编程技术网

Python 如何在matplotlib中创建范围条形图?

Python 如何在matplotlib中创建范围条形图?,python,numpy,matplotlib,Python,Numpy,Matplotlib,我尝试使用matplotlib绘制一个类似于以下内容的绘图: 但是,我不太确定使用哪种类型的图形。我的数据具有以下形式,其中起始x位置是大于或等于0的正值: <item 1><start x position><end x position> <item 2><start x position><end x position> 查看文档,我看到有和,但我不确定是否可以使用带有起始偏移量的barh。考虑到我的数据类型,

我尝试使用matplotlib绘制一个类似于以下内容的绘图:

但是,我不太确定使用哪种类型的图形。我的数据具有以下形式,其中起始x位置是大于或等于0的正值:

<item 1><start x position><end x position>
<item 2><start x position><end x position>

查看文档,我看到有和,但我不确定是否可以使用带有起始偏移量的barh。考虑到我的数据类型,最好的方法是什么?我对图书馆不太熟悉,所以我希望能有所了解。

开胃菜

注释代码 据我所知,最直接的方法是使用
matplotlib的
patches
模块在
matplotlib
画布上直接绘制矩形

下面是一个简单的实现

import matplotlib.pyplot as plt
import matplotlib.patches as patches

def plot_rect(data, delta=0.4):
    """data is a dictionary, {"Label":(low,hi), ... }
    return a drawing that you can manipulate, show, save etc"""

    yspan = len(data)
    yplaces = [.5+i for i in range(yspan)]
    ylabels = sorted(data.keys())

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.set_yticks(yplaces)
    ax.set_yticklabels(ylabels)
    ax.set_ylim((0,yspan))

    # later we'll need the min and max in the union of intervals
    low, hi =  data[ylabels[0]]
    for pos, label in zip(yplaces,ylabels):
        start, end = data[label]
        ax.add_patch(patches.Rectangle((start,pos-delta/2.0),end-start,delta))
        if start<low : low=start
        if end>hi : hi=end

    # little small trick, draw an invisible line so that the x axis
    # limits are automatically adjusted...
    ax.plot((low,hi),(0,0))

    # now get the limits as automatically computed
    xmin, xmax = ax.get_xlim()
    # and use them to draw the hlines in your example
    ax.hlines(range(1,yspan),xmin,xmax)
    # the vlines are simply the x grid lines
    ax.grid(axis='x')
    # eventually return what we have done
    return ax

# this is the main script, note that we have imported pyplot as plt

# the data, inspired by your example, 
data = {'A':(1901,1921),
        'B':(1917,1935),
        'C':(1929,1948),
        'D':(1943,1963),
        'E':(1957,1983),
        'F':(1975,1991),
        'G':(1989,2007)}

# call the function and give its result a name
ax = plot_rect(data)
# so that we can further manipulate it using the `axes` methods, e.g.
ax.set_xlabel('Whatever')
# finally save or show what we have
plt.show()


在我的VH看来,自定义绘图功能应该是描述绘图最不必要的功能,因为在
matplotlib

中,这种后期制作通常非常容易,对于您要求的处理类型,使用术语“范围条形图”是很有必要的,同时,对于该问题的
matplotlib
解决方案,没有任何web参考。我可以请您将问题的标题改为更易于搜索的标题吗?例如,源于您自己的“在matplotlib中使用范围条形图的正确绘图?”或更直接的“使用matplotlib绘制范围条形图”。。。谢谢你的考虑,谢谢!这极大地帮助了我的工作:)
ax = plot_rect(data)
ax.set_xlabel('Whatever')
for rect in ax.patches:
    rect.set(facecolor=(0.9,0.9,0.2,1.0), # a tuple, RGBA
          edgecolor=(0.6,0.2,0.3,1.0),
          linewidth=3.0)
plt.show()