Python中的 Seaborn.barplot() 方法
Seaborn是一个基于 Matplotlib 的Python数据可视化库。它提供了一个用于绘制有吸引力和信息丰富的统计图形的高级界面。精心设计的可视化有一些非凡之处。颜色突出,层次很好地融合在一起,轮廓贯穿始终,整体包装不仅具有良好的美学品质,而且还为我们提供了有意义的见解。
seaborn.barplot() 方法
条形图基本上用于根据某些方法聚合分类数据,默认情况下它是平均值。也可以理解为按动作分组的可视化。为了使用这个图,我们为 x 轴选择一个分类列,为 y 轴选择一个数值列,我们看到它创建了一个对每个分类列取平均值的图。
Syntax : seaborn.barplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, estimator=
参数 :Arguments Value Description x, y, hue names of variables in “data“ or vector data, optional Inputs for plotting long-form data. See examples for interpretation. data DataFrame, array, or list of arrays, optional Dataset for plotting. If “x“ and “y“ are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form. order, hue_order lists of strings, optional Order to plot the categorical levels in, otherwise the levels are inferred from the data objects. estimator callable that maps vector -> scalar, optional Statistical function to estimate within each categorical bin. ci float or “sd” or None, optional Size 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_boot int, optional Number of bootstrap iterations to use when computing confidence intervals. units name of variable in “data“ or vector data, optional Identifier of sampling units, which will be used to perform a multilevel bootstrap and account for repeated measures design. orient “v” | “h”, optional Orientation 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. color matplotlib color, optional Color for all of the elements, or seed for a gradient palette. palette palette name, list, or dict, optional Colors 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. saturation float, optional Proportion 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. errcolor matplotlib color Color for the lines that represent the confidence interval. errwidth float, optional Thickness of error bar lines (and caps). capsize float, optional Width of the “caps” on error bars. dodge bool, optional When hue nesting is used, whether elements should be shifted along the categorical axis. ax matplotlib Axes, optional Axes object to draw the plot onto, otherwise uses the current Axes. kwargs ey, value mappings Other 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()
输出:
示例 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()
输出:
示例 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()
输出:
示例 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()
输出: