[PYTHON] Create multiple line charts from a data frame at once using Matplotlib

Introduction

When creating a graph with Python's Matplotlib, I tried to get the values from each data frame and create all the graphs for hundreds of lists, so I made a prototype method that can be done at once.

Method

The method is as follows. ① Read the data frame ① ‘If the list is duplicated, put it together as a new data frame (remove duplicates) ② Create graphs in order using For Loop

code

This time, I used the kaggle data frame (OSIC Pulmonary Fibrosis Progression). By the way, the number of rows and columns is (1549,7), and the duplication is 176 rows. This time, we will create 176 graphs at once.

① Read the data frame

import pandas as pd
train_df = pd.read_csv("train.csv")
train_df

① ‘If the list is duplicated, put it together as a new data frame (remove duplicates)

new_df = train_df.groupby([train_df.Patient,train_df.Age,train_df.Sex, train_df.SmokingStatus])['Patient'].count() #Duplicate count
new_df.index = new_df.index.set_names(['id','Age','Sex','SmokingStatus'])
new_df = new_df.reset_index()
new_df.rename(columns = {'Patient': 'freq'},inplace = True) 
new_df

② Create graphs in order using For Loop

import matplotlib.pyplot as plt
for num in new_df['id']: #Select the item you want to repeat with in(Select a data frame that eliminates duplication(new_df))
    train2 = train_df.loc[train_df.Patient == num] #Select the column you want to get
    graph = plt.plot(train2["Weeks"],train2["FVC"]) #Get the values for the X and Y axes of the graph
    plt.xlabel("Weeks") #X-axis name
    plt.ylabel("FVC") #Y-axis name
    plt.title("{}".format(num)) #title
    plt.show()

result

As shown in the figure, 176 graphs are displayed. I couldn't make it into a video, so I've included an image and omitted it. スクリーンショット 2020-07-20 10.40.47.png スクリーンショット 2020-07-19 15.13.49.png スクリーンショット 2020-07-19 15.14.10.png

Recommended Posts

Create multiple line charts from a data frame at once using Matplotlib
Instantly create a diagram of 2D data using python's matplotlib
Create a data frame from the acquired boat race text data
Create an API that returns data from a model using turicreate
Create a dataframe from excel using pandas
Python --Read data from a numeric data file and find the multiple regression line.
Create a phylogenetic tree from Biopyton using ClustalW2
Create a binary data parser using Kaitai Struct
Try to create a battle record table with matplotlib from the data of "Schedule-kun"
Create a data collection bot in Python using Selenium
I tried reading data from a file using Node.js.
[Linux] Grep multiple gzip files in a directory at once
Create a GCE instance from a GCR Docker image using terraform
Create multiple users with serial numbers at once with Ansible Playbook
Rsync multiple files at once
Data visualization method using matplotlib (1)
Create a dummy data file
Data visualization method using matplotlib (2)
[numpy] Create a moving window matrix from multidimensional time series data
[Spark Data Frame] Change a column from horizontal to vertical (Scala)
Get data from your website on a regular basis using ScraperWiki
Create a command line tool to convert dollars to yen using Python