📜  在Python中使用 seaborn 的条形图

📅  最后修改于: 2022-05-13 01:55:40.103000             🧑  作者: Mango

在Python中使用 seaborn 的条形图

Seaborn 是一个惊人的Python统计图形绘图可视化库。它提供了漂亮的默认样式和调色板,使统计图更具吸引力。它建立在 matplotlib 库之上,并且与 pandas 的数据结构紧密集成。

Seaborn.barplot()

seaborn.barplot()方法用于绘制条形图。条形图表示数值变量的集中趋势估计值与每个矩形的高度,并使用误差条提供该估计值周围的不确定性的一些指示。

在 Seaborn 条形图中对具有不同属性的变量进行分组

示例 1:绘制一组按分类变量分组的垂直条形图。
使用 seaborn 创建一个简单的条形图。

句法:

Python3
# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
# class v / s fare barplot
sns.barplot(x = 'class', y = 'fare', data = df)
 
# Show the plot
plt.show()


Python3
# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
# class v / s fare barplot
sns.barplot(x = 'class', y = 'fare', hue = 'sex', data = df)
 
# Show the plot
plt.show()


Python3
# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
# fare v / s class horizontal barplot
sns.barplot(x = 'fare', y = 'class', hue = 'sex', data = df)
 
# Show the plot
plt.show()


Python3
# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
# class v / s fare barplot in given order
sns.barplot(x = 'class', y = 'fare', data = df,
            order = ["Third", "Second", "First"])
 
# Show the plot
plt.show()


Python3
# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file from seaborn library
df = sns.load_dataset('titanic')
 
# class v / s fare barplot with same colour
sns.barplot(x = 'class', y = 'fare', data = df, color = "salmon")
 
# Show the plot
plt.show()


Python3
# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
# class v / s fare barplot
# without error bars
sns.barplot(x = 'class', y = 'fare', data = df, ci = None)
 
# Show the plot
plt.show()


Python3
# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
sns.barplot(x = 'class', y = 'fare',
            hue = 'sex', data = df, palette='pastel')
 
# Show the plot
plt.show()


Python3
# importing the required library
import seaborn as sns
from numpy import median
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
sns.barplot(x = 'class', y = 'fare', hue = 'sex', data = df, estimator=median)
 
# Show the plot
plt.show()


Python3
from numpy import mean
sns.barplot(x = 'class', y = 'fare', hue = 'sex', data = df, estimator=mean)


Python3
# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
sns.barplot(x = 'class', y = 'fare', hue = 'sex', data = df,saturation = 0.1)
 
# Show the plot
plt.show()


Python3
# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
sns.barplot(x="class", y="fare", data=df,
                 linewidth=2.5, facecolor=(1, 1, 1, 0),
                 errcolor=".2", edgecolor=".2")


输出 :

条形图 - 1

示例 2:绘制一组由两个变量嵌套分组的垂直条。

使用带有两个变量的色调参数创建条形图。

句法:

Python3

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
# class v / s fare barplot
sns.barplot(x = 'class', y = 'fare', hue = 'sex', data = df)
 
# Show the plot
plt.show()

输出 :

条形图-2

示例 3:显示水平条形图。

交换数据变量而不是两个数据变量,那么这意味着轴将这些数据变量中的每一个表示为轴。

X表示x轴,y表示y轴。

Python3

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
# fare v / s class horizontal barplot
sns.barplot(x = 'fare', y = 'class', hue = 'sex', data = df)
 
# Show the plot
plt.show()

输出 :

条形图-3

示例 4:按给定顺序绘制所有条形图。

通过传递显式顺序来控制条形图顺序。

Python3

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
# class v / s fare barplot in given order
sns.barplot(x = 'class', y = 'fare', data = df,
            order = ["Third", "Second", "First"])
 
# Show the plot
plt.show()

输出 :

条形图-4

示例 5:使用颜色属性以单一颜色绘制所有条形图。

所有元素的颜色。

句法:

Python3

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file from seaborn library
df = sns.load_dataset('titanic')
 
# class v / s fare barplot with same colour
sns.barplot(x = 'class', y = 'fare', data = df, color = "salmon")
 
# Show the plot
plt.show()

输出 :

条形图 5

示例 6:使用 ci 属性的不带误差线的条形图。

我们将使用None 这意味着不会执行引导,并且不会绘制误差线

句法:

Python3

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
# class v / s fare barplot
# without error bars
sns.barplot(x = 'class', y = 'fare', data = df, ci = None)
 
# Show the plot
plt.show()

输出 :

条形图-6

示例 7:用于使用调色板的色调变量的不同级别的颜色。

使用调色板,我们可以生成具有不同颜色的点。在下面的示例中,我们可以看到调色板可以负责生成具有不同颜色图值的条形图。

Python3

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
sns.barplot(x = 'class', y = 'fare',
            hue = 'sex', data = df, palette='pastel')
 
# Show the plot
plt.show()

输出:

示例 8:使用统计函数NumPy .median 和NumPy .mean在每个分类 bin 内进行估计。

Python3

# importing the required library
import seaborn as sns
from numpy import median
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
sns.barplot(x = 'class', y = 'fare', hue = 'sex', data = df, estimator=median)
 
# Show the plot
plt.show()

输出:

对于 Numpy.mean:

Python3

from numpy import mean
sns.barplot(x = 'class', y = 'fare', hue = 'sex', data = df, estimator=mean)

输出:

示例 9:使用饱和度参数。

 绘制颜色的原始饱和度的比例。大色块通常看起来更好,颜色稍微不饱和,但如果您希望绘图颜色与输入颜色规范完美匹配,请将其设置为 1。

句法:

Python3

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
sns.barplot(x = 'class', y = 'fare', hue = 'sex', data = df,saturation = 0.1)
 
# Show the plot
plt.show()

输出:

示例 10:使用matplotlib.axes.Axes.bar()参数来控制样式。

我们可以使用线宽设置构成绘图元素的灰线的宽度。每当我们增加线宽时,点也会自动增加。

句法:

Python3

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
sns.barplot(x="class", y="fare", data=df,
                 linewidth=2.5, facecolor=(1, 1, 1, 0),
                 errcolor=".2", edgecolor=".2")

输出: