Python数学建模与分析——Python高级绘图

        个人学习笔记,课程为Python数学建模与分析:基础入门、数据处理、算法编程、高级绘图、建模实战!

目录

一、折线图

二、密度图

三、小提琴图

四、拟合回归线

五、散点图

六、散点图矩阵

七、直方图

八、箱型图

九、联合分布图


一、折线图

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib as mpl
import warnings
warnings.filterwarnings("ignore")


col1 = np. linspace(0, 10,1000)
col2 = np.sin(col1)
df = pd.DataFrame({"C1": col1 ,"C2":col2})
pd.set_option('dispLay.max_columns',None)
print(df.head(10))
# Plotting linepLot using sns.lineplot()
# plt.style.use('seaborn-darkgrid')-----错的
# 使用 Seaborn 设置主题
sns.set_theme(style="darkgrid")#替换
#matplotlib inline
sns.lineplot(x=df.C1,y=df.C2,data=df)
plt.show()
############################################################################

# 读取数据,不设置特定列为索引
iris = pd.read_csv('E:\pycharm\pyc\iris.data', header=None)
pd.set_option('display.max_columns', None)

# 设置列名
iris.columns = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', 'Species']

# 打印前10行数据
print(iris.head(10))

# 绘制线图
plt.figure(figsize=(20, 8))
sns.lineplot(data=iris)
plt.show()

# #################################################################
# 选择几个数值列来绘图
plt.figure(figsize=(20,8))
plt.plot(iris['SepalLength'], label='Sepal Length')
plt.plot(iris['SepalWidth'], label='Sepal Width')
plt.plot(iris['PetalLength'], label='Petal Length')
plt.plot(iris['PetalWidth'], label='Petal Width')
plt.legend()
plt.xlabel('Index')  # 由于我们没有设置特定的x轴数据,这里使用默认的索引
plt.ylabel('Measurement')  # y轴表示测量值
plt.title('Iris Dataset Features')  # 图表标题
plt.show()
############################################################################


plt.figure(figsize=(20, 8))
plt.plot(iris['SepalLength'], label='Sepal Length')
plt.plot(iris['PetalLength'], label='Petal Length')
plt.legend()
plt.xlabel('Index')  # x轴代表数据的索引
plt.ylabel('Length (cm)')  # y轴代表长度,单位为厘米
plt.title('Iris Dataset - Sepal and Petal Lengths')  # 图表标题
plt.show()


# #############################################################无法运行,缺少文件
# employment =pd.read_excel('unemployment.xlsx')
# print(employment.head(10))
# plt.figure(figsize=(14,7))
# plt.style.use('seaborn-darkgrid')
# sns.lineplot(x='Period', y='Unemployed', hue='Gender', data=employment)
# plt.show()
# plt.figure(figsize=(14,7))
# plt.style.use('seaborn-darkgrid')
# sns.lineplot(x='Period', y='Unemployed', hue='Gender', style='Gender', markers=True, dashes=False, data=employment)
# plt.show()
#
# plt.figure(figsize=(14,7))
# plt.style.use('seaborn-darkgrid')
# sns.lineplot(x='Period', y='Unemployed', hue='Gender', style='Gender', err_style='bars', ci=70, data=employment)
# plt.show()
#
# plt.figure(figsize=(14,7))
# plt.style.use('seaborn-darkgrid')
# sns.set(rc={'xtick.labelsize':17,'vtick.labelsize':10,'axes.labelsize':15, 'axes .grid':False})
# sns.lineplot(x='Period', y='Unemployed', hue='Gender', style='Gender',data=employment, dashes=False, palette='CMRmap',markers=['o','>'])
# plt.show()
#
#
# plt.figure(figsize=(14,7))
# plt.style.use('seaborn-darkgrid')
# plt.gcf().text(.2,.84,'GENDER', fontsize=40, color='Black')
# sns.lineplot(x='Period', y='Unemployed', hue='Gender', style='Gender',data=employment, dashes=False, palette='CMRmap',markers=['o','>'])
# plt.show()

二、密度图

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib as mpl
import warnings
from matplotlib import pyplot as plt

warnings.filterwarnings("ignore")
sns.set_style("white")

names = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'class']
iris = pd.read_csv('E:\pycharm\pyc\iris.data', names=names)
# shade=True 表示填充区域下方,cmap='Reds' 设置颜色映射为蓝色系,shade_lowest=True 表示填充最低密度区域
sns.kdeplot(data=iris, x='SepalWidthCm', y='SepalLengthCm', shade=True, cmap='Blues', shade_lowest=True)
plt.show()
# sns.swarmplot 用于绘制分类数据的分布,其中每个类别的值以点云的形式表示。
# figsize 参数控制图形的大小(宽度和高度)。
plt.figure(figsize=(7, 7))  # 创建新的图形窗口
sns.swarmplot(x='class', y='PetalWidthCm', data=iris)
plt.show()

三、小提琴图

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib as mpl
import warnings
from matplotlib import pyplot as plt

