繁体   English   中英

重叠在 matplotlib plot

[英]overlapping in matplotlib plot

我正在比较穷举搜索和 g.netic 算法的执行时间以及示例解决方案。 在下图中,我试图提高图表的可读性。 但是,它们相互重叠。

示例代码:

import matplotlib.pyplot as plt

# define the data
exhaustive_search = [72, 9139]
genetic_algorithm = [52.8, 200]

# create a figure with two subplots
fig, (ax1, ax2) = plt.subplots(1, 2)

# set the spacing between the subplots
fig.subplots_adjust(wspace=0.1)

# create a bar chart for execution time
ax1.bar(['Exhaustive Search', 'Genetic Algorithm'], [exhaustive_search[0], genetic_algorithm[0]], width=0.4, align='edge')
ax1.set_xlabel('Algorithm')
ax1.set_ylabel('Execution Time (s)')

# create a bar chart for number of solutions
ax2.bar(['Exhaustive Search', 'Genetic Algorithm'], [exhaustive_search[1], genetic_algorithm[1]], width=-0.4, align='edge')
ax2.set_xlabel('Algorithm')
ax2.set_ylabel('Number of Solutions')

# add a title to the figure
fig.suptitle('Execution Time and Solution Comparison')

# show the plot
plt.show()

Output:

在此处输入图像描述

我该如何解决这个问题或为读者进一步改进它?

我正在比较穷举搜索和 g.netic 算法的执行时间以及示例解决方案。 我正在尝试提高图表的可读性。 但是,它们相互重叠。

我该如何解决这个问题或为读者进一步改进它?

您可以为plt.subplots figsize 另外,我删除fig.subplots_adjust(wspace=0.1)并改用plt.tight_layout()

我认为您希望刻度线在条形中心对齐,因此我将条形图的刻度线align更改为'center'

import matplotlib.pyplot as plt

# define the data
exhaustive_search = [72, 9139]
genetic_algorithm = [52.8, 200]

# create a figure with two subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))

# create a bar chart for execution time
ax1.bar(['Exhaustive Search', 'Genetic Algorithm'], [exhaustive_search[0], genetic_algorithm[0]], width=0.4,
        align='center')
ax1.set_xlabel('Algorithm')
ax1.set_ylabel('Execution Time (s)')

# create a bar chart for number of solutions
ax2.bar(['Exhaustive Search', 'Genetic Algorithm'], [exhaustive_search[1], genetic_algorithm[1]], width=-0.4,
        align='center')
ax2.set_xlabel('Algorithm')
ax2.set_ylabel('Number of Solutions')

# add a title to the figure
fig.suptitle('Execution Time and Solution Comparison')

# show the plot
plt.tight_layout()
plt.show()

在此处输入图像描述

暂无
暂无

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

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