最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python利用matplotlib畫出漂亮的分析圖表

 更新時間:2022年07月04日 15:47:47   作者:機器學(xué)習(xí)初學(xué)者  
這篇文章主要介紹了Python利用matplotlib畫出漂亮的分析圖表,文章首先引入數(shù)據(jù)集展開詳情,需要的朋友可以參考一下

前言

作為一名優(yōu)秀的分析師,還是得學(xué)會一些讓圖表漂亮的技巧,這樣子拿出去才更加有面子哈哈。好了,今天的錦囊就是介紹一下各種常見的圖表,可以怎么來畫吧。

數(shù)據(jù)集引入

首先引入數(shù)據(jù)集,我們還用一樣的數(shù)據(jù)集吧,分別是 Salary_Ranges_by_Job_Classification以及 GlobalLandTemperaturesByCity。(具體數(shù)據(jù)集可以后臺回復(fù) plot獲?。?/p>

# 導(dǎo)入一些常用包
import pandas as pd
import numpy as np
import seaborn as sns

%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib as mpl
plt.style.use('fivethirtyeight')

#解決中文顯示問題,Mac
from matplotlib.font_manager import FontProperties

# 查看本機plt的有效style
print(plt.style.available)
# 根據(jù)本機available的style,選擇其中一個,因為之前知道ggplot很好看,所以我選擇了它
mpl.style.use(['ggplot'])

# ['_classic_test', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark-palette', 'seaborn-dark', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'seaborn', 'Solarize_Light2']

# 數(shù)據(jù)集導(dǎo)入

# 引入第 1 個數(shù)據(jù)集 Salary_Ranges_by_Job_Classification
salary_ranges = pd.read_csv('./data/Salary_Ranges_by_Job_Classification.csv')

# 引入第 2 個數(shù)據(jù)集 GlobalLandTemperaturesByCity
climate = pd.read_csv('./data/GlobalLandTemperaturesByCity.csv')
# 移除缺失值
climate.dropna(axis=0, inplace=True)
# 只看中國
# 日期轉(zhuǎn)換, 將dt 轉(zhuǎn)換為日期,取年份, 注意map的用法
climate['dt'] = pd.to_datetime(climate['dt'])
climate['year'] = climate['dt'].map(lambda value: value.year)
climate_sub_china = climate.loc[climate['Country'] == 'China']
climate_sub_china['Century'] = climate_sub_china['year'].map(lambda x:int(x/100 +1))
climate.head()

折線圖

折線圖是比較簡單的圖表了,也沒有什么好優(yōu)化的,顏色看起來順眼就好了。下面是從網(wǎng)上找到了顏色表,可以從中挑選~

# 選擇上海部分天氣數(shù)據(jù)
df1 = climate.loc[(climate['Country']=='China')&(climate['City']=='Shanghai')&(climate['dt']>='2010-01-01')]\
.loc[:,['dt','AverageTemperature']]\
.set_index('dt')
df1.head()

# 折線圖
df1.plot(colors=['lime'])
plt.title('AverageTemperature Of ShangHai')
plt.ylabel('Number of immigrants')
plt.xlabel('Years')
plt.show()

上面這是單條折線圖,多條折線圖也是可以畫的,只需要多增加幾列。

# 多條折線圖
df1 = climate.loc[(climate['Country']=='China')&(climate['City']=='Shanghai')&(climate['dt']>='2010-01-01')]\
.loc[:,['dt','AverageTemperature']]\
.rename(columns={'AverageTemperature':'SH'})
df2 = climate.loc[(climate['Country']=='China')&(climate['City']=='Tianjin')&(climate['dt']>='2010-01-01')]\
.loc[:,['dt','AverageTemperature']]\
.rename(columns={'AverageTemperature':'TJ'})
df3 = climate.loc[(climate['Country']=='China')&(climate['City']=='Shenyang')&(climate['dt']>='2010-01-01')]\
.loc[:,['dt','AverageTemperature']]\
.rename(columns={'AverageTemperature':'SY'})
# 合并
df123 = df1.merge(df2, how='inner', on=['dt'])\
.merge(df3, how='inner', on=['dt'])\
.set_index(['dt'])
df123.head()

# 多條折線圖
df123.plot()
plt.title('AverageTemperature Of 3 City')
plt.ylabel('Number of immigrants')
plt.xlabel('Years')
plt.show()

餅圖

接下來是畫餅圖,我們可以優(yōu)化的點多了一些,比如說從餅塊的分離程度,我們先畫一個“低配版”的餅圖。

df1 = salary_ranges.groupby('SetID', axis=0).sum()

 
# “低配版”餅圖
df1['Step'].plot(kind='pie', figsize=(7,7),
autopct='%1.1f%%',
shadow=True)
plt.axis('equal')
plt.show()

# “高配版”餅圖
colors = ['lightgreen', 'lightblue'] #控制餅圖顏色 ['lightgreen', 'lightblue', 'pink', 'purple', 'grey', 'gold']
explode=[0, 0.2] #控制餅圖分離狀態(tài),越大越分離

df1['Step'].plot(kind='pie', figsize=(7, 7),
autopct = '%1.1f%%', startangle=90,
shadow=True, labels=None, pctdistance=1.12, colors=colors, explode = explode)
plt.axis('equal')
plt.legend(labels=df1.index, loc='upper right', fontsize=14)
plt.show()

散點圖

散點圖可以優(yōu)化的地方比較少了,ggplot2的配色都蠻好看的,正所謂style選的好,省很多功夫!

# 選擇上海部分天氣數(shù)據(jù)
df1 = climate.loc[(climate['Country']=='China')&(climate['City']=='Shanghai')&(climate['dt']>='2010-01-01')]\
.loc[:,['dt','AverageTemperature']]\
.rename(columns={'AverageTemperature':'SH'})

df2 = climate.loc[(climate['Country']=='China')&(climate['City']=='Shenyang')&(climate['dt']>='2010-01-01')]\
.loc[:,['dt','AverageTemperature']]\
.rename(columns={'AverageTemperature':'SY'})
# 合并
df12 = df1.merge(df2, how='inner', on=['dt'])
df12.head()

# 散點圖
df12.plot(kind='scatter', x='SH', y='SY', figsize=(10, 6), color='darkred')
plt.title('Average Temperature Between ShangHai - ShenYang')
plt.xlabel('ShangHai')
plt.ylabel('ShenYang')
plt.show()

面積圖

# 多條折線圖
df1 = climate.loc[(climate['Country']=='China')&(climate['City']=='Shanghai')&(climate['dt']>='2010-01-01')]\
.loc[:,['dt','AverageTemperature']]\
.rename(columns={'AverageTemperature':'SH'})
df2 = climate.loc[(climate['Country']=='China')&(climate['City']=='Tianjin')&(climate['dt']>='2010-01-01')]\
.loc[:,['dt','AverageTemperature']]\
.rename(columns={'AverageTemperature':'TJ'})
df3 = climate.loc[(climate['Country']=='China')&(climate['City']=='Shenyang')&(climate['dt']>='2010-01-01')]\
.loc[:,['dt','AverageTemperature']]\
.rename(columns={'AverageTemperature':'SY'})
# 合并
df123 = df1.merge(df2, how='inner', on=['dt'])\
.merge(df3, how='inner', on=['dt'])\
.set_index(['dt'])
df123.head()

colors = ['red', 'pink', 'blue'] #控制餅圖顏色 ['lightgreen', 'lightblue', 'pink', 'purple', 'grey', 'gold']
df123.plot(kind='area', stacked=False,
figsize=(20, 10), colors=colors)
plt.title('AverageTemperature Of 3 City')
plt.ylabel('AverageTemperature')
plt.xlabel('Years')
plt.show()

直方圖

# 選擇上海部分天氣數(shù)據(jù)
df = climate.loc[(climate['Country']=='China')&(climate['City']=='Shanghai')&(climate['dt']>='2010-01-01')]\
.loc[:,['dt','AverageTemperature']]\
.set_index('dt')
df.head()

# 最簡單的直方圖
df['AverageTemperature'].plot(kind='hist', figsize=(8,5), colors=['grey'])
plt.title('ShangHai AverageTemperature Of 2010-2013') # add a title to the histogram
plt.ylabel('Number of month') # add y-label
plt.xlabel('AverageTemperature') # add x-label
plt.show()

條形圖

# 選擇上海部分天氣數(shù)據(jù)
df = climate.loc[(climate['Country']=='China')&(climate['City']=='Shanghai')&(climate['dt']>='2010-01-01')]\
.loc[:,['dt','AverageTemperature']]\
.set_index('dt')
df.head()

df.plot(kind='bar', figsize = (10, 6))
plt.xlabel('Month')
plt.ylabel('AverageTemperature')
plt.title('AverageTemperature of shanghai')
plt.show()

df.plot(kind='barh', figsize=(12, 16), color='steelblue')
plt.xlabel('AverageTemperature')
plt.ylabel('Month')
plt.title('AverageTemperature of shanghai')
plt.show()

到此這篇關(guān)于Python利用matplotlib畫出漂亮的分析圖表的文章就介紹到這了,更多相關(guān)Python 繪制分析圖表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python編寫生成驗證碼的腳本的教程

    Python編寫生成驗證碼的腳本的教程

    這篇文章主要介紹了Python編寫生成驗證碼的腳本的教程,驗證碼在web編程中幾乎是必備的功能,需要的朋友可以參考下
    2015-05-05
  • Python使用定時調(diào)度任務(wù)的方式

    Python使用定時調(diào)度任務(wù)的方式

    Python 有幾種方法可以定時調(diào)度一個任務(wù),這就是我們將在本文中學(xué)習(xí)的內(nèi)容。接下倆下邊將給大家介紹5種Python使用定時調(diào)度任務(wù)方式,需要的朋友可以參考下面文章內(nèi)容吧
    2021-09-09
  • python驗證碼識別實例代碼

    python驗證碼識別實例代碼

    這篇文章主要介紹了python驗證碼識別實例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • pytorch 如何查看數(shù)據(jù)類型和大小

    pytorch 如何查看數(shù)據(jù)類型和大小

    這篇文章主要介紹了pytorch 實現(xiàn)查看數(shù)據(jù)類型和大小的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python中turtle.write方法使用說明

    Python中turtle.write方法使用說明

    turtle模塊以面向?qū)ο蠛兔嫦蜻^程的方式提供turtle圖形基元,由于它使用Tkinter作為基礎(chǔ)圖形,因此需要安裝有Tk支持的Python版本,下面這篇文章主要給大家介紹了關(guān)于Python中turtle.write方法使用說明的相關(guān)資料,需要的朋友可以參考下
    2022-02-02
  • python not關(guān)鍵字實例用法

    python not關(guān)鍵字實例用法

    在本篇文章里小編給大家整理的是一篇關(guān)于python not關(guān)鍵字實例用法,有興趣的朋友們可以學(xué)習(xí)下。
    2021-04-04
  • Python實現(xiàn)圖書借閱管理系統(tǒng)

    Python實現(xiàn)圖書借閱管理系統(tǒng)

    這篇文章主要為大家詳細介紹了Python實現(xiàn)圖書借閱管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • pandas計算相關(guān)系數(shù)corr返回空的問題解決

    pandas計算相關(guān)系數(shù)corr返回空的問題解決

    本文主要介紹了pandas計算相關(guān)系數(shù)corr返回空的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • python數(shù)據(jù)封裝json格式數(shù)據(jù)

    python數(shù)據(jù)封裝json格式數(shù)據(jù)

    本次內(nèi)容是小編在網(wǎng)上整理的關(guān)于如何python數(shù)據(jù)封裝json格式的內(nèi)容總結(jié),有興趣的讀者們參考下。
    2018-03-03
  • 關(guān)于yolov8訓(xùn)練的一些改動及注意事項

    關(guān)于yolov8訓(xùn)練的一些改動及注意事項

    Yolo是一種目標檢測算法,目標檢測的任務(wù)是從圖片中找出物體并給出其類別和位置,這篇文章主要給大家介紹了關(guān)于yolov8訓(xùn)練的一些改動及注意事項,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-02-02

最新評論

黄山市| 峨眉山市| 合江县| 青河县| 西盟| 阜平县| 青神县| 玉门市| 安吉县| 岚皋县| 河池市| 河北区| 泰宁县| 宕昌县| 乌鲁木齐市| 八宿县| 建宁县| 波密县| 台中市| 札达县| 凉山| 沈阳市| 阿拉善右旗| 阳原县| 枝江市| 白银市| 宁远县| 广平县| 喀喇| 五莲县| 什邡市| 巴南区| 兴城市| 滕州市| 沧源| 乌鲁木齐县| 阿鲁科尔沁旗| 沂水县| 安庆市| 偃师市| 淮安市|