Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/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
matplotlib条形图外部位置和位置之间的固定填充_Matplotlib_Bar Chart - Fatal编程技术网

matplotlib条形图外部位置和位置之间的固定填充

matplotlib条形图外部位置和位置之间的固定填充,matplotlib,bar-chart,Matplotlib,Bar Chart,我正在尝试创建一个条形图,它在外部和内部位置之间始终保持固定的距离,而不管标签的长度。我希望看到bar和bar_long处于与bar_long和bar_perfect相同的位置。我试过使用axis.set_位置,但没有成功。提前感谢您的帮助 import matplotlib.pyplot as plt def createBar(figx, figy, labels): fig, ax = plt.subplots(figsize=(figx, figy) performan

我正在尝试创建一个条形图,它在外部和内部位置之间始终保持固定的距离,而不管标签的长度。我希望看到bar和bar_long处于与bar_long和bar_perfect相同的位置。我试过使用axis.set_位置,但没有成功。提前感谢您的帮助

import matplotlib.pyplot as plt

def createBar(figx, figy, labels):
    fig, ax = plt.subplots(figsize=(figx, figy)
    performance = [10, 70, 120]
    ax.barh(labels, performance)
    return fig
bar = createBar(2, 1, ('Tom', 'Dick', 'Fred'))
bar_long = createBar(2, 1, ('Tom Cruise', 'Dick & Doof', 'Fred Astaire'))
bar_perfect = createBar(2, 1, ('            Tom', 'Dick', 'Fred'))

要使所有绘图都相同,所有绘图都需要相同的边距。因此,您需要将它们全部设置为某个固定值。plt.子批次\u调整。。。这样做。数字是从0到1的分数,其中0是图形的左下角,1是右上角

对于您的2x1示例,以下方法可行:

将matplotlib.pyplot作为plt导入 def createBarfigx、figy、标签: 图,ax=plt.子图尺寸=figx,figy 性能=[10,70,120] ax.Barhlabel,性能 plt.子批次调整左=0.4,右=0.95,顶部=0.97,底部=0.25 返回图 bar=createBar2,1,'Tom','Dick','Fred' bar_long=createBar2,1,'Tom Cruise','Dick&Doof','Fred Astaire' bar_perfect=createBar2,1,'Tom','Dick','Fred' 节目
要使所有绘图都相同,所有绘图都需要相同的边距。因此,您需要将它们全部设置为某个固定值。plt.子批次\u调整。。。这样做。数字是从0到1的分数,其中0是图形的左下角,1是右上角

对于您的2x1示例,以下方法可行:

将matplotlib.pyplot作为plt导入 def createBarfigx、figy、标签: 图,ax=plt.子图尺寸=figx,figy 性能=[10,70,120] ax.Barhlabel,性能 plt.子批次调整左=0.4,右=0.95,顶部=0.97,底部=0.25 返回图 bar=createBar2,1,'Tom','Dick','Fred' bar_long=createBar2,1,'Tom Cruise','Dick&Doof','Fred Astaire' bar_perfect=createBar2,1,'Tom','Dick','Fred' 节目
我不认为这是一个合适的解决方案,我甚至觉得有点不好意思发布它,但如果你真的需要一些工作的同时

import matplotlib.pyplot as plt

def createBar(figx, figy, labels):
    fig, (ax0, ax) = plt.subplots(1, 2, figsize=(figx, figy),
                                  gridspec_kw={'width_ratios': [1, 2]})
    performance = [10, 70, 120]
    ax.barh(labels, performance)
    ax0.set_axis_off()

    return fig

bar = createBar(3, 1, ('Tom', 'Dick', 'Fred'))
bar_long = createBar(3, 1, ('Tom Cruise', 'Dick & Doof', 'Fred Astaire'))
bar_perfect = createBar(3, 1, ('          Tom', 'Dick', 'Fred'))

plt.show()

我不认为这是一个合适的解决方案,我甚至觉得有点不好意思发布它,但如果你真的需要一些工作的同时

import matplotlib.pyplot as plt

def createBar(figx, figy, labels):
    fig, (ax0, ax) = plt.subplots(1, 2, figsize=(figx, figy),
                                  gridspec_kw={'width_ratios': [1, 2]})
    performance = [10, 70, 120]
    ax.barh(labels, performance)
    ax0.set_axis_off()

    return fig

bar = createBar(3, 1, ('Tom', 'Dick', 'Fred'))
bar_long = createBar(3, 1, ('Tom Cruise', 'Dick & Doof', 'Fred Astaire'))
bar_perfect = createBar(3, 1, ('          Tom', 'Dick', 'Fred'))

plt.show()

代码的输出是,它具有所有相同的位置。您能更清楚地了解您面临的问题吗?我添加了一个显示正确输出的链接。我需要的是用短标签定位顶部图表,使其与底部图表中显示的长标签图表相匹配。当然,最简单的方法是使用子图将所有图表放在同一个图中。这是一个选项吗?我知道这会起作用,但不幸的是我必须生成一个数字。您的代码的输出是,它具有所有相同的位置。您能更清楚地了解您面临的问题吗?我添加了一个显示正确输出的链接。我需要的是用短标签定位顶部图表,使其与底部图表中显示的长标签图表相匹配。当然,最简单的方法是使用子图将所有图表放在同一个图中。这是一个选项吗?我知道这会起作用,但不幸的是,我必须生成单个数字。请参阅使用jupyter时的信息。请参阅使用jupyter时的信息。可以只执行ax0.axisoff或ax0.ax.set_axis_off.Done,剪下的整体丑陋度减少了无穷小的数量:|它的起点相当高可以只执行ax0.axisoff或ax0.ax.set_axis_off.Done,剪下的整体丑陋度减少了无穷小的数量:|它的起点相当高