warnings.filterwarnings("ignore")
sns.set_style("white")

names = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'class']
iris = pd.read_csv('E:\pycharm\pyc\iris.data', names=names)
# 创建一个新的图形窗口,并设置其大小为7x7英寸
plt.figure(figsize=(7, 7))
# palette='Set2': 这设置了小提琴图的颜色方案,使用了名为'Set2'的调色板。
# dodge=False: 这个参数决定是否将小提琴图的边缘分开。设置为False表示不分开。
sns.violinplot(x="class", y="PetalWidthCm", data=iris, palette='Set2', dodge=False)
plt.show()

四、拟合回归线

import pandas as pd
import seaborn as sns
import matplotlib as mpl
import warnings
from matplotlib import pyplot as plt

warnings.filterwarnings("ignore")
sns.set_style("white")

names = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'class']
iris = pd.read_csv('E:\pycharm\pyc\iris.data', names=names)
iris1 = iris[iris['class'].isin(['Iris-setosa', 'Iris-versicolour', 'Iris-virginica'])]
# marker='*': 这个参数指定了散点的标记符号)
sns.regplot(x=iris1.SepalLengthCm, y=iris1.SepalWidthCm, color='#FF6600', marker='*')
plt.show()
#logx=True: 这个参数指定了在 x 轴上使用对数刻度。
#回归线样式:'alpha' 设置了线条的透明度为 0.8,'lw' 设置了线条的宽度为 3。
#sns.lmplot(x='bmi',y='charges', hue='smoker', data=insurance, height=8, aspect=1.2)
#lmplot 是一个强大的工具,可用于快速分析和可视化两个变量之间的关系
sns.regplot(x=iris1.SepalLengthCm, y=iris1.SepalWidthCm, logx=True, line_kws={'color':'#FF5722', 'alpha':0.8, 'lw':3})
plt.show()

五、散点图

# import matplotlib.pyplot as plt
# import numpy as np
# import pandas as pd
# import seaborn as sns
# import matplotlib as mpl
# import warnings
# warnings.filterwarnings("ignore")
# sns.set_style('white')
#
# ######################################################缺少文件,无法运行
# employment = pd.read_excel(r'D:\桌面\shumo\newdata\data\unemployment-rate-1948-2010.xls')
# sns.relplot(x='Period', y='value', hue='Gender', data=employment, height=7, aspect=2)
# plt.show()
#
#
# insurance =pd.read_csv('insurance.csv')
# print(insurance.head(10))
# sns.relplot(x='bmi',y='charges', hue='smoker',data=insurance,height=8,aspect=1)
# plt.show()
# sns.relplot(x='Period', y='Unemployed', hue='Gender', col='Age', kind='line',data=employment,height=6, aspect=1,col_wrap=4,linewidth=2)
# plt.show()
#
# sns.relplot(x='bmi',y='charges',hue='sex',col='sex',row='region',data=insurance,height=8,aspect=1)
# plt.show()
#
# sns.relplot(x='bmi', y='charges', hue='sex', col='sex', row='region',data=insurance,height=7,aspect=.6)
# plt.show()


import numpy as np
import pandas as pd
import seaborn as sns
import warnings
import matplotlib.pyplot as plt

warnings.filterwarnings("ignore")
sns.set_style("white")

# 读取CSV文件
employment = pd.read_csv('E:\pycharm\pyc\housing.csv')

# 绘制关系图
# 假设我们想要探索房价(MEDV)与房屋年龄(AGE)之间的关系,并根据房间数(RM)来区分颜色
sns.relplot(x='AGE', y='MEDV', hue='RM', data=employment, height=7, aspect=2)
plt.show()


"""  
# 使用seaborn的relplot函数绘制关系图  
# 注意:以下参数和注释已经根据实际的代码和数据集进行了调整  
sns.relplot(  
    x='AGE',          # x轴上的数据列名,这里是房屋年龄  
    y='MEDV',         # y轴上的数据列名,这里是中位数房价  
    hue='RM',         # 根据这个列的值来区分不同的颜色,这里是房间数  
    # col='Age',      # 这行被注释掉了,因为原始数据中没有'Age'列,且'col'参数不能重复指定  
    # kind='line',    # 这行也被注释掉了,因为relplot默认是散点图,且对于AGE和MEDV的关系,散点图更合适  
    # col='sex',      # 这行被注释掉了,因为原始数据中没有'sex'列  
    # row='region',   # 这行被注释掉了,因为原始数据中没有'region'列  
    data=employment,  # DataFrame的名称,这里包含波士顿房价数据的DataFrame名为employment  
    height=7,         # 每个子图的高度(英寸),这里保持为7  
    aspect=2,         # 子图的宽高比,这里设置为2  
    # col_wrap=4,     # 这行被注释掉了,因为没有使用col参数来创建子图  
    # linewidth=2,    # 这行也被注释掉了,因为默认绘制的是散点图,不是折线图  
)  # 注意这里添加了闭合的括号  
"""

六、散点图矩阵

import pandas as pd
import seaborn as sns
import matplotlib as mpl
import warnings
from matplotlib import pyplot as plt

warnings.filterwarnings("ignore")
sns.set_style("white")

names = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'class']
iris = pd.read_csv('E:\pycharm\pyc\iris.data', names=names)
print(iris.head())
# 从原始的 iris 数据集中筛选出包含 'Iris-setosa'、'Iris-versicolour' 和 'Iris-virginica' 类别的数据
iris1 = iris[iris['class'].isin(['Iris-setosa', 'Iris-versicolour', 'Iris-virginica'])]
sns.pairplot(iris1, hue='class', palette='husl', size=2)  # size=2 设置了每个子图的大小
# vars=['SepalLengthCm','SepalWidthCm','PetalLengthCm']: 这个参数指定了要在散点图矩阵中展示的特征列
# height=3: 这个参数设置了每个子图的高度为 3。
# aspect=1: 这个参数设置了每个子图的宽高比为 1,即正方形。
sns.pairplot(iris1,hue='class',vars=['SepalLengthCm','SepalWidthCm','PetalLengthCm'],height=3, aspect=1)
plt.show()

七、直方图

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib as mpl
import warnings
warnings.filterwarnings("ignore")
# 基本知识
# 生成一个正态分布的随机数组,均值为1,标准差为10,样本数量为1000
mpl.rcParams.update(mpl.rcParamsDefault)
num = np.random.normal(1,10,1000)
sns.distplot(num)
plt.show()

sns.displot(num, kde=False)  # 直方图,不带KDE  没有核密度估计
plt.show()

sns.displot(num, kde=True)  # 直方图,不带KDE  有核密度估计
plt.show()

housing = pd.read_csv('E:\pycharm\pyc\housing.csv')
print(housing.head())
plt.show()
# bins=40:这个参数表示将数据分成40个区间
# figsize=(20,20):这个参数设置图形的大小 单位是英寸
housing.hist(bins=40, figsize=(20, 20))
plt.show()

#多个图 三行三列
fig1 ,axes = plt.subplots(nrows=3,ncols=3 ,figsize = (20,20))
#ax=axes[0, 0]: 这指定了要在哪个子图上绘制直方图。axes是一个二维数组,代表多个子图,这里我们选择了第一个子图(索引为[0, 0])
#kde=False: 这个参数决定是否在直方图上绘制核密度估计曲线。设置为False表示不绘制。
sns.distplot(housing["CRIM"], color="#00bcd4",ax=axes[0, 0] , kde=False , bins=20)
sns.distplot(housing["ZN"], color="#937d14",ax=axes[0, 1], kde=False ,bins=20)
sns.distplot(housing["INDUS"], color="#006600", ax=axes[0, 2],kde=False, bins=20)
sns.distplot(housing["CHAS"] , color="#ff1e56", ax=axes[1,0], kde=False ,bins=20)
sns.distplot(housing["NOX"],color="#216353",ax=axes[1, 1], kde=False,bins=20)
sns.distplot(housing["RM"], color="#FF8F00", ax=axes[1,2],kde=False ,bins=20)
sns.distplot(housing["AGE"], color="#33FF00", ax=axes[2, 0], kde=False,bins=20)
sns.distplot(housing["DIS"], color="#FF3300", ax=axes[2, 1], kde=False,bins=20)
sns.distplot(housing["RAD"], color="#cccc00", ax=axes[2, 2],kde=False,bins=20)
plt.show()

八、箱型图

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib as mpl
import warnings
from matplotlib import pyplot as plt
warnings.filterwarnings("ignore")
sns.set_style("white")
housing = pd.read_csv('E:\pycharm\pyc\housing.csv')
"""
sns.boxplot(x=insurance.smoker, y=insurance.charges, orient=['no', 'yes']): 这行代码绘制了一个分组的箱线图,
展示了不同吸烟者类别('no'和'yes')下的健康保险费用分布情况。其中,x参数指定了分组依据的列(即吸烟者类别),
y参数指定了要展示的数据列(即健康保险费用),orient参数指定了分组的方向(这里设置为['no', 'yes'])。
"""
sns.boxplot(housing.B) #展示housing.csv中B列的箱线图
plt.show()

九、联合分布图

import pandas as pd
import seaborn as sns
import matplotlib as mpl
import warnings
from matplotlib import pyplot as plt

warnings.filterwarnings("ignore")
sns.set_style("white")

names = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'class']
iris = pd.read_csv('E:\pycharm\pyc\iris.data', names=names)
# kind='reg'  参数表示在图中添加一个回归线
# edgecolor='w'参数表示散点的边框颜色为白色。
# s=90参数表示散点的大小为90
# kind='hex'
sns.jointplot(x='SepalLengthCm', y='SepalWidthCm', data=iris, height=10, kind='reg', color='#FF6600')
plt.show()

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值