代码模板:
最前面
importpandas as pd
pd.plotting.register_matplotlib_converters()importmatplotlib.pyplot as plt%matplotlib inlineimportseaborn as snsprint("Setup Complete")
View Code
一、折线图Line charts
plt.figure(figsize=(14,6))#Add title
plt.title("Daily Global Streams of Popular Songs in 2017-2018")#Line chart showing daily global streams of 'Shape of You'
sns.lineplot(data=spotify_data['Shape of You'], label="Shape of You")#Line chart showing daily global streams of 'Despacito'
sns.lineplot(data=spotify_data['Despacito'], label="Despacito")#Add label for horizontal axis
plt.xlabel("Date")#选择需要的变量输出
or
#Set the width and height of the figure
plt.figure(figsize=(14,6))#Add title
plt.title("Daily Global Streams of Popular Songs in 2017-2018")#Line chart showing daily global streams of each song
sns.lineplot(data=spotify_data)#所有变量输出
View Code
二、条状图和热力图Bar charts and heatmaps
#Set the width and height of the figure
plt.figure(figsize=(10,6))#Add title
plt.title("Average Arrival Delay for Spirit Airlines Flights, by Month")#Bar chart showing average arrival delay for Spirit Airlines flights by month
sns.barplot(x=flight_data.index, y=flight_data['NK'])#Add label for vertical axis
plt.ylabel("Arrival delay (in minutes)")
plt.xlabel("")
View Code
#Set the width and height of the figure
plt.figure(figsize=(14,7))#Add title
plt.title("Average Arrival Delay for Each Airline, by Month")#Heatmap showing average arrival delay for each airline by month
sns.heatmap(data=flight_data, annot=True)
当annot为True时,在heatmap中每个方格写入数据#Add label for horizontal axis
plt.xlabel("Airline")
plt.ylabel("")
View Code
三、散点图Scatter plots:
1. Create a scatter plot
sns.scatterplot(x=insurance_data['bmi'], y=insurance_data['charges'])
View Code
2.加入趋势线a regression line
sns.regplot(x=insurance_data['bmi'], y=insurance_data['charges'])
View Code
3、颜色编码的散点图Color-coded scatter plots
sns.scatterplot(x=insurance_data['bmi'], y=insurance_data['charges'], hue=insurance_data['smoker'])
View Code
4、 两条趋势线two regression lines
sns.lmplot(x="bmi", y="charges", hue="smoker", data=insurance_data)
View Code
5、分类散点图a categorical scatter plot
sns.swarmplot(x=candy_data['chocolate'],
y=candy_data['winpercent'])
View Code
四、直方图histograms
#Histogram
sns.distplot(a=iris_data['Petal Length (cm)'], kde=False)
View Code
五、密度图density plots
1、核心密度图kernel density estimate (KDE) plot
#KDE plot
sns.kdeplot(data=iris_data['Petal Length (cm)'], shade=True)
View Code
2、二维KDE图two-dimensional (2D) KDE plot
#2D KDE plot
sns.jointplot(x=iris_data['Petal Length (cm)'], y=iris_data['Sepal Width (cm)'], kind="kde")
View Code
六、彩色图Color-coded plots
#Histograms for each species
sns.distplot(a=iris_set_data['Petal Length (cm)'], label="Iris-setosa", kde=False)
sns.distplot(a=iris_ver_data['Petal Length (cm)'], label="Iris-versicolor", kde=False)
sns.distplot(a=iris_vir_data['Petal Length (cm)'], label="Iris-virginica", kde=False)#Add title
plt.title("Histogram of Petal Lengths, by Species")#Force legend to appear
plt.legend()
View Code
#KDE plots for each species
sns.kdeplot(data=iris_set_data['Petal Length (cm)'], label="Iris-setosa", shade=True)
sns.kdeplot(data=iris_ver_data['Petal Length (cm)'], label="Iris-versicolor", shade=True)
sns.kdeplot(data=iris_vir_data['Petal Length (cm)'], label="Iris-virginica", shade=True)#Add title
plt.title("Distribution of Petal Lengths, by Species")
View Code
总结:
以上图形可分为三类:
趋势Trend——即为变化的模式
- sns.lineplot - 折线图最适合显示一段时间内的趋势,多条折线可用于显示多个组中的趋势
关系Relationship——反映数据中变量之间的关系
-sns.barplot - 柱状图对于比较不同组对应的数量很有用
-sns.heatmap - 热图可以用来在数字表中找到彩色编码的模式
-sns.scatterplot - 散点图表示两个连续变量之间的关系;如果用颜色编码,我们还可以显示与第三个分类变量的关系
-sns.regplot - 在散点图中包含一条回归线可以更容易地看到两个变量之间的任何线性关系.
-sns.lmplot - 如果散点图包含多个颜色编码的组,则此命令对于绘制多个回归线非常有用
-sns.swarmplot - 分类散点图显示了连续变量和分类变量之间的关系.
分布Distribution——可视化分布来显示变量中可能出现的值,以及它们出现的可能性
-sns.distplot - 直方图表示单个数值变量的分布
-sns.kdeplot - KDE图(或2D KDE图)显示了单个数值变量(或两个数值变量)的估计的、平滑的分布。
-sns.jointplot - 此命令对于同时显示2D KDE绘图和每个单独变量的相应KDE绘图非常有用。
(引用于https://blog.csdn.net/freja110/article/details/104856712)