我正在 matplotlib 的add_subplot()函数中创建一个图形。
当我运行这个
ax1 = fig.add_subplot(231)
ax2 = fig.add_subplot(232)
ax3 = fig.add_subplot(233)
ax4 = fig.add_subplot(212) # why not 211
Run Code Online (Sandbox Code Playgroud)
我得到这个输出
我的问题是这最后一个论点是如何运作的。为什么 ax4 是 212 而不是 211,因为第二行只有一个图?
如果我使用 211 而不是 212 运行,如下所示:
ax1 = fig.add_subplot(231)
ax2 = fig.add_subplot(232)
ax3 = fig.add_subplot(233)
ax4 = fig.add_subplot(211)
Run Code Online (Sandbox Code Playgroud)
我得到这个输出,其中 Plot 4 放在第一行的 Plot 2 上。
如果有人能解释索引是如何工作的,我将不胜感激。花了不少时间研究还是没搞懂。
这是问题所在.我有names.txt文件.该文件的内容如下.该问题要求显示此文件中的名称数量.我可以用while循环.它有效,没问题.但出于某种原因,如果我想用for循环这样做,它会给我错误的名字数量.
Julia Roberts
Michael Scott
Kevin Malone
Jim Halpert
Pam Halpert
Dwight Schrute
Run Code Online (Sandbox Code Playgroud)
这是while循环.它运行完美.
def main():
try:
# open the names.txt file in read mode.
infile=open("names.txt", "r")
# set an accumulator for number of names
numbers_of_name=0.0
# read the first line
line=infile.readline()
# read the rest of the file
while line!="":
numbers_of_name+=1
line=infile.readline()
# print the numbers of names in the names.txt file.
print("There are", int(numbers_of_name), "names in the names.txt file.")
# close the file
infile.close() …Run Code Online (Sandbox Code Playgroud)