【问题标题】:Plot multiple graphs using pyplot in python在 python 中使用 pyplot 绘制多个图
【发布时间】:2018-09-22 14:39:07
【问题描述】:

我想绘制数据中所有特征组合之间的散点图。为此,我使用了以下代码,但我得到了重叠的图表。

#importing the important libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.cross_validation import train_test_split
from sklearn import metrics
from sklearn import datasets

wine_data = datasets.load_wine()

#exploring the ralationship between the data by visualizing it.
i = 1
plt.figure(figsize=(15,15))
for feature_x_pos,feature_x in enumerate(wine_data.feature_names):
  for feature_y_pos,feature_y in enumerate(wine_data.feature_names):
    if feature_x_pos != feature_y_pos:
      plt.subplot(60,3,i)
      plt.scatter(wine_data.data[:,feature_x_pos],wine_data.data[:,feature_y_pos],c = wine_data.target, cmap = 'jet')
      plt.xlabel(feature_x)
      plt.ylabel(feature_y)
      i=i+1

葡萄酒数据包含 13 个特征。我想绘制所有特征对之间的散点图。 上述代码的输出如下所示:

我在 google colab 上做代码。

请帮助避免图表重叠。

【问题讨论】:

  • 所以你想要一个 15x15 英寸的图形上的 180 个子图,每个子图都有标签,你不希望任何东西重叠?我认为这不太可行。
  • 感谢@ThomasKühn 您的评论让我解决了这个问题。我只是增加了情节的长度( plt.figure(figsize=(15,190)) )。这是我犯的一个非常愚蠢的错误。再次感谢。

标签: python matplotlib scikit-learn


【解决方案1】:

两种解决方案:

1. 尝试在代码末尾添加plt.tight_layout(),这应该可以消除重叠。

    i = 1
    plt.figure(figsize=(15,15))
    for feature_x_pos,feature_x in enumerate(wine_data.feature_names):
      for feature_y_pos,feature_y in enumerate(wine_data.feature_names):
        if feature_x_pos != feature_y_pos:
          plt.subplot(60,3,i)
          plt.scatter(wine_data.data[:,feature_x_pos],wine_data.data[:,feature_y_pos],c = wine_data.target, cmap = 'jet')
          plt.xlabel(feature_x)
          plt.ylabel(feature_y)
          i=i+1;

plt.tight_layout()

2.创建 180 个数字,而不是一个包含 180 个的数字。

    i = 1

    for feature_x_pos,feature_x in enumerate(wine_data.feature_names):
      for feature_y_pos,feature_y in enumerate(wine_data.feature_names):
        if feature_x_pos != feature_y_pos:
          fig, ax = plt.subplots(1,1)
          ax.scatter(wine_data.data[:,feature_x_pos],wine_data.data[:,feature_y_pos],c = wine_data.target, cmap = 'jet')
          ax.set_xlabel(feature_x)
          ax.set_ylabel(feature_y)
          fig.show()
          i=i+1;

【讨论】:

    【解决方案2】:

    我得到了解决方案,它只是增加了图形的长度。

    #exploring the ralationship between the data by visualizing it.
    i = 1
    plt.figure(figsize=(15,200)) #changed the length from 15 to 200
    for feature_x_pos,feature_x in enumerate(wine_data.feature_names):
      for feature_y_pos,feature_y in enumerate(wine_data.feature_names):
        if feature_x_pos != feature_y_pos:
          plt.subplot(60,3,i)
          plt.scatter(wine_data.data[:,feature_x_pos],wine_data.data[:,feature_y_pos],c = wine_data.target, cmap = 'jet')
          plt.xlabel(feature_x)
          plt.ylabel(feature_y)
          i=i+1
    

    感谢大家的cmets和指导:)

    【讨论】:

      猜你喜欢
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多