英文:
Error ValueError: Number of columns must be a positive integer, not 0
问题
ValueError Traceback (most recent call last)
<ipython-input-22-541b90fa47bc> in <cell line: 7>()
5 return title[:10]
6
----> 7 fig, axs = plt.subplots(2, (len(app_infos)) // 2, figsize=(14, 5))
8
9 for i, ax in enumerate(axs.flat):
4 frames
/usr/local/lib/python3.10/dist-packages/matplotlib/gridspec.py in __init__(self, nrows, ncols, height_ratios, width_ratios)
50 f"Number of rows must be a positive integer, not {nrows!r}")
51 if not isinstance(ncols, Integral) or ncols <= 0:
---> 52 raise ValueError(
53 f"Number of columns must be a positive integer, not {ncols!r}")
54 self._nrows, self._ncols = nrows, ncols
ValueError: 列数必须是正整数,而不是0
def format_title(title): sep_index = title.find(':') if title.find(':') != -1 else title.find('-') if sep_index != -1: title = title[:sep_index] return title[:10]
fig, axs = plt.subplots(2, (len(app_infos)) // 2, figsize=(14, 5))
for i, ax in enumerate(axs.flat): ai = app_infos[i] img = plt.imread(ai['icon']) ax.imshow(img) ax.set_title(format_title(ai['title'])) ax.axis('off')
英文:
ValueError Traceback (most recent call last)
<ipython-input-22-541b90fa47bc> in <cell line: 7>()
5 return title[:10]
6
----> 7 fig, axs = plt.subplots(2, (len(app_infos)) // 2, figsize=(14, 5))
8
9 for i, ax in enumerate(axs.flat):
4 frames
/usr/local/lib/python3.10/dist-packages/matplotlib/gridspec.py in __init__(self, nrows, ncols, height_ratios, width_ratios)
50 f"Number of rows must be a positive integer, not {nrows!r}")
51 if not isinstance(ncols, Integral) or ncols <= 0:
---> 52 raise ValueError(
53 f"Number of columns must be a positive integer, not {ncols!r}")
54 self._nrows, self._ncols = nrows, ncols
ValueError: Number of columns must be a positive integer, not 0
def format_title(title): sep_index = title.find(':') if title.find(':') != -1 else title.find('-') if sep_index != -1: title = title[:sep_index] return title[:10]
fig, axs = plt.subplots(2, (len(app_infos)) // 2, figsize=(14, 5))
for i, ax in enumerate(axs.flat): ai = app_infos[i] img = plt.imread(ai['icon']) ax.imshow(img) ax.set_title(format_title(ai['title'])) ax.axis('off')
答案1
得分: 1
The issue with your code appears to be due to the expression len(app_infos) // 2
resulting in zero. This will happen if app_infos
is empty or has only one item. When you use the //
operator (floor division), and the size of app_infos
is either 1 or 0, the output is 0.
The exception message ValueError: Number of columns must be a positive integer, not 0
is indicating that your code is trying to create a subplot with 2 rows and 0 columns, which is not feasible.
Here's a refined version of your code to address this issue:
import matplotlib.pyplot as plt
def format_title(title):
sep_index = title.find(':') if title.find(':') != -1 else title.find('-')
if sep_index != -1:
title = title[:sep_index]
return title[:10]
# Ensuring at least 1 column for the subplots
columns = max(int(len(app_infos)//2), 1)
fig, axs = plt.subplots(2, columns, figsize=(14, 5))
for i, ax in enumerate(axs.flat):
ai = app_infos[i]
img = plt.imread(ai['icon'])
ax.imshow(img)
ax.set_title(format_title(ai['title']))
ax.axis('off')
In the revised code, I've replaced len(app_infos) // 2
with max(int(len(app_infos)//2), 1)
within plt.subplots
. This modification ensures that the number of columns set for the subplot is always at least 1.
英文:
The issue with your code appears to be due to the expression len(app_infos) // 2
resulting in zero. This will happen if app_infos
is empty or has only one item. When you use the //
operator (floor division), and the size of app_infos
is either 1 or 0, the output is 0.
The exception message ValueError: Number of columns must be a positive integer, not 0
is indicating that your code is trying to create a subplot with 2 rows and 0 columns, which is not feasible.
Here's a refined version of your code to address this issue:
import matplotlib.pyplot as plt
def format_title(title):
sep_index = title.find(':') if title.find(':') != -1 else title.find('-')
if sep_index != -1:
title = title[:sep_index]
return title[:10]
# Ensuring at least 1 column for the subplots
columns = max(int(len(app_infos)//2), 1)
fig, axs = plt.subplots(2, columns, figsize=(14, 5))
for i, ax in enumerate(axs.flat):
ai = app_infos[i]
img = plt.imread(ai['icon'])
ax.imshow(img)
ax.set_title(format_title(ai['title']))
ax.axis('off')
In the revised code, I've replaced len(app_infos) // 2
with max(int(len(app_infos)//2), 1)
within plt.subplots
. This modification ensures that the number of columns set for the subplot is always at least 1.
Side-Note
In the future, please provide a more detailed description of your problem and ensure that all necessary code components, such as the app_infos
list, are included.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论