【问题标题】:what does n += 1 in plt.subplot meanplt.subplot 中的 n += 1 是什么意思
【发布时间】:2020-12-02 21:06:38
【问题描述】:

我不明白下面的 n +=1 变量如何将 distplot 可视化分成 3 列

n=0
for x in ['Age', 'Annual Income (k$)', 'Spending Score (1-100)']:
    n +=1
    plt.subplot(1,3,n)
    plt.subplots_adjust(hspace=0.5,wspace = 0.5)
    sns.distplot(df[x], bins = 20)
    plt.title('Displot of {}'.format(x))
plt.show()

请看附件distplotenter image description here的照片

【问题讨论】:

  • 附带说明,您可以改为枚举 - for n, x in enumerate(['Age', 'Annual Income (k$)', 'Spending Score (1-100)']):
  • 这能回答你的问题吗? What does += mean in Python?

标签: python matplotlib seaborn


【解决方案1】:

plt.subplot的第三个参数代表索引。这可能对你有帮助:https://matplotlib.org/3.3.0/api/_as_gen/matplotlib.pyplot.subplot.html

"其中一个描述的子图的位置

三个整数(nrows、ncols、index)。子图将在具有 nrows 行和 ncols 列的网格上获取索引位置。索引从左上角的 1 开始,向右增加。 index 也可以是一个二元组,指定子图的(第一个,最后一个)索引(从 1 开始,包括最后一个),例如, fig.add_subplot(3, 1, (1, 2)) 生成一个跨越的子图图的上 2/3。

3 位整数。这些数字被解释为好像分别作为三个个位数整数给出,即 fig.add_subplot(235) 与 fig.add_subplot(2, 3, 5) 相同。请注意,这只能在子图不超过 9 个时使用。”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2010-12-28
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多