简体   繁体   English

无限期绘制matplotlib子图?

[英]plot matplotlib subplots indefinitely?

say I was testing a range of parameters of a clustering algorithm and I wanted to write python code that would plot all the results of the algorithm in subplots 2 to a row 说我正在测试聚类算法的一系列参数,我想编写python代码,将子图2中算法的所有结果绘制到一行

is there a way to do this without pre-calculating how many total plots you would need? 有没有一种方法无需预先计算您将需要多少个总地块?

something like: 就像是:

for c in range(3,10):
    k = KMeans(n_clusters=c)
    plt.subplots(_, 2, _)
    plt.scatter(data=data, x='x', y='y', c=k.fit_predict(data))

... and then it would just plot 'data' with 'c' clusters 2 plots per row until it ran out of stuff to plot. ...然后它将仅以'c'簇绘制'数据',每行2个图,直到用完所有要绘制的东西。

thanks! 谢谢!

This answer from the question Dynamically add/create subplots in matplotlib explains a way to do it: https://stackoverflow.com/a/29962074/3827277 这个问题的答案来自在matplotlib中动态添加/创建子图,说明了一种实现方法: https : //stackoverflow.com/a/29962074/3827277

verbatim copy & paste: 逐字复制并粘贴:

import matplotlib.pyplot as plt

# Start with one
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1,2,3])

# Now later you get a new subplot; change the geometry of the existing
n = len(fig.axes)
for i in range(n):
    fig.axes[i].change_geometry(n+1, 1, i+1)

# Add the new
ax = fig.add_subplot(n+1, 1, n+1)
ax.plot([4,5,6])

plt.show() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM