Python Matplotlib,设置图表在图像中的位置

Python Matplotlib,设置图表在图像中的位置,python,matplotlib,figure,Python,Matplotlib,Figure,我正在制作一个工具,使用Matplotlib自动生成两个图形,对于我遇到的每一个问题,通常在经过一些研究之后,我最终找到了一个解决方案,但在这里我觉得我卡住了 1-我想要的是,在生成的png文件中,在左侧图形开始之前有一种偏移量,在这里我可以插入一些文本对象。如何做到这一点 2-我还希望能够在图形下方插入文本,因此它将非常有用 我主要希望能够定义图像文件中绘图的位置,这里是一个示例,说明了我当前的内容和内容 编辑: def plot_bar(): fig = plt.figure(fig

我正在制作一个工具,使用Matplotlib自动生成两个图形,对于我遇到的每一个问题,通常在经过一些研究之后,我最终找到了一个解决方案,但在这里我觉得我卡住了

1-我想要的是,在生成的png文件中,在左侧图形开始之前有一种偏移量,在这里我可以插入一些文本对象。如何做到这一点

2-我还希望能够在图形下方插入文本,因此它将非常有用

我主要希望能够定义图像文件中绘图的位置,这里是一个示例,说明了我当前的内容和内容

编辑:

def plot_bar():


  fig = plt.figure(figsize=(600/96, 360/96), dpi = 96)


# Setting the padding between the ticks' labels and the axes
  mpl.rcParams['xtick.major.pad']='10'

  ax = plt.gca()

  ax.spines['top'].set_visible(False)
  ax.spines['right'].set_visible(False)
  ax.spines['left'].set_visible(False)
  ax.spines['bottom'].set_visible(False)


  ax.axes.get_yaxis().set_visible(False)

  mpl.rcParams['axes.facecolor'] = 'white'
  mpl.rcParams["figure.facecolor"]= 'white'
  mpl.rcParams["savefig.facecolor"]= 'white'
  Values1 = ( 10 , 25 , 30 , 40 )
  Values2 = ( 40 , 3 , 10 , 6 )

  N = 4

  ind = np.arange(N)

  width = 0.7
p1 = plt.bar(ind, Values1, width, color='#00A79A', ec='#00A79A')
  p2 = plt.bar(ind, Values2, width, color='#8FDCD6', bottom = Values1, ec='#8FDCD6')


  plt.xticks(ind+width/2., ('Fuel\n\n dollars \n\n dollars', 'Tires', 'Roadcalls', 'Maintenance') )
  plt.yticks(np.arange(0,81,10))

  # Removing Tick lines from axes
  for tic in ax.xaxis.get_major_ticks():
    tic.tick1On = tic.tick2On = False

  for tic in ax.yaxis.get_major_ticks():
    tic.tick1On = tic.tick2On = False
  plt.savefig("fichier.png")

尝试使用
子批次调整
图形方法:

fig.subplots_adjust( left=0.25, bottom=0.25 )
可以使用添加间距。还将扩展您的框,以适合之前绘图中缺少的文本(“美元”)

至于绘图的其余部分,添加标签应该相当容易。例如,下面的
For
循环将总计添加到每个条的顶部(类似于)


你能告诉我们你用来形成这个图形的代码吗?当然,我会加上它。你能告诉我们提供的解决方案是否适合你吗?如果没有,我们可以相应地更新。谢谢,这正是我想要的。
for bar in p2:
    height = bar.get_height() + p1[bar.get_x()].get_height()
    ax.text(bar.get_x()+bar.get_width()/2., 1.05*height, '%d'%int(height),
            ha='center', va='bottom')
plt.tight_layout()
plt.subplots_adjust(left=0.3, bottom=0.3)