Matplotlib視覺化庫的使用
一、pandas匯入資料&matplotlib基本繪圖
# _*_ coding: utf-8 _*_
import pandas as pd
import matplotlib.pyplot as plt
#
# Author: yz
# Date: 2017-12-3
#
'''
pandas匯入資料和date格式轉換
matplotlib基本的繪圖:橫縱座標標籤,標題,座標值旋轉等
'''
# 匯入資料
unrate = pd.read_csv("data/UNRATE.csv")
unrate["DATE"] = pd.to_datetime(unrate["DATE" ]) # 1948/1/1 -> 1948-01-01
# print(unrate.head(10))
'''
DATE VALUE
0 1948-01-01 3.4
1 1948-02-01 3.8
2 1948-03-01 4.0
3 1948-04-01 3.9
4 1948-05-01 3.5
5 1948-06-01 3.6
6 1948-07-01 3.6
7 1948-08-01 3.9
8 1948-09-01 3.8
9 1948-10-01 3.7
'''
# plt.plot()
# plt.show()
'''
While the y-axis looks fine, the x-axis tick labels are too close together and are unreadable
We can rotate the x-axis tick labels by 90 degrees so they don't overlap
We can specify degrees of rotation using a float or integer value.
'''
# first_twelve = unrate[0:12]
# plt.plot(first_twelve["DATE"], first_twelve["VALUE"])
# plt.xticks(rotation=45) # x座標值太長時,可以讓其旋轉再顯示
# # print(help(plt.xticks))
# plt.show()
'''
xlabel(): accepts a string value, which gets set as the x-axis label.
ylabel(): accepts a string value, which is set as the y-axis label.
title(): accepts a string value, which is set as the plot title.
'''
first_twelve = unrate[0:12]
plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
plt.xticks(rotation=45)
plt.xlabel('Month')
plt.ylabel('Unemployment Rate')
plt.title('Monthly Unemployment Trends, 1948')
plt.show()
二、增加子圖&指定影象大小和線條顏色&新增標籤並指定位置
# _*_ coding: utf-8 _*_
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
#
# Author: yz
# Date: 2017-12-3
#
'''
增加子圖 add_subplot(first,second,index)
指定影象大小 plt.figure(figsize=(12, 6))
指定線條的顏色 plt.plot(unrate[0:12]["MONTH"], unrate[0:12]["VALUE"], c="red")
新增標籤並指定位置 plt.plot(subset["MONTH"], subset["VALUE"], c=colors[i], label=label) plt.legend(loc='upper left')
'''
'''
add_subplot(first,second,index) first means number of Row,second means number of Column.
'''
# fig = plt.figure()
# ax1 = fig.add_subplot(3, 2, 1)
# ax2 = fig.add_subplot(3, 2, 2)
# ax3 = fig.add_subplot(3, 2, 3)
# ax6 = fig.add_subplot(3, 2, 6)
# ax1.plot(np.random.randint(1, 5, 5), np.arange(5))
# ax2.plot(np.arange(10) * 3, np.arange(10))
# plt.show()
'''
指定顏色和大小
'''
unrate = pd.read_csv("data/UNRATE.csv")
unrate["DATE"] = pd.to_datetime(unrate["DATE"])
unrate["MONTH"] = unrate["DATE"].dt.month
# fig = plt.figure(figsize=(12, 6)) # figsize 影象大小
# plt.plot(unrate[0:12]["MONTH"], unrate[0:12]["VALUE"], c="red") # c 指定線條顏色
# plt.plot(unrate[12:24]["MONTH"], unrate[12:24]["VALUE"], c="green")
# plt.show()
'''
lable
'''
fig = plt.figure(figsize=(10, 6))
colors = ['red', 'blue', 'green', 'orange', 'black']
for i in range(5):
start_index = i * 12
end_index = (i + 1) * 12
label = str(1948 + i)
subset = unrate[start_index:end_index]
plt.plot(subset["MONTH"], subset["VALUE"], c=colors[i], label=label) # 新增lable
# 指定lable的位置 'best' 'upper right/left' 'lower right/left' 'right' 'center right/left' 'upper/lower center' 'center'
plt.legend(loc='upper left')
# print(help(plt.legend))
plt.xlabel('Month, Integer')
plt.ylabel('Unemployment Rate, Percent')
plt.title('Monthly Unemployment Trends, 1948-1952')
plt.show()
三、柱狀圖&散點圖
# _*_ coding: utf-8 _*_
import pandas as pd
import matplotlib.pyplot as plt
from numpy import arange
#
# Author: yz
# Date: 2017-12-3
#
'''
柱狀圖 fig, ax = plt.subplots() ax.bar(bar_positions, bar_heights, 0.5)
散點圖
'''
#
# 匯入資料
#
reviews = pd.read_csv("data/fandango_scores.csv")
cols = ['FILM', 'RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
norm_reviews = reviews[cols]
# print(norm_reviews[:1])
#
# 柱狀圖
#
# num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
# bar_heights = norm_reviews.ix[0, num_cols].values
# # print(bar_heights) # [4.2999999999999998 3.5499999999999998 3.8999999999999999 4.5 5.0]
# bar_positions = arange(5) + 0.75
# # print(bar_positions) # [1 2 3 4 5]
# fig, ax = plt.subplots()
# ax.bar(bar_positions, bar_heights, 0.5)
# plt.show()
#
# 將座標值改為標籤 ax.set_xticklabels(num_cols, rotation=45)
#
# num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
# bar_heights = norm_reviews.ix[0, num_cols].values
# # print(bar_heights) # [4.2999999999999998 3.5499999999999998 3.8999999999999999 4.5 5.0]
# bar_positions = arange(5) + 0.75
# # print(bar_positions) # [1 2 3 4 5]
# fig, ax = plt.subplots()
# ax.bar(bar_positions, bar_heights, 0.5)
#
# tick_positions = range(1,6)
# ax.set_xticks(tick_positions)
# ax.set_xticklabels(num_cols, rotation=45)
#
# ax.set_xlabel('Rating Source')
# ax.set_ylabel('Average Rating')
# ax.set_title('Average User Rating For Avengers: Age of Ultron (2015)')
# plt.show()
# fig, ax = plt.subplots()
# # ax.hist(norm_reviews['Fandango_Ratingvalue'])
# # ax.hist(norm_reviews['Fandango_Ratingvalue'],bins=20)
# ax.hist(norm_reviews['Fandango_Ratingvalue'], range=(3, 5),bins=20)
# plt.show()
#
# 散點圖
#
fig, ax = plt.subplots()
ax.scatter(norm_reviews['Fandango_Ratingvalue'], norm_reviews['RT_user_norm'])
# ax.scatter([4.5, 3], [4.3, 4]) # (4.5, 4.3) (3, 4)
ax.set_xlabel('Fandango')
ax.set_ylabel('Rotten Tomatoes')
plt.show()
曲線圖&座標的設定
# _*_ coding: utf-8 _*_
import pandas as pd
import matplotlib.pyplot as plt
#
# Author: yz
# Date: 2017-12-3
#
'''
曲線圖
'''
women_degrees = pd.read_csv('data/percent-bachelors-degrees-women-usa.csv')
# plt.plot(women_degrees['Year'], women_degrees['Biology'])
# plt.show()
# plt.plot(women_degrees['Year'], women_degrees['Biology'], c='blue', label='Women')
# plt.plot(women_degrees['Year'], 100-women_degrees['Biology'], c='green', label='Men')
# plt.legend(loc='upper right')
# plt.title('Percentage of Biology Degrees Awarded By Gender')
# plt.show()
# fig, ax = plt.subplots()
# ax.plot(women_degrees['Year'], women_degrees['Biology'], label='Women')
# ax.plot(women_degrees['Year'], 100-women_degrees['Biology'], label='Men')
#
# ax.tick_params(bottom="off", top="off", left="off", right="off")
# ax.set_title('Percentage of Biology Degrees Awarded By Gender')
# ax.legend(loc="upper right")
#
# plt.show()
# fig, ax = plt.subplots()
# ax.plot(women_degrees['Year'], women_degrees['Biology'], c='blue', label='Women')
# ax.plot(women_degrees['Year'], 100-women_degrees['Biology'], c='green', label='Men')
# ax.tick_params(bottom="off", top="off", left="off", right="off")
#
# for key,spine in ax.spines.items():
# spine.set_visible(False)
# # End solution code.
# ax.legend(loc='upper right')
# plt.show()
major_cats = ['Biology', 'Computer Science', 'Engineering', 'Math and Statistics']
fig = plt.figure(figsize=(12, 12))
for sp in range(0,4):
ax = fig.add_subplot(2,2,sp+1)
ax.plot(women_degrees['Year'], women_degrees[major_cats[sp]], c='blue', label='Women')
ax.plot(women_degrees['Year'], 100-women_degrees[major_cats[sp]], c='green', label='Men')
# Add your code here.
# Calling pyplot.legend() here will add the legend to the last subplot that was created.
plt.legend(loc='upper right')
plt.show()
major_cats = ['Biology', 'Computer Science', 'Engineering', 'Math and Statistics']
fig = plt.figure(figsize=(12, 12))
for sp in range(0,4):
ax = fig.add_subplot(2,2,sp+1)
ax.plot(women_degrees['Year'], women_degrees[major_cats[sp]], c='blue', label='Women')
ax.plot(women_degrees['Year'], 100-women_degrees[major_cats[sp]], c='green', label='Men')
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(major_cats[sp])
ax.tick_params(bottom="off", top="off", left="off", right="off")
# Calling pyplot.legend() here will add the legend to the last subplot that was created.
plt.legend(loc='upper right')
plt.show()
線條的種類
# _*_ coding: utf-8 _*_
import pandas as pd
import matplotlib.pyplot as plt
#
# Author: yz
# Date: 2017-12-3
#
women_degrees = pd.read_csv('data/percent-bachelors-degrees-women-usa.csv')
major_cats = ['Biology', 'Computer Science', 'Engineering', 'Math and Statistics']
# cb_dark_blue = (0/255, 107/255, 164/255)
# cb_orange = (255/255, 128/255, 14/255)
#
# fig = plt.figure(figsize=(12, 12))
#
# for sp in range(0,4):
# ax = fig.add_subplot(2,2,sp+1)
# # The color for each line is assigned here.
# ax.plot(women_degrees['Year'], women_degrees[major_cats[sp]], c=cb_dark_blue, label='Women')
# ax.plot(women_degrees['Year'], 100-women_degrees[major_cats[sp]], c=cb_orange, label='Men')
# for key,spine in ax.spines.items():
# spine.set_visible(False)
# ax.set_xlim(1968, 2011)
# ax.set_ylim(0,100)
# ax.set_title(major_cats[sp])
# ax.tick_params(bottom="off", top="off", left="off", right="off")
#
# plt.legend(loc='upper right')
# plt.show()
#Setting Line Width
cb_dark_blue = (0/255, 107/255, 164/255)
cb_orange = (255/255, 128/255, 14/255)
fig = plt.figure(figsize=(12, 12))
for sp in range(0,4):
ax = fig.add_subplot(2,2,sp+1)
# Set the line width when specifying how each line should look.
ax.plot(women_degrees['Year'], women_degrees[major_cats[sp]], c=cb_dark_blue, label='Women', linewidth=10)
ax.plot(women_degrees['Year'], 100-women_degrees[major_cats[sp]], c=cb_orange, label='Men', linewidth=10)
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(major_cats[sp])
ax.tick_params(bottom="off", top="off", left="off", right="off")
plt.legend(loc='upper right')
plt.show()
相關推薦
Matplotlib視覺化庫的使用
一、pandas匯入資料&matplotlib基本繪圖 # _*_ coding: utf-8 _*_ import pandas as pd import matplotlib.pyplot as plt # # Author: yz # D
Python視覺化庫 python視覺化--matplotlib
轉自小小蒲公英原文用Python視覺化庫 現如今大資料已人盡皆知,但在這個資訊大爆炸的時代裡,空有海量資料是無實際使用價值,更不要說幫助管理者進行業務決策。那麼資料有什麼價值呢?用什麼樣的手段才能把資料的價值直觀而清晰的表達出來?答案是要提供像人眼一樣的直覺的、互動的和反應靈敏的視覺化環境。資料
【python庫】matplotlib視覺化
官網《—— 本po環境 maxos 10.12.3 python2.7 模組下載,python2 pip install matplotlib 如果是python3 pip3 install matplotlib pyplot模組 impo
Python視覺化庫Matplotlib的使用
一。匯入資料import pandas as pd unrate = pd.read_csv('unrate.csv') unrate['DATE'] = pd.to_datetime(unrate['DATE']) print(unrate.head(12)) 結果如下:
視覺化庫-Matplotlib-盒圖(第四天)
盒圖由五個數值點組成,最小觀測值,下四分位數,中位數,上四分位數,最大觀測值 IQR = Q3 - Q1 Q3表示上四分位數, Q1表示下四分位數,IQR表示盒圖的長度 最小觀測值 min =Q1 - 1.5*IQR 最大觀測值 max=Q3 + 1.5*IQR , 大於最大值或
視覺化庫-Matplotlib-直方圖(第四天)
1.plt.hist(array, bins, color) # array表示數值, bins表示的是bin的範圍 data = np.random.normal(0, 20, 1000) # 畫出bins值 bins = np.arange(-100, 100, 5) plt.hist
視覺化庫-Matplotlib-散點圖(第四天)
1. 畫基本的散點圖 plt.scatterdata[:, 0], data[:, 1], marker='o', color='r', label='class1', alpha=0.4) np.random.multivariate_normal 根據均值和協方差生成多行列表 mu_vec1 =
視覺化庫-Matplotlib-3D圖(第四天)
1. 畫三維圖片圖 axes = Axes3D(fig)這一步將二維座標轉換為三維座標,axes.plot_surface() import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axe
Python視覺化庫matplotlib庫各種圖demo
關聯分析、數值比較:散點圖、曲線圖 分佈分析:灰度圖、密度圖 涉及分類的分析:柱狀圖、箱式圖 核密度估計(Kernel density estimation),是一種用於估計概率密度函式的非引數方法,採用平滑的峰值函式(“核”)來擬合觀察到的資料點,從而對真
視覺化庫----Matplotlib+Pandas高階篇及應用
以下文件的原始檔,我做成網頁了,可以直接點選這裡 一、柱狀圖詳解 import matplotlib.pyplot as plt import numpy as np plt.rcParams["font.sans-serif"]=['Si
matplotlib 視覺化 cmap colormap
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
pyecharts視覺化庫的使用
pyecharts介紹 pyechats是一個用於資料視覺化的包。 Echats是百度開源的一個數據視覺化js庫,主要用於資料視覺化,pyecharts 是一個用於生成Echarts圖示的類庫,實際上就是Echarts和Python的對接。 Pyecharts安裝 anaconda中沒
matplotlib視覺化初體驗
這篇部落格主要是總結一下最近進行的matplotlib視覺化實驗,內容主要來自於官方文件的例項。 (1)首先最簡單的——圓形散點圖: import matplotlib.pyplot as plt import numpy as np #繪製一個圓形散點圖 t = np.arange(1,
18-12-11-視覺化庫Seaborn學習筆記(七:Heatmap)
1、資料準備 import matplotlib.pyplot as plt import numpy as np; np.random.seed(0) import seaborn as sns; sns.set() uniform_data = np.random.rand(3, 3)
18-12-11-視覺化庫Seaborn學習筆記(六:FacetGrid)
引數: data :DataFrame 整潔(“長形式”)資料框,其中每列是變數,每行是觀察。 row,col,hue:strings 定義資料子集的變數,將在網格中
18-12-9-視覺化庫Seaborn學習筆記(五:category-分類)
資料匯入 #!/usr/bin/python # -*- coding: UTF-8 -*- # %matplotlib inline import numpy as np import pandas as pd import matplotlib as mpl import matplotli
18-12-8-視覺化庫Seaborn學習筆記(四:REG-迴歸分析繪圖)
目錄 獲取是否付小費資料 regplot()和lmplot()都可以繪製迴歸關係,推薦regplot() sns.lmplot(x="x", y="y", data=XXX, order=2); #曲線 利用hue引數畫出男女給予小費的不同 sns.l
18-12-8-視覺化庫Seaborn學習筆記(三:Var)
案例 #!/usr/bin/python # -*- coding: UTF-8 -*- # %matplotlib inline import numpy as np import pandas as pd from scipy import stats, integrate im
18-12-7-視覺化庫Seaborn學習筆記(二:Color)
目錄 1,調色盤 分類色板 圓形畫板 hls_palette()函式來控制顏色的亮度和飽和 使用XKCD顏色來命名顏色 連續色板 cubehelix_palette()調色盤 light_palette()和dark_palette()呼叫定製連續調
18-12-7-視覺化庫Seaborn學習筆記(一:Style)
目錄 安裝:pip install seaborn 程式碼: 5種主題風格 主題風格使用: sns.boxplot(data ) 用despine()刪除圖表邊框: sns.violinplot(資料) 畫子圖 sns.set_context(“紙”)