Open In App

Plot multiple separate graphs for same data from one Python script

Improve
Improve
Like Article
Like
Save
Share
Report

It is very easy to plot different kinds of maps and plots of different data using Python. One such useful library in Python is Matplotlib which is very useful for creating different types of plots using the same data.

The easiest way to install Matplotlib is by using the pip command in the command-line as shown below:

pip install matplotlib

Also, we use numpy and pandas libraries for creating and using the data sets. Therefore, installing numpy and pandas library can also be done using pip command in command-line.

pip install numpy 
pip install pandas

To make plots creative and easy to find the located data inside the graph we use the plotly and cufflinks libraries. To use these libraries the method of installation is also the same as above mentioned libraries. 

pip install chart_studio.plotly
pip install cufflinks

In this article, we will create a Plotter that enables us to create different types of plots using the same data.

In this program, we first give the option for choosing the data set required to plot the graph. There are 3 options available for that using if-elif statements. 

  • Creating random data using 100 rows and 5 columns
  • User input data set with 4 rows and 5 columns
  • Upload the CSV/JSON file

The second step is to decide the plot to be plotted with the entire data set or for specific columns. And lastly, we present different kinds of plotters users want to plot their data and plot the graph accordingly. 

Below is the implementation:

Python3




# importing required libraries
import numpy as np
import pandas as pd
import chart_studio.plotly as pl
import plotly.offline as po
import cufflinks as cf
po.init_notebook_mode(connected = True)
cf.go_offline()
 
# define a function for creating
# data set for plotting graph
def createdata(data):
   
    # creating random data set
    if(data == 1):
        x = np.random.rand(100,5)
        df1 = pd.DataFrame(x, columns = ['A', 'B',
                                        'C', 'D',
                                        'E'])
         
    # creating user data set with input
    elif(data == 2):           
        x = [0, 0, 0, 0, 0]
        r1 = [0, 0, 0, 0, 0]
        r2 = [0, 0, 0, 0, 0]
        r3 = [0, 0, 0, 0, 0]
        r4 = [0, 0, 0, 0, 0]
         
        print('Enter the values for columns')
        i = 0
         
        for i in [0, 1, 2, 3, 4]:
            x[i] = input()
            i = i + 1
        print('Enter the values for first row')
        i = 0
         
        for i in [0, 1, 2, 3, 4]:
            r1[i] = int(input())
            i = i + 1
             
        print('Enter the values for second row')
        i = 0
         
        for i in [0, 1, 2, 3, 4]:
            r2[i] = int(input())
            i = i + 1
             
        print('Enter the values for third row')
        i = 0
         
        for i in [0, 1, 2, 3, 4]:
            r3[i] = int(input())
            i = i + 1
             
        print('Enter the values for fourth row')
        i = 0
         
        for i in [0, 1, 2, 3, 4]:
            r4[i] = int(input())
            i = i + 1
             
        df1 = pd.DataFrame([r1,r2,r3,r4] ,
                           columns = x)
    # creating data set by csv file  
    elif(data == 3):           
        file = input('Enter the file name')
        x = pd.read_csv(file)
        df1 = pd.DataFrame(x)
         
    else:
        print('DataFrame creation failed please' +
              'enter in between 1 to 3 and try again')
         
    return df1
 
# define a function for
# types of plotters
def plotter(plot):
   
    if(plot == 1):
        finalplot = df1.iplot(kind = 'scatter')
         
    elif(plot == 2):
        finalplot = df1.iplot(kind = 'scatter', mode = 'markers',
                              symbol = 'x', colorscale = 'paired')
    elif(plot == 3):
        finalplot = df1.iplot(kind = 'bar')
         
    elif(plot == 4):
        finalplot = df1.iplot(kind = 'hist')
         
    elif(plot == 5):
        finalplot = df1.iplot(kind = 'box')
         
    elif(plot == 6):
        finalplot = df1.iplot(kind = 'surface')
         
    else:
        finalplot = print('Select only between 1 to 7')
         
    return finalplot
 
