iis發(fā)布網(wǎng)站慢常州seo排名收費
1. 餅圖:顯示基本比例關系
import matplotlib.pyplot as pltplt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False# ——————————————————————————————————————————————————————————
#1.餅圖:顯示基本比例關系# 定義餅圖的標簽,對應不同的用戶興趣類別
labels = '財經(jīng)15%', '社會30%', '體育15%', '科技10%', '其它30%'# 定義餅圖的大小,對應每個類別的用戶興趣比例
datas = [15, 30, 15, 10, 30]# 創(chuàng)建一個figure對象和axes對象,用于繪制餅圖
fig1, ax1 = plt.subplots()
pie = ax1.pie(datas, labels=labels, autopct='%1.1f%%')# 設置整個圖表的標題
plt.title('新聞網(wǎng)站用戶興趣分析')# 顯示圖表
plt.show()
2. 堆疊柱形圖
它將兩個或多個變量的值在同一個軸上以堆疊的形式展示出來,使得觀察者可以清晰地看到每個變量的總和以及它們各自的部分。
#練習2:某城市一周內(nèi)每日天氣狀況
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 一周內(nèi)每天的日期
days = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']# 一周內(nèi)每天的最高氣溫、最低氣溫和降雨量
high_temps = [28, 30, 32, 31, 29, 27, 33]
low_temps = [18, 20, 22, 19, 17, 16, 21]
rainfall = [2, 0, 5, 3, 0, 0, 7]# 使用arange函數(shù)生成一個數(shù)組,表示條形圖的x坐標位置
x = np.arange(7)
# 設置柱狀圖的寬度
bar_width = 0.25# 誤差條
high= (2, 3, 4, 1, 2,3,2)
low = (3, 5, 2, 3, 3,1,2)
rain= (0.3, 0.5, 2, 1, 3,1,0.2)
# 繪制柱狀圖
plt.bar(x, high_temps,color='red', width=bar_width,yerr=high, label='High Temp')
plt.bar(x, low_temps, bottom=high_temps,color='blue', width=bar_width, yerr=low, label='Low Temp')
plt.bar(x, rainfall, bottom=(np.array(low_temps)+np.array(high_temps)),color='green', width=bar_width, yerr=rain, label='Rainfall')# 添加圖例
plt.legend()# 添加標題和標簽
plt.xlabel('星期')
plt.ylabel('氣溫 (℃) / 降雨量 (mm)')
plt.title('一周天氣變化')# 設置x軸的刻度標簽
plt.xticks(x,('周一', '周二', '周三', '周四', '周五', '周六', '周日'))
# # 添加圖例
# plt.legend((p1[0], p2[0], p3[0]), ('最高溫度', '最低溫度', '降雨量'))# 顯示圖形
plt.show()
3. 板塊層級圖
- 通常是一種用于展示不同板塊之間層級關系或分類的圖表,可以是組織結構圖、分類圖或其他類型的層級表示方法。
- 安裝squarify:
pip install squarify
# 練習3:公司部門年度收入分布
import matplotlib.pyplot as plt
import squarify
# 定義部門和對應的收入
departments = ['技術部', '市場部', '人力資源部', '財務部']
revenues = [10, 15, 50, 30] # 轉(zhuǎn)換為相同的數(shù)量級以方便計算
# 定義每個部門的顏色
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728']
# 繪制板塊層級圖
squarify.plot(sizes=revenues, label=departments, color=colors, alpha=0.7)
plt.axis('off')
plt.title('公司部門年度收入分布')
plt.show()
4. 堆疊面積圖
安裝seaborn: pip install seaborn
- 用于顯示每個數(shù)值所占大小隨時間或類別變化的趨勢線,展示的是部分與整體的關系。
- 堆疊面積圖上的最大的面積代表了所有的數(shù)據(jù)量的總和,是一個整體。
- 各個疊起來的面積表示各個數(shù)據(jù)量的大小,這些堆疊起來的面積圖在表現(xiàn)大數(shù)據(jù)的總量分量的變化情況時格外有用,所以堆疊面積圖不適用于表示帶有負值的數(shù)據(jù)集。非常適用于對比多變量隨時間變化的情況。
import numpy as np
# 導入seaborn庫,用于高級的統(tǒng)計圖表繪制
import seaborn as snsx = range(21, 26)# 定義x軸的數(shù)據(jù),這里表示年齡范圍
# 定義y軸的數(shù)據(jù),這里是一個二維數(shù)組,表示不同組在不同年齡的分值
y = [[10, 4, 6, 5, 3],[12, 2, 7, 10, 1],[8, 18, 5, 7, 6],[1, 8, 3, 5, 9]]
labels = ['組A', '組B', '組C', '組D']# 定義每個組的標簽
pal = sns.color_palette("Set1")# 使用seaborn的color_palette函數(shù)生成一組顏色
# 使用plt.stackplot函數(shù)繪制堆疊面積圖
plt.stackplot(x, y, labels=labels, colors=pal, alpha=0.7)# alpha參數(shù)指定透明度,colors=pal, alpha=0.7:可選項
plt.ylabel('分值')
plt.xlabel('年齡')
plt.title('不同組用戶區(qū)間分值比較')
plt.legend(loc='upper right')
plt.show()
5. 散點圖和氣泡圖
散點圖(Scatter Plot)是一種用于顯示兩個變量之間關系的圖表,通過在坐標平面上描繪點來展示數(shù)據(jù)的分布和趨勢。
import numpy as np
import matplotlib.pyplot as plt
import randomplt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標簽
plt.rcParams['axes.unicode_minus']=False #用來正常顯示負號
# 設置隨機種子,確保結果的可重現(xiàn)性
np.random.seed(42)# ------------------------------------------------------------------
#1.生成散點圖
data = {'城市': ['A', 'B', 'C', 'D', 'E'],'溫度': [22, 24, 26, 23, 25],'相對濕度': [60, 65, 70, 55, 68],}x = data['溫度']
y = data['相對濕度']
print(x)
print(y)
# #氣泡圖需要修改顏色和大小
# #------------------------------
# color = np.random.rand(5)
color1 = ["red",'blue','yellow','black','green']
# size = np.random.rand(5)*1000#【0,1)-->[0,1000)
size1 = [100,20,30,600,400]
# #---------------------------------# 使用scatter函數(shù)繪制散點圖,s=100表示點的大小,c='black'表示點的顏色為黑色,alpha=0.8表示點的透明度
# plt.scatter(x, y)
plt.scatter(x, y,s=size1,c=color1, alpha=0.6)plt.title("隨機生成的數(shù)字散點")
plt.ylabel('Y坐標值')
plt.xlabel('X坐標值')
# 顯示圖表
plt.show()
散點圖:
氣泡圖:就是改變散點圖的大小和顏色(隨機生成)
6.直方圖
一種用于展示數(shù)據(jù)分布特征的統(tǒng)計圖表,它通過將數(shù)據(jù)分組并計算每組中的頻數(shù)或頻率來表示數(shù)據(jù)的分布情況。
##3.直方圖:
"""用于展示數(shù)據(jù)分布特征的統(tǒng)計圖表,
它通過將數(shù)據(jù)分組并計算每組中的頻數(shù)或頻率來表示數(shù)據(jù)的分布情況。"""
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標簽
plt.rcParams['axes.unicode_minus']=False #用來正常顯示負號
np.random.seed(19680801)mu = 100 # 均值
sigma = 15 # 標準差
x = mu + sigma * np.random.randn(500)# 根據(jù)正態(tài)分布生成隨機樣本數(shù)據(jù)num_bins = 10# 設置直方圖的箱數(shù)fig, ax = plt.subplots()# fig是整個圖形的容器,ax是圖形的軸
ax.hist(x, num_bins, density=True, alpha=0.6,edgecolor='black')#density=True表示y軸顯示概率密度而非計數(shù)
ax.set_xlabel('智商IQ')
ax.set_ylabel('概率密度')
ax.set_title(r'智商分布情況直方圖')# plt.hist(x,num_bins,density=False)
# plt.xlabel('學生人數(shù)')
# plt.ylabel('考試成績')
# plt.title('學生考試成績分布')plt.show()
7. 熱力圖(heatmap)
是一種數(shù)據(jù)可視化技術,它通過顏色的變化來展示數(shù)據(jù)矩陣中數(shù)值的大小。
##4.heatmap熱力圖:通過顏色的變化來展示數(shù)據(jù)矩陣中數(shù)值的大小。
import seaborn as sns
import pandas as pd
import numpy as np
import seaborn as sns
from matplotlib import pyplot as pltplt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標簽
plt.rcParams['axes.unicode_minus']=Falsestudents = ['A', 'B', 'C', 'D', 'E']
subjects = ['數(shù)學', '物理', '化學', '生物', '英語']# 生成隨機成績數(shù)據(jù)
#np.random.seed(0) # 為了結果的可重復性
data = { '數(shù)學': [85, 75, 95, 60, 80],'物理': [90, 80, 97, 65, 82],'化學': [92, 85, 90, 70, 88],'生物': [88, 70, 85, 80, 90],'英語': [78, 90, 80, 75, 85]}print(data)# 創(chuàng)建DataFrame
df = pd.DataFrame(data, index=students,columns=subjects)
print(df)# 使用Seaborn繪制熱力圖
plt.figure(figsize=(10, 8))
sns.heatmap(df, xticklabels=subjects, yticklabels=students)
# 設置圖表標題
plt.title('各小組工作日表現(xiàn)比較熱力圖')
# 顯示圖表
plt.show()
8. 箱型圖
是一種非常有用的數(shù)據(jù)可視化工具,它不僅可以展示數(shù)據(jù)的中位數(shù)、四分位數(shù),還可以直觀地表示異常值,幫助用戶快速了解數(shù)據(jù)的集中趨勢、分散程度和異常情況。
#5.box:小組成員的得分情況
"""箱型圖:展示數(shù)據(jù)的中位數(shù)、四分位數(shù),還可以直觀地表示異常值,
幫助用戶快速了解數(shù)據(jù)的集中趨勢、分散程度和異常情況。
"""
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import randomplt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標簽
plt.rcParams['axes.unicode_minus']=False #用來正常顯示負號# 設置隨機種子以確保結果的可重現(xiàn)性
np.random.seed(42)# 生成組號列表
day_list = ['組1', '組2', '組3', '組4', '組5', '組6', '組7']
day = [random.choice(day_list) for _ in range(1000)]#從day_list隨機選取1000個數(shù)據(jù)存入day
print(day)
# 重新設置隨機種子,生成數(shù)據(jù)(800個正常數(shù)據(jù),200個離群數(shù)據(jù))
np.random.seed(666)
spread = np.random.rand(800) * 100 # 生成800個0~100之間的隨機數(shù),np.random.rand(800) :800個[0,1)的數(shù)據(jù)
flier_high = np.random.rand(100) * 100 + 100 # 生成100個100~200之間的隨機數(shù)
flier_low = np.random.rand(100) *100 -100 # 生成100個-100~0之間的隨機數(shù)
# #
# # # 將數(shù)據(jù)合并為一個數(shù)組
data = np.concatenate((spread, flier_high, flier_low), 0)
# data1={'組號': day,
# '得分': data}
# # # 創(chuàng)建DataFrame
# df = pd.DataFrame(data1)
df = pd.DataFrame({'組號': day,'得分': data})
print(df)
# # 使用Seaborn繪制箱型圖
sns.boxplot(x='組號', y='得分', data=df)
#
# # 設置x軸的刻度標簽
# plt.xticks(range(len(day_list)), day_list)
plt.xticks(range(7), day_list)
#
# # 顯示圖表
plt.show()