例:(面向对象方式绘制极坐标)
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(0)
# Compute pie slices
N = 20
theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
radii = 10 * np.random.rand(N)
width = np.pi / 4 * np.random.rand(N)
ax = plt.subplot(111, projection='polar')
bars = ax.bar(theta, radii, width=width, bottom=0.0)
# Use custom colors and opacity
for r, bar in zip(radii, bars):
bar.set_facecolor(plt.cm.viridis(r / 10.))
bar.set_alpha(0.5)
plt.show()
-
官方文档:
matplotlib.pyplot.bar
-
matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)
Make a bar plot.
The bars are positioned at x with the given alignment. Their dimensions are given by width and height. The vertical baseline is bottom (default 0).(
在给定的对齐方式下,条形图定位在x处。它们的尺寸是由宽度和高度确定的。垂直基线为底部(默认为0)。)Each of x, height, width, and bottom may either be a scalar applying to all bars, or it may be a sequence of length N providing a separate value for each bar.(
x、高度、宽度和底部中的每一个都可以是应用于所有条形的标量,也可以是长度N的序列,为每个条形提供一个单独的值。)
Parameters:
-
x : sequence of scalars
- The x coordinates of the bars. See also align for the alignment of the bars to the coordinates.(条形的x坐标。有关条形图与坐标的对齐方式,请参见对齐。)
-
height : scalar or sequence of scalars
- The height(s) of the bars.
-
width : scalar or array-like, optional
- The width(s) of the bars (default: 0.8).
-
bottom : scalar or array-like, optional
The y coordinate(s) of the bars bases (default: 0).
-
align : {'center', 'edge'}, optional, default: 'center'
Alignment of the bars to the x coordinates:
'center': Center the base on the x positions.
'edge': Align the left edges of the bars with the x positions.
To align the bars on the right edge pass a negative width and align='edge'.
Returns:
-
container : BarContainer
- Container with all the bars and optionally errorbars.(包含所有条形和可选错误栏的容器。)
Other Parameters:
-
color : scalar or array-like, optional
- The colors of the bar faces.
-
edgecolor : scalar or array-like, optional
- The colors of the bar edges.
-
linewidth : scalar or array-like, optional
- Width of the bar edge(s). If 0, don't draw edges.
-
tick_label : string or array-like, optional
- The tick labels of the bars. Default: None (Use default numeric labels.)
-
xerr, yerr : scalar or array-like of shape(N,) or shape(2,N), optional
-
If not None, add horizontal / vertical errorbars to the bar tips. The values are +/- sizes relative to the data:
scalar: symmetric +/- values for all bars
shape(N,): symmetric +/- values for each bar
shape(2,N): Separate - and + values for each bar. First row
contains the lower errors, the second row contains the upper errors.(第一行包含较低的错误,第二行包含较高的错误。)None: No errorbar. (Default)
See Different ways of specifying error bars for an example on the usage of xerr and yerr.(有关使用xerr和yerr的示例,请参阅指定错误栏的不同方法。)
-
-
ecolor : scalar or array-like, optional, default: 'black'
- The line color of the errorbars.
-
capsize : scalar, optional
- The length of the error bar caps in points. Default: None, which will take the value from rcParams["errorbar.capsize"].
-
error_kw : dict, optional
- Dictionary of kwargs to be passed to the errorbar method. Values of ecolor or capsize defined here take precedence over the independent kwargs.(要传递给errorbar方法的kwargs字典。此处定义的ecolor或capsize的值优先于独立的kwargs。)
-
log : bool, optional, default: False
- If True, set the y-axis to be log scale.
-
orientation : {'vertical', 'horizontal'}, optional
- This is for internal use only. Please use barh for horizontal bar plots. Default: 'vertical'.(这仅供内部使用。请使用barh作为水平条形图。默认值:'vertical'。)