Python 使页面上所有子批次的x轴长度相同
我不熟悉matplotlib,并尝试通过循环从pandas数据帧创建和保存绘图。每个绘图应具有相同的x轴,但不同的y轴长度和标签。创建和保存具有不同y轴长度和标签的绘图没有问题,但创建绘图时,matplotlib会根据图形左侧y轴标签所需的空间大小重新缩放x轴 这些数字用于技术报告。我计划在报告的每一页上放置一个,我希望所有的x轴在页面上占据相同的空间 这是我得到的和我想要得到的MSPaint版本。 希望这是足够的代码来帮助。我相信这其中有很多非最佳部分Python 使页面上所有子批次的x轴长度相同,python,python-2.7,matplotlib,seaborn,subplot,Python,Python 2.7,Matplotlib,Seaborn,Subplot,我不熟悉matplotlib,并尝试通过循环从pandas数据帧创建和保存绘图。每个绘图应具有相同的x轴,但不同的y轴长度和标签。创建和保存具有不同y轴长度和标签的绘图没有问题,但创建绘图时,matplotlib会根据图形左侧y轴标签所需的空间大小重新缩放x轴 这些数字用于技术报告。我计划在报告的每一页上放置一个,我希望所有的x轴在页面上占据相同的空间 这是我得到的和我想要得到的MSPaint版本。 希望这是足够的代码来帮助。我相信这其中有很多非最佳部分 import pandas as pd
import pandas as pd
import matplotlib.pyplot as plt
import pylab as pl
from matplotlib import collections as mc
from matplotlib.lines import Line2D
import seaborn as sns
# elements for x-axis
start = -1600
end = 2001
interval = 200 # x-axis tick interval
xticks = [x for x in range(start, end, interval)] # create x ticks
# items needed for legend construction
lw_bins = [0,10,25,50,75,90,100] # bins for line width
lw_labels = [3,6,9,12,15,18] # line widths
def make_proxy(zvalue, scalar_mappable, **kwargs):
color = 'black'
return Line2D([0, 1], [0, 1], color=color, solid_capstyle='butt', **kwargs)
# generic image ID
img_path = r'C:\\Users\\user\\chart'
img_ID = 0
for line_subset in data:
# create line collection for this run through loop
lc = mc.LineCollection(line_subset)
# create plot and set properties
sns.set(style="ticks")
sns.set_context("notebook")
fig, ax = pl.subplots(figsize=(16, len(line_subset)*0.5)) # I want the height of the figure to change based on number of labels on y-axis
# Figure width should stay the same
ax.add_collection(lc)
ax.set_xlim(left=start, right=end)
ax.set_xticks(xticks)
ax.set_ylim(0, len(line_subset)+1)
ax.margins(0.05)
sns.despine(left=True)
ax.xaxis.set_ticks_position('bottom')
ax.set_yticks(line_subset['order'])
ax.set_yticklabels(line_subset['ylabel'])
ax.tick_params(axis='y', length=0)
# legend
proxies = [make_proxy(item, lc, linewidth=item) for item in lw_labels]
ax.legend(proxies, ['0-10%', '10-25%', '25-50%', '50-75%', '75-90%', '90-100%'], bbox_to_anchor=(1.05, 1.0),
loc=2, ncol=2, labelspacing=1.25, handlelength=4.0, handletextpad=0.5, markerfirst=False,
columnspacing=1.0)
# title
ax.text(0, len(line_subset)+2, s=str(img_ID), fontsize=20)
# save as .png images
plt.savefig(r'C:\\Users\\user\\Desktop\\chart' + str(img_ID) + '.png', dpi=300, bbox_inches='tight')
要获得子地块的匹配x轴(每个子地块的x轴长度相同),需要在子地块之间共享x轴
请参见此处的示例除非您使用特定定义纵横比的轴(如在imshow绘图中或通过调用
.set_aspect(“equal”)
),否则轴占用的空间应仅取决于沿该方向的图形大小和图形设置的间距
因此,您需要的是默认行为,唯一阻止您获得该行为的是在savefig
命令中使用bbox\u inches='tight'
bbox\u inches='tight'
将更改体形大小!因此,不要使用它,轴的大小将保持不变`
根据我对问题的理解,您的体形大小定义为figsize=(16,len(line_子集)*0.5)
似乎是合理的。所以剩下的就是要确保图形中的轴是您想要的大小。您可以通过使用手动放置它来完成此操作
其中左、下、宽、高为0到1的地物坐标。或者,您可以使用调整子地块外的间距
有没有办法存储x轴,以便我可以在下一次循环中使用它?我不希望所有的图都在一个连续的图形上;它们需要保存为单个.png文件。您可以在临时变量中存储x限制。假设从第0次运行开始,并将其应用于下一次运行(第一次运行)。但您可能会遇到这样的问题:对于第二次运行,您将应用第一次运行的限制。你可能不想这样。这就是我不加批判地分解我找到的一些代码所得到的。但是,当我取下bbox\u inches='tight'
时,我的y轴标签只有在我将figsize
设置为巨大的x值(>25英寸)时才会完全显示。我能不能把它全部改成8.5x11英寸的尺寸?
fig.add_axes([left, bottom, width, height])
plt.subplots_adjust(left, bottom, right, top)