我有2个文件夹,每个文件夹包含2个文件:
./folder_222/116.5.dat
./folder_222/118.1.dat
./folder_444/116.5.dat
./folder_444/118.1.dat
(116.5和118.1代表一卷)
我想创建一个2行,1列子图,其中:
Upper Graph:
Title: V = 116.5
Contains the plot of:
folder_222/116.5.dat
folder_444/116.5.dat
Lower Graph:
Title: V = 118.1
Contains the plot of:
folder_222/118.1.dat
folder_444/118.1.dat
我想到的最好的方法如下:
import matplotlib.pyplot as plt
import numpy as np
import os
# Folders, files and volumes:
folder_222 = './folder_222'
folder_444 = './folder_444'
files_222 = ["116.5.dat",\
"118.1.dat"]
files_444 = ["116.5.dat",\
"118.1.dat"]
vols = ["116.5",\
"118.1"]
fig = plt.figure()
# Generation of the subplots, and the title:
for indx_vols in range(1, len(vols)+1):
print ' indx_vols = ', indx_vols
ax = fig.add_subplot(len(vols), 1, indx_vols)
ax.set_title('V = ' + vols[indx_vols-1], fontsize=10)
# For a given subplot, now plot x and y:
for indx_files in range(len(vols)):
print 'indx_files = ', indx_files
x_222, y_222 = np.loadtxt(os.path.join(folder_222, files_222[indx_files]), skiprows = 1).T
x_444, y_444 = np.loadtxt(os.path.join(folder_444, files_444[indx_files]), skiprows = 1).T
print 'y_222 = ', y_222, 'x_222 = ', x_222
print 'y_444 = ', y_444, 'x_444 = ', x_444
ax.scatter(x_222, y_222, color='k', marker='o', label='222')
ax.scatter(x_444, y_444, color='m', marker='o', label='444')
如果使用所有这些打印语句运行程序,可以看到程序正在执行预期的操作:
indx_vols = 1
indx_files = 0
y_222 = [ 1086. 2328. 3633. 3387. 1278.] x_222 = [ 89. 629. 694. 785. 882.]
y_444 = [ 2262. 3876. 1119. 4338. 2817.] x_444 = [ 753. 1291. 372. 1445. 938.]
indx_files = 1
y_222 = [ 1353. 903. 4695. 429. 4641.] x_222 = [ 450. 300. 1564. 142. 1546.]
y_444 = [ 3102. 1335. 57. 1962. 2958.] x_444 = [ 1033. 444. 18. 653. 985.]
indx_vols = 2
indx_files = 0
y_222 = [ 1086. 2328. 3633. 3387. 1278.] x_222 = [ 89. 629. 694. 785. 882.]
y_444 = [ 2262. 3876. 1119. 4338. 2817.] x_444 = [ 753. 1291. 372. 1445. 938.]
indx_files = 1
y_222 = [ 1353. 903. 4695. 429. 4641.] x_222 = [ 450. 300. 1564. 142. 1546.]
y_444 = [ 3102. 1335. 57. 1962. 2958.] x_444 = [ 1033. 444. 18. 653. 985.]
然而,两个子集之间没有区别:
就好像只考虑了一个卷数据集。
如果你能帮助我,我将不胜感激
这里运行代码的文件:
./folder_222/116.5.dat:
# x y
89 1086
629 2328
694 3633
785 3387
882 1278
./folder_222/118.1.dat:
# x y
450 1353
300 903
1564 4695
142 429
1546 4641
./folder_444/116.5.dat:
# x y
753 2262
1291 3876
372 1119
1445 4338
938 2817
./folder_444/118.1.dat:
# x y
1033 3102
444 1335
18 57
653 1962
985 2958
答案 0 :(得分:0)
正如@ImportanceOfBeingErnest所指出的,所有文件的内容都被绘制到两个图中。
也许,有一种更聪明的方法,但我一直在想的一个可能的解决方案是用zip
for循环替换嵌套的for循环:
import matplotlib.pyplot as plt
import sys
import numpy as np
import os
folder_222 = './folder_222'
folder_444 = './folder_444'
files_222 = ["116.5.dat",\
"118.1.dat"]
files_444 = ["116.5.dat",\
"118.1.dat"]
vols = ["116.5",\
"118.1"]
fig = plt.figure()
for indx_vols, indx_files in zip(range(1, len(vols)+1), range(len(vols))):
print ' indx_vols = ', indx_vols
ax = fig.add_subplot(len(vols), 1, indx_vols)
ax.set_title('V = ' + vols[indx_vols-1], fontsize=10)
print 'indx_files = ', indx_files
x_222, y_222 = np.loadtxt(os.path.join(folder_222, files_222[indx_files]), skiprows = 1).T
x_444, y_444 = np.loadtxt(os.path.join(folder_444, files_444[indx_files]), skiprows = 1).T
print 'y_222 = ', y_222, 'x_222 = ', x_222
print 'y_444 = ', y_444, 'x_444 = ', x_444
ax.scatter(x_222, y_222, color='k', marker='o', label='222')
ax.scatter(x_444, y_444, color='m', marker='o', label='444')
fig.set_facecolor('w')
plt.tight_layout()
plt.show()
现在打印语句显示每个卷图将包含唯一数据集(x_222, y_222)
和(x_444, y_444)
:
indx_vols = 1
indx_files = 0
y_222 = [ 1086. 2328. 3633. 3387. 1278.] x_222 = [ 89. 629. 694. 785. 882.]
y_444 = [ 2262. 3876. 1119. 4338. 2817.] x_444 = [ 753. 1291. 372. 1445. 938.]
indx_vols = 2
indx_files = 1
y_222 = [ 1353. 903. 4695. 429. 4641.] x_222 = [ 450. 300. 1564. 142. 1546.]
y_444 = [ 3102. 1335. 57. 1962. 2958.] x_444 = [ 1033. 444. 18. 653. 985.]