小编Meh*_*CER的帖子

.add_subplot(nrows, ncols, index) 如何工作?

我正在 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 上。

在此处输入图片说明

如果有人能解释索引是如何工作的,我将不胜感激。花了不少时间研究还是没搞懂。

python matplotlib subplot

2
推荐指数
1
解决办法
3949
查看次数

在Python中使用for循环读取文件的内容

这是问题所在.我有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)

python for-loop while-loop

1
推荐指数
1
解决办法
518
查看次数

标签 统计

python ×2

for-loop ×1

matplotlib ×1

subplot ×1

while-loop ×1