怎麼用matplotlib畫出漂亮的分析圖表
今日錦囊
特徵錦囊:怎麼用matplotlib畫出漂亮的分析圖表
???? Index
???? 資料集引入
# 匯入一些常用包import pandas as pdimport numpy as npimport seaborn as sns%matplotlib inlineimport matplotlib.pyplot as pltimport matplotlib as mplplt.style.use('fivethirtyeight')#解決中文顯示問題,Macfrom matplotlib.font_manager import FontProperties# 檢視本機plt的有效styleprint(plt.style.available)# 根據本機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']# 資料集匯入# 引入第 1 個資料集 Salary_Ranges_by_Job_Classificationsalary_ranges = pd.read_csv('./data/Salary_Ranges_by_Job_Classification.csv')# 引入第 2 個資料集 GlobalLandTemperaturesByCityclimate = pd.read_csv('./data/GlobalLandTemperaturesByCity.csv')# 移除缺失值climate.dropna(axis=0, inplace=True)# 只看中國# 日期轉換, 將dt 轉換為日期,取年份, 注意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()
???? 折線圖
# 選擇上海部分天氣資料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', ># 多條折線圖df123.plot()plt.title('AverageTemperature Of 3 City')plt.ylabel('Number of immigrants')plt.xlabel('Years')plt.show()???? 餅圖
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] #控制餅圖分離狀態,越大越分離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()???? 散點圖 anhui/
# 選擇上海部分天氣資料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', ># 散點圖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', >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()???? 直方圖
# 選擇上海部分天氣資料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 histogramplt.ylabel('Number of month') # add y-labelplt.xlabel('AverageTemperature') # add x-labelplt.show()???? 條形圖
# 選擇上海部分天氣資料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()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30239065/viewspace-2742977/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 停車場地圖怎樣好看,停車場怎麼畫簡單又漂亮地圖
- Python畫圖——matplotlib(普通折線圖)Python
- python中Matplotlib是什麼?怎麼用?Python
- matplotlib 畫圖直接寫入excelExcel
- 公司位置怎麼上地圖,怎麼在地圖上畫出區域地圖
- 2023 年了,React Native 中怎麼畫圖表?React Native
- matplotlib畫圖未顯示,以及PyCharm中 %matplotlib inline報錯PyCharminline
- 使用者PHP圖表包裝程式建立漂亮的圖表的方法PHP
- python matplotlib畫圖是設定線寬Python
- 停車場構造圖怎麼畫好看,簡單的停車場地圖怎麼畫圖地圖
- [Python] Matplotlib 圖表的繪製和美化技巧Python
- 用matplotlib散點圖用餅圖示記
- python matplotlib畫圖設定座標軸刻度的字型大小Python
- Python Matplotlib繪製氣溫圖表Python
- echarts搞定各種地圖(想怎麼畫就怎麼畫)Echarts地圖
- (資料科學學習手札149)用matplotlib輕鬆繪製漂亮的表格資料科學
- 停車場地圖怎麼畫出來,商場地下車庫導航地圖怎麼做地圖
- laravel用phpWord匯出漂亮的word檔案LaravelPHP
- 停車場分佈圖怎麼畫,地下停車場的樓梯怎麼畫
- 1、2、3維圖見過,用Python畫出來的六維圖見過麼?Python
- 位置分佈圖怎麼畫,怎麼做網點分佈圖
- 位置分佈圖怎麼畫,怎麼做地圖網點分佈圖地圖
- Matplotlib 視覺化最有價值的 50 個圖表視覺化
- 地下車庫地圖怎麼畫的,怎麼畫停車場最最最最簡單的地圖
- 業務流程圖該怎麼畫?流程圖
- Ubuntu上關於PyCharm不能用matplotlib畫圖的解決方案UbuntuPyCharm
- 【matplotlib基礎】--動畫動畫
- 使用 ConstraintLayout 製作漂亮的動畫AI動畫
- 流程圖怎麼畫?生產流程圖模板分享流程圖
- 分析攻擊IP來源地並畫出餅圖
- 停車場方點陣圖怎麼畫,停車場怎麼畫簡單一點的
- 園區地圖路線規劃圖怎麼做的?廠區三維圖怎麼畫好看?地圖
- python matplotlib畫圖改變圖示題和座標軸標題的字型大小Python
- matplotlib | Python強大的作圖工具,讓你從此駕馭圖表Python
- 商場導覽圖怎麼畫?商場路線指引圖怎麼做?
- delphi 畫圖表,曲線圖
- matplotlib畫圖教程,設定座標軸標籤和間距
- 用AI繪畫怎麼可以把照片一鍵生成漫畫圖?詳細ai繪畫教程來啦!AI