Draw simple graphs using statistical functions
Article directory
1. Draw simple graphics
1.1.1 Function Introduction
1. bar
(Draw a column chart)
import matplotlib as mpl
import matplotlib.pyplot as plt
import random
import string
def test():
# 设置字体为黑体
mpl.rcParams['font.family'] = 'SimHei'
# 设置在中文字体是能够正常显示负号(“-”)
mpl.rcParams['axes.unicode_minus'] = False
x = [i for i in range(1, 9)]
y = [random.choice(x) for i in range(8)]
plt.bar(x,
y,
align="center",
color="c",
tick_label=[random.choice(string.ascii_lowercase) for i in range(8)],
hatch="/")
plt.xlabel('箱子编号')
plt.ylabel('箱子重量(kg)')
plt.show()
matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.bar.html?highlight=bar#matplotlib.pyplot.bar
Make a bar chart.
Position the bar at x using the given alignment . Their dimensions are given by height and width . The vertical baseline is bottom (default 0).
Many parameters can take a single value to apply to all bars or a range of values, one value per bar.
parameter:
x
: float or array-like
The x-coordinate of the bar. See also align for the alignment of bars to coordinates.
height
:float or array-like
The height of the bar.
width
:float or array-like, default: 0.8
The width of the bar.
bottom
:float or array-like, default: 0
The y-coordinate of the base of the bar.
align
:{'center', 'edge'}, default: 'center'
Alignment of bars to x- coordinates:
- 'center': Center on the x position.
- 'edge': Align the left edge of the bar with the x position.
To align bars to the right edge, pass a negative width and align='edge'
.
2. barh
(Draw a bar chart)
import matplotlib as mpl
import matplotlib.pyplot as plt
import random
import string
def test():
# 设置字体为黑体
mpl.rcParams['font.family'] = 'SimHei'
# 设置在中文字体是能够正常显示负号(“-”)
mpl.rcParams['axes.unicode_minus'] = False
x = [i for i in range(1, 9)]
y = [random.choice(x) for i in range(8)]
plt.barh(x,
y,
align="center",
color="c",
tick_label=[random.choice(string.ascii_lowercase) for i in range(8)],
hatch="/")
plt.xlabel('箱子重量(kg)')
plt.ylabel('箱子编号')
plt.show()
matplotlib.pyplot.barh(y, width, height=0.8, left=None, *, align='center', **kwargs)
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.barh.html
3. hist
(Draw a histogram)
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
def test():
# 设置字体为黑体
mpl.rcParams['font.family'] = 'SimHei'
# 设置在中文字体是能够正常显示负号(“-”)
mpl.rcParams['axes.unicode_minus'] = False
box_weight = np.random.randint(0, 10, 100)
x = box_weight
bins = range(11)
plt.hist(x,
bins=bins,
color="g",
histtype="bar",
rwidth=1,
alpha=0.6)
plt.xlabel('箱子重量(kg)')
plt.ylabel('销售数量')
plt.show()
matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, *, data=None, **kwargs)
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html?highlight=hist#matplotlib.pyplot.hist
Draw a histogram.
Compute and plot a histogram of x . The return value is a tuple ( n , bins , patches ) or ([ n0 , n1 , …], bins , [ patches0 , patches1 , … .]) if the input contains multiple items. See the documentation for the weights parameter to plot a histogram of binned data.
Multiple data can be provided by x as a list of datasets of possibly different lengths ([ x0 , x1 , …] ), or as a 2D ndarray where each column is a dataset. Note that the ndarray form is transposed relative to the list form.
Masked arrays are not supported.
The bins , range , weights and density parameters behave like [ numpy.histogram
](https://numpy.org/doc/stable/reference/generated/numpy.histogram.html#numpy.histogram).
parameter:
x
: (n,) array or sequence of (n,) arrays
Input values, which require a single array or not a sequence of arrays of the same length. ( This data is the data of the counted frequency )
bins
: int or sequence or str, default: rcParams["hist.bins"] (default:
10)
If bins is an integer, it defines the number of equal-width bins in the range.
If bins is a sequence, it defines the bin edges, including the left edge of the first bin and the right edge of the last bin; in this case, the bin
spacing of the bins may not be equal. bin
All but the last one (rightmost) bin
are half-open (intervals with left open and right closed). In other words, if bins is:
[1, 2, 3, 4]
Then the first bin is [1, 2)
(including 1, but excluding 2) and the second [2, 3)
. However, the last bin is [3, 4]
, and it includes 4.
If bins is a string, it is numpy.histogram_bin_edges
one of the supported binning strategies): 'auto', 'fd', 'doane', 'scott', 'stone', 'rice', 'sturges' or 'sqrt'.
range
:tuple or None, default: None
The lower and upper bounds of the bin. Ignore upper and lower outliers. If not provided, range is (x.min(), x.max())
. Range has no effect if bins is a sequence.
If bins is a sequence or range is specified , autoscaling is based on the specified bin range rather than the range of x.
density
: bool, default: False
If so True
, plot and return the probability density: each bin will show the bin's raw count divided by the total count and bin width ( density = counts / (sum(counts) * np.diff(bins ))
), such that the area under the histogram integrates to 1 ( np.sum(density * np.diff(bins)) == 1
).
If stacked is also stacked True
, the sum of the histograms is normalized to 1.
weights
:(n,) array-like or None, default: None
An array of weights of the same shape as x . Each value in x contributes only its associated weight to the bin count (instead of 1). If density is True
, the weights are normalized so that the integral of the density over that range remains 1.
This parameter can be used to draw a histogram of binned data, for example using numpy.histogram
(by treating each bin as a single point with a weight equal to its number)
counts, bins = np.histogram(data)
plt.hist(bins[:-1], bins, weights=counts)
(or you can also use bar()
).
cumulative
:bool or -1, default: False
If True
, computes a histogram where each bin gives the count in that bin plus the smaller of all bins. The last bin gives the total number of data points.
If density is also true True
, the histogram is normalized such that the last bin is equal to 1.
If cumulative is a number less than 0 (eg -1), the direction of accumulation is reversed. In this case, the histogram is normalized such that the first bin is equal to 1 if density is also present .True
bottom
: array-like, scalar, or None, default: None
The position of the bottom of each box, ie. The bins are bottom
drawn from to bottom + hist(x, bins)
If scalar, the bottom of each bin is shifted by the same amount. In the case of an array, each bin is shifted independently, and the length of the bottom must match the number of bins. If None, defaults to 0.
histtype
:{'bar', 'barstacked', 'step', 'stepfilled'}, default: 'bar'
- The type of histogram to plot.
bar
is a traditional bar histogram. If more than one data is given, the bars are arranged side by side.barstacked
is a bar histogram where multiple pieces of data are stacked together.step
Generates a line plot that is unfilled by default.stepfilled
Generate a line plot with default fill.
align
:{'left', 'mid', 'right'}, default: 'mid'
- The horizontal alignment of the histogram bars.
- 'left': The bar is centered on the left edge of the bin.
- 'mid': The bars are centered between the bin edges.
- 'right': The bar is centered on the right bin edge.
orientation
:{'vertical', 'horizontal'}, default: 'vertical'
If 'horizontal', barh
will be used for bar type histograms and the bottom kwarg will be the left edge.
rwidth
:float or None, default: None
The relative width of the bars, as a fraction of the bin width. If None, the width is automatically calculated.
Ignored if histtype is 'step' or 'stepfilled'.
rwidth=0.8的效果
log
:bool, default: False
If yes True
, the histogram axis will be set to a logarithmic scale.
color
:color or array-like of colors or None, default: None
A color or sequence of colors, one per dataset. Default ( None
) uses the standard line color sequence.
label
:str or None, default: None
A string or sequence of strings to match against multiple datasets. Bar plots generate multiple patches for each dataset, but only the first one gets labels, so [ legend
](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html#matplotlib .axes.Axes.legend) will work as expected.
stacked
: bool, default: False
If yes True
, multiple data are stacked on top of each other If yes , False
then multiple data are arranged side by side, if yes , multiple data are stacked on top of each otherhisttype
bar
histtype
step
4. pie
(Draw a pie chart)
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
def test():
# 设置字体为黑体
mpl.rcParams['font.family'] = 'SimHei'
# 设置在中文字体是能够正常显示负号(“-”)
mpl.rcParams['axes.unicode_minus'] = False
# 这个tuple类型
kinds = "简易箱", "保温箱", "行李箱", "密封箱"
colors = ['#e41a1c', '#377eb8', '#4daf4a', '#984ea3']
soldNums = [0.05, 0.45, 0.15, 0.35]
plt.pie(soldNums,
labels=kinds,
autopct="%3.1f%%",
startangle=60,
colors=colors)
plt.title("不同类型的箱子的销售数量占比")
plt.show()
matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=0, radius=1, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, *, normalize=True, data=None)
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.pie.html?highlight=pie#matplotlib.pyplot.pie
Draw a pie chart.
Make a pie chart of the array x . The fractional area of each wedge is x/sum(x)
given by . If sum(x) < 1
, then the value of x gives the fractional area directly, and the array is not normalized. The resulting pie chart will have an 1 - sum(x)
empty wedge of size .
Wedges are drawn counterclockwise, starting from the x-axis by default.
parameter:
x
:1D array-like
wedge size. (Given a list whose cumulative sum is equal to 1)
explode
:array-like, default: None
If not None , len(x)
an array specifying the fraction of radii to offset each wedge.
labels
:list, default: None
Sequence of strings providing labels for each wedge (tuple and list both work)
colors
:array-like, default: None
A range of colors that the pie chart will cycle through. If None , the colors in the currently active cycle will be used (this should have its own set of default color combinations).
autopct
:None or str or callable, default: None
If not None , a string or function used to label wedges with their numerical values. The label will be placed inside the wedge. If it is a format string, the label will be fmt % pct
. If it's a function, it will be called.
autopct="%4.2f%%"
4 represents the maximum number of digits displayed
2 indicates how many decimal places the decimal point retains
% means percentage calculation
pctdistance
: float, default: 0.6
The ratio between the center of each pie chart and the beginning of the text generated by autopct . Ignored if autopct is None .
shadow
:bool, default: False
Draw a shadow under the pie.
normalize
:bool, default: True
When True , always make a full pie chart by normalizing x so that sum(x) == 1
. If Falsesum(x) <= 1
then make partial pie charts and for sum( x) > 1
.
labeldistance
:float or None, default: 1.1
The radial distance to draw the pie chart labels. If set to None
, the label will not be drawn, but will be stored in legend()
the
startangle
:float, default: 0 degrees
The angle by which the starting point of the pie chart is rotated counterclockwise from the x-axis.
radius
:float, default: 1
The radius of the pie.
counterclock
:bool, default: True
Specifies the direction of the fraction, clockwise or counterclockwise.
wedgeprops
:dict, default: None
Dictionary of parameters to pass to the wedge object to make the pie. For example, you can pass in wedgeprops = {'linewidth': 3}
to set the width of the wedge border equal to 3. Check out the documentation/parameters of the wedge object for more details. By default clip_on=False
.
textprops
:dict, default: None
Dictionary of parameters to pass to the text object.
center
:(float, float), default: (0, 0)
The coordinates of the center of the chart.
frame
:bool, default: False
If true, plot the axis boxes with the chart.
rotatelabels
:bool, default: False
If True, rotate each label to the angle of the corresponding slice.
data
:indexable object, optional
If given, the following arguments also accept strings s
, which are interpreted as data[s]
(unless this raises an exception): x , explode , labels , colors
5. polar
(Drawing Polar Diagram)
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
def test():
# 设置字体为黑体
mpl.rcParams['font.family'] = 'SimHei'
# 设置在中文字体是能够正常显示负号(“-”)
mpl.rcParams['axes.unicode_minus'] = False
# 这个tuple类型
barSlices = 12
# 在0-2π均匀取12个值
theta = np.linspace(0.0, 2*np.pi, barSlices, endpoint=False)
# 生成 30*[0.1) 12个一维数组
r = 30 * np.random.rand(barSlices)
# chartreuse 是一种淡绿色
plt.polar(theta, r, color="chartreuse", linewidth=2, marker='*', mfc="b", ms=10)
plt.title("极线图")
plt.show()
matplotlib.pyplot.polar(*args, **kwargs)
Make a polar plot.
Call signature:
polar(theta, r, **kwargs)
Supports multiple theta , r parameters, with format strings eg plot
.
All parameters refer toplot
6. scatter
(Draw a bubble chart)
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
def test():
# 设置字体为黑体
mpl.rcParams['font.family'] = 'SimHei'
# 设置在中文字体是能够正常显示负号(“-”)
mpl.rcParams['axes.unicode_minus'] = False
a = np.random.randn(100)
b = np.random.randn(100)
# RdYlBu属于中间渐变色
plt.scatter(a, b, s=np.power(10*a+20*b, 2),
c=np.random.rand(100),
cmap=mpl.cm.RdYlBu,
marker="o")
plt.title("气泡图")
plt.show()
matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data=None, **kwargs)
A scatterplot of y versus x with different marker sizes and/or colors.
parameter:
x, y
: float or array-like, shape (n, )
data location.
s
:float or array-like, shape (n, ), optional
Marker size**2 in points. Default is rcParams['lines.markersize'] ** 2
.
c
:array-like or list of colors or color, optional
Marker color. Possible values:
- Map a scalar or sequence of n numbers to a color using cmap and norm . (That is to say,
cmap
select the color in the gradient color block, which can be a random number) - A two-dimensional array where rows are RGB or RGBA.
- A sequence of colors of length n.
- A single color format string.
Note that c should not be a single numeric RGB or RGBA sequence, as it is indistinguishable from an array of values to be colormapped. If you want to assign the same RGB or RGBA value to all points, use a two-dimensional array with a single row. Otherwise, where the size matches x and y , the value match will take precedence.
If you wish to specify a single color for all points, prefer the color keyword argument.
Default is None
. In this case, the marker color is determined by the value of color , facecolor , or facecolors . If neither of these is specified , the marker color is determined by the next color of the None
current 'Shape and Fill' color cycle. Axes
This loop defaults to rcParams["axes.prop_cycle"]
(default: cycler('color', ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b'、'#e377c2'、'#7f7f7f'、'#bcbd22'、'#17becf'])
).
marker
: MarkerStyle
, default: rcParams["scatter.marker"] (default:
‘o’)
markup style. marker can be an instance of a class or a textual shorthand for a specific marker. For more information on markup styles, see matplotlib.markers
.
cmap
: str or
Colormap
, default: rcParams["image.cmap"]
(default: 'viridis'
)
Colormap
Instance or registered colormap name. cmap is only used when c is an array of floats.
norm
Normalize
, default: None
If c is an array of floats, norm is used to scale the color data c in the range 0 to 1 for mapping to the colormap cmap . If None , use the default colors.Normalize
.
vmin, vmax
: float, default: None
vmin and vmax are used with the default specification to map the color array c to the colormap cmap . If None, the corresponding minimum and maximum values of the color array are used. It is an error to use vmin / vmax when norm is given .
alpha
: float, default: None
Alpha blending value, between 0 (transparent) and 1 (opaque).
linewidths
: float or array-like, default: rcParams["lines.linewidth"] (default:
1.5)
The line width of the marked edge. NOTE: The default edgecolors is 'face'. You may also want to change this setting.
edgecolors
: {'face', 'none', *None*} or color or sequence of color, default: rcParams["scatter.edgecolors"] (default:
‘face’)
The edge color of the marker. Possible values:
- 'face': The edge color will always be the same as the face color.
- 'none': Do not draw patch boundaries.
- A color or sequence of colors.
For unfilled markers, edgecolors will be ignored. Instead, the colors are determined as if using 'face', ie from c , colors or facecolors .
plotnonfinite
:bool, default: False
Whether to plot points using non-finite c (i.e. inf
, -inf
or nan
). If True, draw points using the bad colormap color (see [ Colormap.set_bad
](https://matplotlib.org/stable/api/_as_gen/matplotlib.colors.Colormap.html#matplotlib.colors.Colormap.set_bad) ).
7. stem
(Draw a cotton swab graph)
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
def test():
# 设置字体为黑体
mpl.rcParams['font.family'] = 'SimHei'
# 设置在中文字体是能够正常显示负号(“-”)
mpl.rcParams['axes.unicode_minus'] = False
x = np.linspace(0.5, 2*np.pi, 20)
# randn函数返回一个或一组样本,具有标准正态分布。
y = np.random.randn(20)
plt.stem(x, y, linefmt="-.", markerfmt="o", basefmt="-")
plt.title("棉棒图")
plt.show()
matplotlib.pyplot.stem(*args, linefmt=None, markerfmt=None, basefmt=None, bottom=0, label=None, use_line_collection=True, orientation='vertical', data=None)
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.stem.html?highlight=stem#matplotlib.pyplot.stem
Create a swab graph (stem graph).
The swab plot draws a line perpendicular to the baseline from the baseline to the heads at each location locs and places a marker there. For vertical swab plots (the default), locs are x locations and heads are y values. For horizontal stem plots, locs are y locations and heads are x values.