> AI Gravity :: Matplotlib
본문으로 바로가기

Matplotlib

category AI Library/Pandas 2021. 11. 14. 18:22

1. Extract data

Code:

import seaborn as sns				#use seaborn lib
import matplotlib.pyplot as plt		#use matplotlib lib
tips=sns.load_dataset("tips")		#get "tips" dataset
print(tips)
print(type(tips))

Result:


2. Histogram Graph

Code:

histogram_plot=plt.figure()				#make many graphs in one figure

axes1=histogram_plot.add_subplot(1,2,1)			#make 1st subplot in (1x2) 
axes1.hist(tips['total_bill'],bins=10)			#set 'total_bill' as histogram with ten x-axis
axes1.set_title('Histogram of Total Bill')		
axes1.set_xlabel('Frequency')
axes1.set_ylabel('Total Bill')

axes2=histogram_plot.add_subplot(1,2,2)			#make 2st subplot in (1x2)
axes2.hist(tips['tip'],bins=10)				#set 'tip' as histogram with ten x-axis
axes2.set_title('Histogram of Tip')
axes2.set_xlabel('Frequency')
axes2.set_ylabel('Tip')

Result:


3. Scatterplot Graph

Code:

scatter_plot=plt.figure()
axes1=scatter_plot.add_subplot(1,1,1)
axes1.scatter(tips['total_bill'],tips['tip'])
axes1.set_title('Scatterplot of Total Bill vs Tip')
axes1.set_xlabel('Total Bill')
axes1.set_ylabel('Tips')

Result:


4. Boxplot Graph

Code:

boxplot=plt.figure()
axes1=boxplot.add_subplot(1,1,1)

axes1.boxplot([tips[tips['sex']=='Female']['tip'],
               tips[tips['sex']=='Male']['tip']],
              labels=['Female','Male'])

axes1.set_xlabel('Sex')
axes1.set_ylabel('Tip')
axes1.set_title('Boxplot of Tips by Sex')

Result:


5. Multivariate Graph

Code:

def recode_sex(sex):
    if sex=='Remale':
        return 0
    else:
        return 1

tips['sex_color']=tips['sex'].apply(recode_sex)		#update 'sex_color' to tips

scatter_plot=plt.figure()
axes1=scatter_plot.add_subplot(1,1,1)
axes1.scatter(
    x=tips['total_bill'],
    y=tips['tip'],
    s=tips['size']*10,
    c=tips['sex_color'],
    alpha=0.5)
axes1.set_title('Total Bill vs tip Colored by Sex and Sized by Size')
axes1.set_xlabel('Total Bill')
axes1.set_ylabel('Tip')

Result:

'AI Library > Pandas' 카테고리의 다른 글

Graph Type  (0) 2021.11.15
Seaborn Library  (0) 2021.11.14
Anscombe's quartet  (0) 2021.11.14
Data Extract  (0) 2021.11.12
Setting for Pandas  (0) 2021.11.12