📌  相关文章
📜  Python中的 Seaborn.barplot() 方法

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

Python中的 Seaborn.barplot() 方法

Seaborn是一个基于 Matplotlib 的Python数据可视化库。它提供了一个用于绘制有吸引力和信息丰富的统计图形的高级界面。精心设计的可视化有一些非凡之处。颜色突出,层次很好地融合在一起,轮廓贯穿始终,整体包装不仅具有良好的美学品质,而且还为我们提供了有意义的见解。

seaborn.barplot() 方法

条形图基本上用于根据某些方法聚合分类数据,默认情况下它是平均值。也可以理解为按动作分组的可视化。为了使用这个图,我们为 x 轴选择一个分类列,为 y 轴选择一个数值列,我们看到它创建了一个对每个分类列取平均值的图。

参数 :

Arguments                         Value                                                                              Description
x, y, huenames of variables in “data“ or vector data, optionalInputs for plotting long-form data. See examples for interpretation.
dataDataFrame, array, or list of arrays, optionalDataset for plotting. If “x“ and “y“ are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.
order, hue_orderlists of strings, optionalOrder to plot the categorical levels in, otherwise the levels are inferred from the data objects.
estimatorcallable that maps vector -> scalar, optionalStatistical function to estimate within each categorical bin.
cifloat or “sd” or None, optionalSize of confidence intervals to draw around estimated values.  If “sd”, skip bootstrapping and draw the standard deviation of the observations. If “None“, no bootstrapping will be performed, and error bars will not be drawn.
n_bootint, optionalNumber of bootstrap iterations to use when computing confidence intervals.
unitsname of variable in “data“ or vector data, optionalIdentifier of sampling units, which will be used to perform a multilevel bootstrap and account for repeated measures design. 
orient“v” | “h”, optionalOrientation of the plot (vertical or horizontal). This is usually inferred from the dtype of the input variables, but can be used to specify when the “categorical” variable is a numeric or when plotting wide-form data.
colormatplotlib color, optionalColor for all of the elements, or seed for a gradient palette.
palettepalette name, list, or dict, optionalColors to use for the different levels of the “hue“ variable. Should be something that can be interpreted by :func:`color_palette`, or a dictionary mapping hue levels to matplotlib colors.
saturationfloat, optionalProportion of the original saturation to draw colors at. Large patches often look better with slightly desaturated colors, but set this to “1“ if you want the plot colors to perfectly match the input color spec.
errcolormatplotlib colorColor for the lines that represent the confidence interval.
errwidthfloat, optionalThickness of error bar lines (and caps). 
capsizefloat, optionalWidth of the “caps” on error bars.
dodgebool, optionalWhen hue nesting is used, whether elements should be shifted along the categorical axis. 
axmatplotlib Axes, optionalAxes object to draw the plot onto, otherwise uses the current Axes.
kwargsey, value mappingsOther keyword arguments are passed through to “plt.bar“ at draw time.

使用以下步骤:

  • 进口 Seaborn
  • 从 Seaborn 加载数据集,因为它包含很好的数据集集合。
  • 使用 seaborn.barplot() 方法绘制条形图。

下面是实现:

示例 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')
 
# who v/s fare barplot
sns.barplot(x = 'who',
            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')
 
 
# who v/s fare barplot
sns.barplot(x = 'who',
            y = 'fare',
            hue = 'class',
            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')
 
 
# who v/s fare barplot
sns.barplot(x = 'who',
            y = 'fare',
            hue = 'class',
            data = df,
            palette = "Blues")
 
# Show the plot
plt.show()


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


输出:

简单的barpot

示例 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')
 
 
# who v/s fare barplot
sns.barplot(x = 'who',
            y = 'fare',
            hue = 'class',
            data = df)
 
# Show the plot
plt.show()

输出:

条形图 - 2

示例 3:

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')
 
 
# who v/s fare barplot
sns.barplot(x = 'who',
            y = 'fare',
            hue = 'class',
            data = df,
            palette = "Blues")
 
# Show the plot
plt.show()

输出:

条形图 - 3

示例 4:

Python3

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

输出: