1. 程式人生 > >簡單繪圖,python實現

簡單繪圖,python實現

Python資料處理從零開始----第四章(視覺化)(1)Matplotlib包 :https://www.jianshu.com/p/c07723faf5d7

 

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np

### 畫圖 直方圖
#import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv("./HR.csv", header=0)
plt.title("SALARY")
plt.xlabel("salary")
plt.ylabel("Number")
#plt.xticks("刻度","刻度標籤")
plt.xticks(np.arange(len(df["salary"].value_counts())), df["salary"].value_counts().index)
plt.bar(np.arange(len(df["salary"].value_counts())), df["salary"].value_counts(), width=0.5)
plt.text(1, 7318,7318,ha="center", va="bottom")
plt.show()

### 箱線圖
## 展示的資料處理
df = df.sort_values(by="department")
print(df.head())
AMH = []
Levels = df['department'].unique()
print(Levels)
for i in Levels:
    AMH.append(df.loc[df.department==i, 'average_monthly_hours'])
    
# 設定圖形的顯示風格
plt.style.use('ggplot')

# 設定中文和負號正常顯示
plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
plt.rcParams['axes.unicode_minus'] = False
plt.boxplot(x = AMH,
            patch_artist=True, labels= Levels,
            showmeans=True,
            boxprops = {'color':'black','facecolor':'#9999ff'},
            flierprops = {'marker':'o','markerfacecolor':'red','color':'black'},
            meanprops = {'marker':'D','markerfacecolor':'indianred'},
            medianprops = {'linestyle':'--','color':'orange'}
            )
plt.show()