網(wǎng)站首頁 編程語言 正文
一、seaborn概述
Seaborn是在matplotlib的基礎(chǔ)上進行了更高級的API封裝,從而使得作圖更加容易,在大多數(shù)情況下使用seaborn就能做出很具有吸引力的圖。詳情請查閱官網(wǎng):seaborn
二、數(shù)據(jù)整理
import seaborn as sns import numpy as np import matplotlib as mpl from matplotlib import pyplot as plt import pandas as pd from datetime import datetime,timedelta %matplotlib inline plt.rcParams['font.sans-serif']=['SimHei'] # 用來正常顯示中文標簽 plt.rcParams['axes.unicode_minus']=False # 用來正常顯示負號 from datetime import datetime
films=['穿過寒冬擁抱你','反貪風暴5:最終章','李茂扮太子','誤殺2','以年為單位的戀愛','黑客帝國:矩陣重啟','雄獅少年','魔法滿屋','汪汪隊立大功大電影','愛情神話'] regions=['中國','英國','澳大利亞','美國','美國','中國','英國','澳大利亞','美國','美國'] bos=['61,181','44,303','42,439','22,984','13,979','61,181','44,303','41,439','20,984','19,979'] persons=['31','23','56','17','9','31','23','56','17','9'] prices=['51','43','56','57','49','51','43','56','57','49'] showdate=['2022-12-03','2022-12-05','2022-12-01','2022-12-02','2022-11-05','2022-12-03','2022-12-05','2022-12-01','2022-12-02','2022-11-05'] ftypes=['劇情','動作','喜劇','劇情','劇情','愛情','動作','動畫','動畫','動畫'] points=['8.1','9.0','7.9','6.7','3.8','8.1','9.0','7.9','6.7','3.8'] filmdescript={ 'ftypes':ftypes, 'bos':bos, 'prices':prices, 'persons':persons, 'regions':regions, 'showdate':showdate, 'points':points }
import numpy as np import pandas as pd cnbo2021top5=pd.DataFrame(filmdescript,index=films) cnbo2021top5[['prices','persons']]=cnbo2021top5[['prices','persons']].astype(int) cnbo2021top5['bos']=cnbo2021top5['bos'].str.replace(',','').astype(int) cnbo2021top5['showdate']=cnbo2021top5['showdate'].astype('datetime64') cnbo2021top5['points']=cnbo2021top5['points'].apply(lambda x:float(x) if x!='' else 0)
cnbo2021top5
# 常用調(diào)色盤 r_hex = '#dc2624' # red, RGB = 220,38,36 dt_hex = '#2b4750' # dark teal, RGB = 43,71,80 tl_hex = '#45a0a2' # teal, RGB = 69,160,162 r1_hex = '#e87a59' # red, RGB = 232,122,89 tl1_hex = '#7dcaa9' # teal, RGB = 125,202,169 g_hex = '#649E7D' # green, RGB = 100,158,125 o_hex = '#dc8018' # orange, RGB = 220,128,24 tn_hex = '#C89F91' # tan, RGB = 200,159,145 g50_hex = '#6c6d6c' # grey-50, RGB = 108,109,108 bg_hex = '#4f6268' # blue grey, RGB = 79,98,104 g25_hex = '#c7cccf' # grey-25, RGB = 199,204,207
color=['#dc2624' ,'#2b4750','#45a0a2','#e87a59','#7dcaa9','#649E7D','#dc8018','#C89F91','#6c6d6c','#4f6268','#c7cccf'] sns.set_palette(color)
01 折線圖
def sinplot(flip=1): x = np.linspace(0, 14, 100) for i in range(1, 7): plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip) sinplot() # 對兩種畫圖進行比較 fig = plt.figure() sns.set() sinplot()
plt.rcParams['font.sans-serif']=['SimHei'] # 用來正常顯示中文標簽 plt.rcParams['axes.unicode_minus']=False # 用來正常顯示負號 plt.figure(figsize=(14,8)) plt.title("中國電影票房2021top10") plt.xlabel("電影名稱") plt.ylabel("電影票房") sns.lineplot(data=cnbo2021top5[['bos']]) plt.xticks(rotation=45)
02 柱形圖
cnbo2021top5ftgb=cnbo2021top5.groupby(['ftypes'])['bos','persons','prices','points'].mean() cnbo2021top5ftgb=cnbo2021top5ftgb.reset_index().replace() cnbo2021top5ftgb
### 02 條形圖 plt.figure(figsize=(14,8)) plt.title("中國電影票房2021top10") sns.barplot(x=cnbo2021top5ftgb['ftypes'],y=cnbo2021top5ftgb['persons']) plt.xlabel("電影類型") plt.ylabel("場均人次") plt.xticks(rotation=45) plt.show()
03 直方圖
### 03 直方圖 plt.figure(figsize=(14,8)) plt.title("中國電影票房2021top10") sns.histplot(x=cnbo2021top5['bos'],bins=15) # x=cnbo2021top5ftgb['ftypes'],y=cnbo2021top5ftgb['persons'] plt.xlabel("電影類型") plt.ylabel("場均人次") plt.xticks(rotation=45) plt.show()
三、繪圖
上面的數(shù)據(jù)只有十部電影,而下面的數(shù)據(jù)是我整理出來的電影數(shù)據(jù):
Excel:300部電影數(shù)據(jù)整理
import pandas as pd cnboo=pd.read_excel("cnboNPPD1.xlsx") cnboo
01 設(shè)定調(diào)色盤
# 設(shè)定調(diào)色盤 sns.set_palette(color) sns.palplot(sns.color_palette(color,11)) # 表示11種顏色
02 柱狀圖
sns.set_palette(color) sns.palplot(sns.color_palette(color,11)) plt.figure(figsize=(25,20)) plt.title('電影票房') plt.xticks(rotation=45) sns.barplot(x='TYPE', y='PRICE', hue='TYPE', data=cnboo)
03 技術(shù)圖
sns.set_palette(color) sns.palplot(sns.color_palette(color,11)) plt.figure(figsize=(15,10)) plt.title('電影票房') plt.xticks(rotation=45) sns.countplot(x='TYPE',data=cnboo)
04 點圖
sns.set_palette(color) sns.palplot(sns.color_palette(color,11)) plt.figure(figsize=(15,10)) plt.title('電影票房') plt.xticks(rotation=45) sns.pointplot(x='TYPE',y='PRICE',data=cnboo) plt.show()
sns.set_palette(color) sns.palplot(sns.color_palette(color,11)) plt.figure(figsize=(25,10)) plt.title('電影票房') plt.xticks(rotation=45) sns.pointplot(x='TYPE',y='PRICE',hue='REGION',data=cnboo) plt.show()
05 箱型圖
### 05 箱型圖 sns.set_palette(color) sns.palplot(sns.color_palette(color,11)) plt.figure(figsize=(35,10)) plt.title('電影票房') plt.xticks(rotation=45) sns.boxplot(x='TYPE',y='PERSONS',hue='REGION',data=cnboo) # ,markers=['^','o'],linestyles=['-','--'] plt.show() # 圖中的單個點代表在此數(shù)據(jù)當中的異常值
06 小提琴圖
### 06 小提琴圖 sns.set_palette(color) sns.palplot(sns.color_palette(color,11)) plt.figure(figsize=(35,10)) plt.title('電影票房') plt.xticks(rotation=45) sns.violinplot(x='TYPE',y='PRICE',hue='REGION',data=cnboo) # ,markers=['^','o'],linestyles=['-','--'] plt.show()
繪制橫著的小提琴圖:
sns.set_palette(color) sns.palplot(sns.color_palette(color,11)) plt.figure(figsize=(35,10)) plt.title('電影票房') plt.xticks(rotation=45) sns.violinplot(x='PERSONS',y='PRICE',hue='REGION',data=cnboo,orient='h') plt.show()
原文鏈接:https://blog.csdn.net/wxfighting/article/details/123454072
相關(guān)推薦
- 2022-08-30 C語言示例講解結(jié)構(gòu)體的聲明與初始化方法_C 語言
- 2022-03-23 C語言實現(xiàn)打印楊輝三角的方法詳細(三種方法)_C 語言
- 2022-07-06 R語言可視化ggplot2繪制24小時動態(tài)血糖圖_R語言
- 2022-08-18 Android?Canva實現(xiàn)漸變進度條_Android
- 2022-04-18 ASP.Net?Core?MVC基礎(chǔ)系列之獲取配置信息_基礎(chǔ)應(yīng)用
- 2022-10-24 Numpy?數(shù)據(jù)處理?ndarray使用詳解_python
- 2022-11-30 Android實現(xiàn)一鍵鎖屏功能_Android
- 2022-10-23 C#各種異常處理方式總結(jié)_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支