# define a function for allowing
# to plot for specific rows and columns
def plotter2(plot):           
   
    col = input('Enter the number of columns you' +
                'want to plot by selecting only 1 , 2 or 3')
     
    col = int(col)
     
    if(col==1):
       
        colm = input('Enter the column you want to plot' +
                     'by selecting any column from dataframe head')
        if(plot == 1):
            finalplot = df1[colm].iplot(kind = 'scatter')
             
        elif(plot == 2):
            finalplot = df1[colm].iplot(kind = 'scatter', mode = 'markers',
                                        symbol = 'x', colorscale = 'paired')
        elif(plot == 3):
            finalplot = df1[colm].iplot(kind = 'bar')
             
        elif(plot == 4):
            finalplot = df1[colm].iplot(kind = 'hist')
             
        elif(plot == 5):
            finalplot = df1[colm].iplot(kind = 'box')
             
        elif(plot == 6 or plot == 7):
            finalplot = print('Bubble plot and surface plot require' +
                              'more than one column arguments')
        else:
            finalplot = print('Select only between 1 to 7')
             
    elif(col == 2):
       
        print('Enter the columns you want to plot' +
              'by selecting from dataframe head')
         
        x = input('First column')
        y = input('Second column')
         
        if(plot == 1):
            finalplot = df1[[x,y]].iplot(kind = 'scatter')
             
        elif(plot == 2):
            finalplot = df1[[x,y]].iplot(kind = 'scatter', mode = 'markers',
                                         symbol = 'x', colorscale = 'paired')
        elif(plot == 3):
            finalplot = df1[[x,y]].iplot(kind = 'bar')
             
        elif(plot == 4):
            finalplot = df1[[x,y]].iplot(kind = 'hist')
             
        elif(plot == 5):
            finalplot = df1[[x,y]].iplot(kind = 'box')
             
        elif(plot == 6):
            finalplot = df1[[x,y]].iplot(kind = 'surface')
             
        elif(plot == 7):
            size = input('Please enter the size column for bubble plot')
            finalplot = df1.iplot(kind = 'bubble', x = x,
                                  y = y, size = size)
        else:
            finalplot = print('Select only between 1 to 7')
             
    elif(col == 3):
       
        print('Enter the columns you want to plot')
        x = input('First column')
        y = input('Second column')
        z = input('Third column')
          
        if(plot == 1):
            finalplot = df1[[x,y,z]].iplot(kind = 'scatter')
             
        elif(plot == 2):
            finalplot = df1[[x,y,z]].iplot(kind = 'scatter', mode = 'markers',
                                           symbol = 'x' ,colorscale = 'paired')
        elif(plot == 3):
            finalplot = df1[[x,y,z]].iplot(kind = 'bar')
             
        elif(plot == 4):
            finalplot = df1[[x,y,z]].iplot(kind = 'hist')
             
        elif(plot == 5):
            finalplot = df1[[x,y,z]].iplot(kind = 'box')
             
        elif(plot == 6):
            finalplot = df1[[x,y,z]].iplot(kind = 'surface')
             
        elif(plot == 7):
            size = input('Please enter the size column for bubble plot')
             
            finalplot = df1.iplot(kind = 'bubble', x = x, y = y,
                                  z = z, size = size )
        else:
            finalplot = print('Select only between 1 to 7')
    else:
        finalplot = print('Please enter only 1 , 2 or 3')
    return finalplot
 
# define a main function
# for asking type of plot
# and calling respective function
def main(cat):   
   
    if(cat == 1):
       
        print('Select the type of plot you need to plot by writing 1 to 6')
        print('1.Line plot')
        print('2.Scatter plot')
        print('3.Bar plot')
        print('4.Histogram')
        print('5.Box plot')
        print('6.Surface plot')
        plot = int(input())
        output = plotter(plot)
         
    elif(cat == 2):
       
        print('Select the type of plot you need to plot by writing 1 to 7')
        print('1.Line plot')
        print('2.Scatter plot')
        print('3.Bar plot')
        print('4.Histogram')
        print('5.Box plot')
        print('6.Surface plot')
        print('7.Bubble plot')
        plot = int(input())
        output = plotter2(plot)
         
    else:
        print('Please enter 1 or 2 and try again'
         
print('Select the type of data you need to plot(By writing 1,2 or 3)')
print('1.Random data with 100 rows and 5 columns')
print('2.Customize dataframe with 5 columns and. 4 rows')
print('3.Upload csv/json/txt file')
 
data = int(input())
df1 = createdata(data)
print('Your DataFrame head is given below check the columns to plot using cufflinks')
 
df1.head()
print('What kind of plot you need , the complete data plot or columns plot')
cat = input('Press 1 for plotting all columns or press 2 for specifying columns to plot')
cat = int(cat)
 
main(cat)


 
 

Output:

 

Scatter plot:

 

 Scatter graph plot

 Scatter graph plot

Histogram plot:

 

Histogram plot

Histogram plot 

Boxplot:

 

Box plot

Boxplot 

 



Last Updated : 13 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads