繁体   English   中英

如何在matplotlib中缩小X轴上的图?

[英]How to shrink plot on x-axis in matplotlib?

我已经看到了几个示例,但是这些图的构造不同,并且我不知道如何使语法起作用。 这是我的代码:

pdf_file = PdfPages(sys.argv[1].split('.')[0] + "_graphs.pdf")
for i in range(0, len(list_of_data)):
  biorep = int(list_of_figure_key[i].split('.')[1])
  construct = int(list_of_figure_key[i].split('.')[0].split('_')[1])
  plot(time, list_of_data[i], color=color_dict[construct], linestyle=linestyle_dict[biorep], label=list_of_figure_key[i] )
  xlabel('time (hours)', fontsize=9)
  ylabel(ReadType, fontsize=9)
  xlim(min(time),max(time))
  legend(fontsize=8, loc='center left', bbox_to_anchor=(1, .5))
pdf_file.savefig()

它产生漂亮的图形,但图例太长,超出页面边缘。 我想在x轴上缩小绘图,以使图例适合作为2列的图例。

可以在这里看到图: http : //i.imgur.com/mvgzIhj.jpg

提前致谢!

您可以使用ncol legend属性创建一个两列的图例。 您可以通过在绘图上绘制轴并固定其大小来缩小绘图的宽度:

from matplotlib import pyplot as plt
fig = plt.figure()                      # initialize figure
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # add axis

为了使此功能与您的代码一起使用,应执行以下操作:

# import pyplot
from matplotlib import pyplot as plt

# set up filename to save it
pdf_file = PdfPages(sys.argv[1].split('.')[0] + "_graphs.pdf")

# set up axis object
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])

# plot your data
for i in range(0, len(list_of_data)):
    biorep = int(list_of_figure_key[i].split('.')[1])
    construct = int(list_of_figure_key[i].split('.')[0].split('_')[1])
    ax.plot(time, list_of_data[i], color=color_dict[construct],
        linestyle=linestyle_dict[biorep], label=list_of_figure_key[i] )

# modify axis limits and legend 
ax.set_xlabel('time (hours)', fontsize=9)
ax.set_ylabel(ReadType, fontsize=9)
ax.set_xlim(min(time),max(time))
ax.legend(fontsize=8, loc='upper left', bbox_to_anchor=(1, .5), ncol=2)

# save final figure
plt.savefig(pdf_file)

在您的代码中,您将在for循环的每次迭代中重新创建图例,限制和图例,以及保存然后覆盖pdf图像。 这不是必需的-您只需在最后做一次即可。

有关更多图例提示, 这篇文章非常方便。 也很有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM