简体   繁体   中英

Plot multiple graphs using pyplot in python

I want to plot the scatter plot between all the combinations of the features in the data. For this I am using the following code, but I am getting overlapping graphs.

#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

The wine data contains 13 features. I want to plot the scatter plot between all the pairs of feature. The output of the above code looks like below:

在此处输入图片说明

I am doing code on google colab.

Please help in avoiding the overlapping of the graphs.

Two solutions:

1. Try adding plt.tight_layout() at the end of your code, which should function to remove overlap.

    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. Create 180 figures, instead of one containing 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;

I got the solution, it was just increasing the length of the figure.

#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

Thank you all for your comments and guidance :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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