【发布时间】:2021-09-09 06:35:50
【问题描述】:
我有一个特征文件,我想显示每个特征之间的对比,但由于它们很多,我将每个特征的图分开,所以每个图的一个特征与所有其他特征。由于它很慢,我将它并行化,并且我有这个代码。
def DrawPlots(Features, FeatureNames, ...):
feature1 = FeatureNames[Column]
fig, axs = plt.subplots(...)
for j in range(length):
if Column != j:
feature2 = FeatureNames[j]
...
plot = seaborn.scatterplot(data=X, x=feature1, y=feature2, ax=axs[...])
fig.savefig("Test " + str(Column) + ".png", dpi=150)
if __name__ == '__main__':
Features = pandas.read_csv("...csv")
ColumnNames = list(Features.columns)
...
Threads = []
startime = time.time()
for i in range(5):
#DrawPlots(Features, ColumnNames, i, ...) # When not parallelized
thread = threading.Thread(target=DrawPlots, args=(Features, ColumnNames, i, ...))
Threads.append(thread)
thread.start()
print(str(i) + ' running.')
for t in Threads:
t.join()
print("--- %s seconds ---" % (time.time() - startime))
当我从 PyCharm 运行此代码时,它会按预期工作:
0 running.
1 running.
2 running.
3 running.
4 running.
--- 118.35315799713135 seconds ---
All done.
Process finished with exit code 0
但是当我从命令行启动它时,我立即收到以下错误(胎面的短版):
0 running.
1 running.
Test.py:25: UserWarning: Starting a Matplotlib GUI outside of the main thread will likely fail.
figs, axs = plt.subplots(...)
2021-06-25 17:05:23.080 Python[96144:2936258] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff38ded627 __exceptionPreprocess + 250
1 libobjc.A.dylib 0x00007fff71ccd5bf objc_exception_throw + 48
...
60 libsystem_pthread.dylib 0x00007fff7307a109 _pthread_start + 148
61 libsystem_pthread.dylib 0x00007fff73075b8b thread_start + 15
)
libc++abi.dylib: terminating with uncaught exception of type NSException
[1] 96144 abort python Test.py
为什么它是从命令行而不是从 PyCharm 发生的?
我该如何解决?
有没有更好的方法来并行化代码以在两种环境中工作?
【问题讨论】:
标签: python matplotlib parallel-processing pycharm seaborn