Python_matplotlib 跳转链接
之前的博客中已经展示了使用python的matplotlib库进行一些基础图像的绘制,本篇进一步展示一些matplotlib中的一些进阶图像绘制。
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
Titanic = pd.read_csv("/Users/guxiang/应用统计/统计学基础/(02)习题数据/exercise/chap02/exercise2_1.csv", encoding='gbk')
print(Titanic.head())
# 条形图
t1 = pd.crosstab(Titanic.Sex, Titanic.Survived)
t1.plot(kind='bar')
plt.title('(a)垂直并列条形图')
plt.show()
t1.plot(kind='barh')
plt.title('(b)水平并列条形图')
plt.show()
t2 = pd.crosstab(Titanic.Sex, Titanic.Survived)
t2.plot(kind='bar', stacked=True)
plt.title('(c)垂直堆叠条形图')
plt.show()
t2 = pd.crosstab(Titanic.Sex, Titanic.Survived)
t2.plot(kind='barh', stacked=True)
plt.title('(d)水平堆叠条形图')
plt.show()
# 计算舱位类别分布
class_counts = Titanic['Class'].value_counts().sort_index() # 按照舱位等级进行排序
# 创建饼图
plt.figure(figsize=(6, 6))
plt.pie(class_counts, labels=class_counts.index, autopct='%1.1f%%', startangle=90)
plt.title('Titanic乘客舱位类别分布')
plt.axis('equal') # 使饼图成为一个正圆
plt.show()
# 创建环形图
plt.figure(figsize=(6, 6))
plt.pie(class_counts, labels=class_counts.index, autopct='%1.1f%%', startangle=90, wedgeprops=dict(width=0.4))
plt.title('Titanic乘客舱位类别分布')
plt.axis('equal') # 使环形图成为一个正圆
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['font.sans-serif'] = ['SimHei']
faithful = pd.read_csv("/Users/guxiang/应用统计/统计学基础/(02)习题数据/exercise/chap02/exercise2_2.csv", encoding='gbk')
print(faithful.head())
# 直方图
sns.histplot(faithful.eruptions)
plt.title('(a)默认绘制(y轴为频数)')
plt.show()
# 核密度曲线
sns.histplot(faithful.eruptions, kde=True)
plt.title('(b)核密度曲线(y轴为频率)')
plt.show()
# 核密度比较曲线
plt.figure(figsize=(8, 6))
sns.kdeplot(faithful['eruptions'], label='Eruptions', fill=True)
sns.kdeplot(faithful['waiting'], label='Waiting', fill=True)
plt.title('核密度比较曲线')
plt.xlabel('Value')
plt.ylabel('Density')
plt.legend()
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['font.sans-serif'] = ['SimHei']
mtcars = pd.read_csv("/Users/guxiang/应用统计/统计学基础/(02)习题数据/exercise/chap02/exercise2_3.csv", encoding='gbk')
print(mtcars.head())
# 绘制散点图
plt.figure(figsize=(8, 6))
plt.scatter(mtcars['wt'], mtcars['mpg'], c='blue', alpha=0.6)
plt.xlabel('Car Weight (wt)')
plt.ylabel('Miles per Gallon (mpg)')
plt.title('散点图')
plt.grid(True)
plt.show()
# 绘制散点图矩阵
sns.set(style="ticks")
sns.pairplot(mtcars)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['font.sans-serif'] = ['SimHei']
iris = pd.read_csv("/Users/guxiang/应用统计/统计学基础/(02)习题数据/exercise/chap02/exercise2_4.csv", encoding='gbk')
print(iris.head())
# 折线图
iris_50 = iris.head(50)
dfs = [iris_50['Sepal.Length'], iris_50['Sepal.Width'], iris_50['Petal.Length'], iris_50['Petal.Width']]
sns.lineplot(data=dfs, markers=True)
# 雷达图
import numpy as np
datalenth = 50
df1 = np.array(iris_50['Sepal.Length'])
df2 = np.array(iris_50['Sepal.Width'])
df3 = np.array(iris_50['Petal.Length'])
df4 = np.array(iris_50['Petal.Width'])
angles = np.linspace(0, 2*np.pi, datalenth, endpoint=False)
df1 = np.concatenate((df1, [df1[0]]))
df2 = np.concatenate((df2, [df2[0]]))
df3 = np.concatenate((df3, [df3[0]]))
df4 = np.concatenate((df4, [df4[0]]))
angles = np.concatenate((angles, [angles[0]]))
plt.figure(figsize=(6, 6), facecolor='lightyellow')
plt.polar(angles, df1, 'r--', linewidth=1, label='Sepal.Length')
plt.polar(angles, df2, 'b', linewidth=1, label='Sepal.Width')
plt.polar(angles, df3, 'k', linewidth=1, label='Petal.Length')
plt.polar(angles, df4, 'g', linewidth=1, label='Petal.Width')
plt.show()
# 一些图像操作
import numpy as np
fit = np.poly1d(np.polyfit(x, y, deg=1)) # 使用一项式拟合
y_hat = fit(x)
plt.plot(x.mean(), y.mean(), 'ro', markersize=20, fillstyle='bottom') # 添加均值点并设置点的大小,颜色和填充类型
plt.axhline(y.mean(), color='black', ls='--', lw=1)
plt.axvline(x.mean(), color='black', ls=(0,(5,10)), lw=1)
plt.grid(linestyle=":") # 添加网格线
ax = plt.gca() #得到当前操作图像
# 更改边框颜色
ax.spines['right'].set_color('#4169E1')
ax.spines['left'].set_color('#4169E1')
ax.spines['top'].set_color('royalblue')
ax.spines['bottom'].set_color('b')
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ["SimHei"]
df = pd.read_csv("/Users/guxiang/应用统计/统计学基础/(01)例题数据/pydata/chap02/example2_2.csv", encoding='gbk')
print(df)
plt.subplots(1 ,2, figsize=(10, 6))
# 绘制普通饼图
plt.subplot(121)
pl = plt.pie(df['北京'], labels=df['支出项目'], autopct='%1.2f%%')
plt.title('(a)普通饼图')
plt.subplot(122)
pl = plt.pie(df['北京'], labels=df['支出项目'], shadow=True, autopct='%1.2f%%', explode=(0.2,0,0.1,0,0,0,0,0))
# 绘制环形图
plt.subplots(1 ,2, figsize=(10, 6))
plt.subplot(121)
p1 = plt.pie(df['北京'], labels=df['支出项目'], autopct='%1.2f%%',
pctdistance=0.75, wedgeprops={'width':0.5, 'edgecolor':'w'})
plt.title('(a)北京消费支出的环形图')
plt.subplot(122)
colors = ['red', 'yellow', 'slateblue', 'lawngreen', 'magenta', 'green', 'orange', 'cyan', 'pink']
p2 = plt.pie(df['北京'], labels=df['支出项目'], autopct='%1.2f%%', radius=1,
pctdistance=0.75, colors=colors, wedgeprops={'linewidth':1.8, 'width':0.5, 'edgecolor':'w'})
plt.subplot(122)
colors = ['red', 'yellow', 'slateblue', 'lawngreen', 'magenta', 'green', 'orange', 'cyan', 'pink']
p2 = plt.pie(df['上海'], autopct='%1.2f%%', radius=0.7,
pctdistance=0.7, colors=colors, wedgeprops={'linewidth':1.8, 'width':0.3, 'edgecolor':'w'})
plt.title('(b)北京和上海消费支出的环形图')
plt.show()
# 直方图
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['font.sans-serif'] = ["SimHei"]
df = pd.read_csv("/Users/guxiang/应用统计/统计学基础/(01)例题数据/pydata/chap02/example2_3.csv", encoding='gbk')
plt.subplots(1, 3, figsize=(16, 5))
plt.subplot(131)
sns.histplot(df['北京'])
plt.title('(a)默认绘制(y轴是频数)')
plt.subplot(132)
sns.histplot(df['上海'], kde=True, stat='frequency')
plt.title('(b)显示核密度曲线(y轴是频率)')
plt.subplot(133)
sns.histplot(df['郑州'], bins=20, kde=True, stat='density')
plt.title('(c)显示核密度曲线(y轴是密度)')
# 绘制核密度图
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['font.sans-serif'] = ["SimHei"]
plt.rcParams['axes.unicode_minus'] = False
df = pd.read_csv("/Users/guxiang/应用统计/统计学基础/(01)例题数据/pydata/chap02/example2_3.csv", encoding='gbk')
# sns.set_style('darkgrid')可以用来调整风格
plt.subplots(1, 3, figsize=(15, 4))
plt.subplot(131)
sns.kdeplot(df['北京'], bw_adjust=0.2)
plt.title('(a)bw_adjust=0.2')
plt.subplot(132)
sns.kdeplot(df['北京'], bw_adjust=0.5)
plt.title('(b)bw_adjust=0.5')
plt.subplot(133)
sns.kdeplot(df['北京'], bw_method=0.5)
plt.title('(b)bw_method=0.5')
标签:plt,进阶,title,Python,matplotlib,df,sns,pd,import
From: https://blog.csdn.net/weixin_58607181/article/details/137360353