【Python基础】matplotlib 使用指南

news2024/9/27 12:16:19

文章目录

  • matplotlib 使用指南
    • 1 matplotlib安装与基本介绍
      • 1.1 %matplotlib inline
      • 1.2 认识matplotlib图表
    • 2 matplotlib基本使用
      • 2.1 参数
      • 2.2格式配置
      • 2.3设置x与y轴
      • 2.4 字体设置
      • 2.5 title/图例设置
      • 2.6 子图
      • 2.7 创建画布
      • 2.8 设置坐标轴
    • 3 matplotlib常用的图表
      • 3.1 折线图
      • 3.1 散点图
      • 3.3 柱状图/条形图
      • 3.4 饼状图
      • 3.5 直方图
      • 3.6 箱形图

matplotlib 使用指南

1 matplotlib安装与基本介绍

  1. 基本介绍

    Matplotlib是Python的绘图模块,他与Numpy,Pandas等配合使用,类似于MATLAB 的绘图工具;

    • Matplotlib是一个基础工具,后面所介绍的seanborn等都是基于这个模块实现;
    • Matplotlib可以在Jupyter中直接显示,是Python进行数据分析的必备模块之一;
    • 官网:https://matplotlib.org/index.html (https://matplotlib.org/index.html)
    • 案例:https://matplotlib.org/gallery/index.html (https://matplotlib.org/gallery/index.html)
  2. 安装

    pip install matplotlib

    无需anaconda环境,可直接使用

#导入matplotlib
import matplotlib.pyplot as plt
import numpy as np
#创建图表
plt.plot(np.arange(5), np.arange(5))
#显示
plt.show()


png

1.1 %matplotlib inline

%matplotlib inline作用:iPython 中定义的魔法函数(Magic Function),将matplotlib绘制的图显示在页面里中;

如果不加这句话需要调用:plt.show()

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
plt.plot(np.arange(5), np.arange(5))
plt.plot(np.arange(3), [0,2,4])
#plt.show()
[<matplotlib.lines.Line2D at 0x22a9bcd6950>]


png

1.2 认识matplotlib图表

认识画布,axis,axes 画布:图表大小,进行图表绘制

axes:坐标系,一个画布中可以指定多个坐标系

axis:坐标轴,每个坐标系都有一个坐标轴

image-20240202103119856

2 matplotlib基本使用

主要内容:

  1. 绘制基本图表;
  2. 图表格式设置;
  3. 多图与子图;

2.1 参数

plt.plot?
[1;31mSignature:[0m
[0mplt[0m[1;33m.[0m[0mplot[0m[1;33m([0m[1;33m
[0m    [1;33m*[0m[0margs[0m[1;33m:[0m [1;34m'float | ArrayLike | str'[0m[1;33m,[0m[1;33m
[0m    [0mscalex[0m[1;33m:[0m [1;34m'bool'[0m [1;33m=[0m [1;32mTrue[0m[1;33m,[0m[1;33m
[0m    [0mscaley[0m[1;33m:[0m [1;34m'bool'[0m [1;33m=[0m [1;32mTrue[0m[1;33m,[0m[1;33m
[0m    [0mdata[0m[1;33m=[0m[1;32mNone[0m[1;33m,[0m[1;33m
[0m    [1;33m**[0m[0mkwargs[0m[1;33m,[0m[1;33m
[0m[1;33m)[0m [1;33m->[0m [1;34m'list[Line2D]'[0m[1;33m[0m[1;33m[0m[0m
[1;31mDocstring:[0m
Plot y versus x as lines and/or markers.

Call signatures::

    plot([x], y, [fmt], *, data=None, **kwargs)
    plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)

The coordinates of the points or line nodes are given by *x*, *y*.

The optional parameter *fmt* is a convenient way for defining basic
formatting like color, marker and linestyle. It's a shortcut string
notation described in the *Notes* section below.

>>> plot(x, y)        # plot x and y using default line style and color
>>> plot(x, y, 'bo')  # plot x and y using blue circle markers
>>> plot(y)           # plot y using x as index array 0..N-1
>>> plot(y, 'r+')     # ditto, but with red plusses

You can use `.Line2D` properties as keyword arguments for more
control on the appearance. Line properties and *fmt* can be mixed.
The following two calls yield identical results:

>>> plot(x, y, 'go--', linewidth=2, markersize=12)
>>> plot(x, y, color='green', marker='o', linestyle='dashed',
...      linewidth=2, markersize=12)

When conflicting with *fmt*, keyword arguments take precedence.


Plotting labelled data

There's a convenient way for plotting objects with labelled data (i.e.
data that can be accessed by index ``obj['y']``). Instead of giving
the data in *x* and *y*, you can provide the object in the *data*
parameter and just give the labels for *x* and *y*::

>>> plot('xlabel', 'ylabel', data=obj)

All indexable objects are supported. This could e.g. be a `dict`, a
`pandas.DataFrame` or a structured numpy array.


Plotting multiple sets of data

There are various ways to plot multiple sets of data.

- The most straight forward way is just to call `plot` multiple times.
  Example:

  >>> plot(x1, y1, 'bo')
  >>> plot(x2, y2, 'go')

- If *x* and/or *y* are 2D arrays a separate data set will be drawn
  for every column. If both *x* and *y* are 2D, they must have the
  same shape. If only one of them is 2D with shape (N, m) the other
  must have length N and will be used for every data set m.

  Example:

  >>> x = [1, 2, 3]
  >>> y = np.array([[1, 2], [3, 4], [5, 6]])
  >>> plot(x, y)

  is equivalent to:

  >>> for col in range(y.shape[1]):
  ...     plot(x, y[:, col])

- The third way is to specify multiple sets of *[x]*, *y*, *[fmt]*
  groups::

  >>> plot(x1, y1, 'g^', x2, y2, 'g-')

  In this case, any additional keyword argument applies to all
  datasets. Also, this syntax cannot be combined with the *data*
  parameter.

By default, each line is assigned a different style specified by a
'style cycle'. The *fmt* and line property parameters are only
necessary if you want explicit deviations from these defaults.
Alternatively, you can also change the style cycle using
:rc:`axes.prop_cycle`.


Parameters
----------
x, y : array-like or scalar
The horizontal / vertical coordinates of the data points.
x values are optional and default to range(len(y)).

    Commonly, these parameters are 1D arrays.

    They can also be scalars, or two-dimensional (in that case, the
    columns represent separate data sets).

    These arguments cannot be passed as keywords.

fmt : str, optional
    A format string, e.g. 'ro' for red circles. See the *Notes*
    section for a full description of the format strings.

    Format strings are just an abbreviation for quickly setting
    basic line properties. All of these and more can also be
    controlled by keyword arguments.

    This argument cannot be passed as keyword.

data : indexable object, optional
    An object with labelled data. If given, provide the label names to
    plot in *x* and *y*.

    .. note::
        Technically there's a slight ambiguity in calls where the
        second label is a valid *fmt*. ``plot('n', 'o', data=obj)``
        could be ``plt(x, y)`` or ``plt(y, fmt)``. In such cases,
        the former interpretation is chosen, but a warning is issued.
        You may suppress the warning by adding an empty format string
        ``plot('n', 'o', '', data=obj)``.

Returns
-------
list of `.Line2D`
    A list of lines representing the plotted data.

Other Parameters
----------------
scalex, scaley : bool, default: True
    These parameters determine if the view limits are adapted to the
    data limits. The values are passed on to
    `~.axes.Axes.autoscale_view`.

**kwargs : `~matplotlib.lines.Line2D` properties, optional
    *kwargs* are used to specify properties like a line label (for
    auto legends), linewidth, antialiasing, marker face color.
    Example::

    >>> plot([1, 2, 3], [1, 2, 3], 'go-', label='line 1', linewidth=2)
    >>> plot([1, 2, 3], [1, 4, 9], 'rs', label='line 2')

    If you specify multiple lines with one plot call, the kwargs apply
    to all those lines. In case the label object is iterable, each
    element is used as labels for each set of data.

    Here is a list of available `.Line2D` properties:

    Properties:
    agg_filter: a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array and two offsets from the bottom left corner of the image
    alpha: scalar or None
    animated: bool
    antialiased or aa: bool
    clip_box: `~matplotlib.transforms.BboxBase` or None
    clip_on: bool
    clip_path: Patch or (Path, Transform) or None
    color or c: color
    dash_capstyle: `.CapStyle` or {'butt', 'projecting', 'round'}
    dash_joinstyle: `.JoinStyle` or {'miter', 'round', 'bevel'}
    dashes: sequence of floats (on/off ink in points) or (None, None)
    data: (2, N) array or two 1D arrays
    drawstyle or ds: {'default', 'steps', 'steps-pre', 'steps-mid', 'steps-post'}, default: 'default'
    figure: `~matplotlib.figure.Figure`
    fillstyle: {'full', 'left', 'right', 'bottom', 'top', 'none'}
    gapcolor: color or None
    gid: str
    in_layout: bool
    label: object
    linestyle or ls: {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}
    linewidth or lw: float
    marker: marker style string, `~.path.Path` or `~.markers.MarkerStyle`
    markeredgecolor or mec: color
    markeredgewidth or mew: float
    markerfacecolor or mfc: color
    markerfacecoloralt or mfcalt: color
    markersize or ms: float
    markevery: None or int or (int, int) or slice or list[int] or float or (float, float) or list[bool]
    mouseover: bool
    path_effects: list of `.AbstractPathEffect`
    picker: float or callable[[Artist, Event], tuple[bool, dict]]
    pickradius: float
    rasterized: bool
    sketch_params: (scale: float, length: float, randomness: float)
    snap: bool or None
    solid_capstyle: `.CapStyle` or {'butt', 'projecting', 'round'}
    solid_joinstyle: `.JoinStyle` or {'miter', 'round', 'bevel'}
    transform: unknown
    url: str
    visible: bool
    xdata: 1D array
    ydata: 1D array
    zorder: float

See Also
--------
scatter : XY scatter plot with markers of varying size and/or color (
    sometimes also called bubble chart).

Notes
-----
**Format Strings**

A format string consists of a part for color, marker and line::

    fmt = '[marker][line][color]'

Each of them is optional. If not provided, the value from the style
cycle is used. Exception: If ``line`` is given, but no ``marker``,
the data will be a line without markers.

Other combinations such as ``[color][marker][line]`` are also
supported, but note that their parsing may be ambiguous.

**Markers**

=============   ===============================
character       description
=============   ===============================
``'.'``         point marker
``','``         pixel marker
``'o'``         circle marker
``'v'``         triangle_down marker
``'^'``         triangle_up marker
``'<'``         triangle_left marker
``'>'``         triangle_right marker
``'1'``         tri_down marker
``'2'``         tri_up marker
``'3'``         tri_left marker
``'4'``         tri_right marker
``'8'``         octagon marker
``'s'``         square marker
``'p'``         pentagon marker
``'P'``         plus (filled) marker
``'*'``         star marker
``'h'``         hexagon1 marker
``'H'``         hexagon2 marker
``'+'``         plus marker
``'x'``         x marker
``'X'``         x (filled) marker
``'D'``         diamond marker
``'d'``         thin_diamond marker
``'|'``         vline marker
``'_'``         hline marker
=============   ===============================

**Line Styles**

=============    ===============================
character        description
=============    ===============================
``'-'``          solid line style
``'--'``         dashed line style
``'-.'``         dash-dot line style
``':'``          dotted line style
=============    ===============================

Example format strings::

    'b'    # blue markers with default shape
    'or'   # red circles
    '-g'   # green solid line
    '--'   # dashed line with default color
    '^k:'  # black triangle_up markers connected by a dotted line

**Colors**

The supported color abbreviations are the single letter codes

=============    ===============================
character        color
=============    ===============================
``'b'``          blue
``'g'``          green
``'r'``          red
``'c'``          cyan
``'m'``          magenta
``'y'``          yellow
``'k'``          black
``'w'``          white
=============    ===============================

and the ``'CN'`` colors that index into the default property cycle.

If the color is the only part of the format string, you can
additionally use any  `matplotlib.colors` spec, e.g. full names
(``'green'``) or hex strings (``'#008000'``).
[1;31mFile:[0m      d:\software\anaconda\lib\site-packages\matplotlib\pyplot.py
[1;31mType:[0m      function

2.2格式配置

文档:https://matplotlib.org/stable/api/index.html

方法说明
plt.plot(fmt, data, **kwargs)绘制线状图,fmt 是字符串,表示线与点的属性。data 是数据。kwargs 是关键字参数。
  1. 设置线型

    fmt格式:‘-or’,分别代表:线型,点形状,颜色,顺序可以颠倒

    符号说明
    '-'实线样式
    '--'虚线样式
    '-.'点划线样式
    ':'点线样式
  2. 设置点

    符号说明
    '.'点标记
    ','像素标记
    'o'圆形标记
    'v'朝下的三角形标记
    '^'朝上的三角形标记
    '<'朝左的三角形标记
    '>'朝右的三角形标记
y = range(1,4)
plt.plot(y,'-.or')
[<matplotlib.lines.Line2D at 0x22a9c913c50>]


png

  1. 设置颜色
    符号说明
    'b'蓝色
    'g'绿色
    'r'红色
    'c'青色
    'm'洋红色
    'y'黄色
    'k'黑色
    'w'白色
#fmt:--:虚线,o:点形状为圆,r:红色,, linewidth:设置线宽度
plt.plot(range(4), '--or', linewidth=2)
#fmt:-.:虚线,<:点形状为三角,r:蓝色
plt.plot(range(1,5), '-.<b')
[<matplotlib.lines.Line2D at 0x22a9da02c10>]


png

#设置xy轴范围(-10, 10)
import matplotlib.pyplot as plt
plt.xlim(-10,10)
plt.ylim(-10,10)
#折线图
plt.plot(range(-10,10),range(-10,10), '-o')
#设置x-ylabel
plt.xlabel('x-value')
plt.ylabel('y-value')
#设置x轴显示坐标值(x轴值与显示值要对应,例如:-10:a, -9:b...10:t)
xticks = [chr(v) for v in range(ord('a'), ord('a')+20)]
_ = plt.xticks(range(-10,10),xticks)


png

2.3设置x与y轴

坐标轴设置包括:

坐标轴值,坐标轴显示值,坐标轴标签

操作说明
设置 x 轴标签plt.xlabel(s, *args, **kwargs)
设置 y 轴标签plt.ylabel(s, *args, **kwargs)
设置 x 轴刻度plt.xticks(*args, **kwargs)
设置 y 轴刻度plt.yticks(*args, **kwargs)
设置 x 轴范围plt.xlim(*args, **kwargs)
设置 y 轴范围plt.ylim(*args, **kwargs)
#设置xy轴范围(-10, 10)
import matplotlib.pyplot as plt
plt.xlim(-10,10)
plt.ylim(-10,10)
#折线图
plt.plot(range(-10,10),range(-10,10), '-o')
#设置x-ylabel
plt.xlabel('x-value')
plt.ylabel('y-value')
#设置x轴显示坐标值(x轴值与显示值要对应,例如:-10:a, -9:b...10:t)
xticks = [chr(v) for v in range(ord('a'), ord('a')+20)]
_ = plt.xticks(range(-10,10),xticks)


png

2.4 字体设置

字体设置包括大小,字体,颜色,旋转角度等

参考链接:https://matplotlib.org/api/text_api.html (https://matplotlib.org/api/text_api.html)

#设置xy轴范围(-10, 10)
import matplotlib.pyplot as plt
#折线图
plt.plot(range(5), '-o')
#设置x-ylabel
_ = plt.xlabel('x-value')
_ = plt.ylabel('y-value')
_ = plt.xticks(rotation=30, fontfamily = 'fantasy', size=20,alpha=0.7,visible=True,c='r')


png

2.5 title/图例设置

plt.title(label, fontdict=None, loc='center', pad=None, **kwargs):设置标题;

plt.legend(*args, **kwargs):设置图例,例如一个图表中可以绘制多个折现,每个折现代表说明可以使用legend标识;档;

import matplotlib.pyplot as plt
#折线图
plt.plot(range(5), '-o', label="line1")
plt.plot(range(1,6), '-o', label="line2")
#设置x-ylabel
plt.legend()
plt.title("case1")
Text(0.5, 1.0, 'case1')


png

import matplotlib.pyplot as plt
#折线图
line1 = plt.plot(range(5), '-o')
line2 = plt.plot(range(1,6), '-o')
#设置x-ylabel
plt.legend(('l1', 'l2'))
plt.title("case1")
Text(0.5, 1.0, 'case1')


png

plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
import matplotlib.pyplot as plt
#折线图
line1 = plt.plot(range(5), '-o')
line2 = plt.plot(range(1,6), '-o')
#设置x-ylabel
plt.legend(('l1', 'l2'))
plt.title("案例1")
Text(0.5, 1.0, '案例1')


png

2.6 子图

添加多个图表方式可以使用子图及多图

plt.subplot(*args, **kwargs):返回axes

plt.subplot(1,2,1) #同:plt.subplot(121)
plt.subplot(1,2,2) #同:plt.subplot(122)
<Axes: >


png

plt.subplot(2,1,1)# 同plt.subplot(211)
plt.subplot(2,1,2)# 同plt.subplot(212)
<Axes: >


png

#axes1
axes1 = plt.subplot(221)
axes1.plot(range(4))
axes1.set_facecolor('r')
#axes2
axes2 = plt.subplot(222)
axes2.plot(range(1,5))
axes2.set_facecolor('y')
#axes3
axes3 = plt.subplot(223)
axes3.plot(range(2,6))
axes3.set_facecolor('b')
#axes4
axes4 =plt.subplot(224)
axes4.plot(range(3,7))
axes4.set_facecolor('c')


png

2.7 创建画布

#创建画布1
plt.figure(1,figsize=(6, 3))
plt.plot([1,2,3])
#创建画布2
plt.figure(2,figsize=(5, 5), facecolor='y')
plt.plot([4,5,6])
#获取当前坐标系
ax = plt.gca()
#设置颜色为ax
ax.set_facecolor('r')


png

png

2.8 设置坐标轴

默认坐标系为00,有时候我们希望坐标轴中心点在其他位置,且只希望显示x,y两个轴,实现如下

import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
#支持中文
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
#结局负数乱码
plt.rcParams['axes.unicode_minus']=False
x = np.linspace(-4, 4, 200)
y = np.sin(x)
plt.figure()
#获取当前axes
ax = plt.gca()
#坐标系中right,top不显示
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
#设置ticks显示位置
ax.xaxis.set_ticks_position('bottom') 
ax.yaxis.set_ticks_position('left')
#移动坐标轴位置:中心位置为(1,0)
ax.spines['left'].set_position(('data', 1))
ax.spines['bottom'].set_position(('data', 0))
plt.plot(x, y)

[<matplotlib.lines.Line2D at 0x22a9defb490>]


png

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist
fig = plt.figure('Sine Wave', (6,4))
#创建axisartist对象
ax = axisartist.Subplot(fig, 1,1,1)
#增加坐标系
fig.add_axes(ax)
#坐标设置不显示
ax.axis[:].set_visible(False)
#指定坐标系位置
ax.axis["x"] = ax.new_floating_axis(0, 0)
ax.axis["y"] = ax.new_floating_axis(1, 0)
ax.axis["x"].set_axis_direction('top')
ax.axis["y"].set_axis_direction('left')
#设置坐标轴格式
ax.axis["x"].set_axisline_style("->", size = 2.0)
ax.axis["y"].set_axisline_style("->", size = 2.0)
t = np.linspace(0, 1*np.pi)
y = 2*np.sin(2*t)
ax.plot(t, y, color = 'red', linewidth = 2)
plt.title('y = 2sin(2t)',fontsize = 14)
#设置x轴刻度
ax.set_xticks(np.linspace(0.25,1.25,6)*np.pi)
ax.set_xticklabels(['$\\frac{\pi}{4}$','$\\frac{\pi}{2}$', '$\\frac{3\pi}{4}$', '$\pi$', '$\\frac{5\pi}{4}$', '$\\frac{3\pi}{2}$'])
ax.set_yticks([0, 1, 2])
#设置xy周范围
ax.set_xlim(-0.5*np.pi,1.5*np.pi)
ax.set_ylim(-2.2, 2.2)
(-2.2, 2.2)


png

3 matplotlib常用的图表

3.1 折线图

  • 折线图:趋势变化关系,场合时间等结合起来使用;
  • 例如:新增用户,累计用户,股票数据等;
#案例:近一周某用户粉丝每天增长量与累积粉丝数:
import matplotlib.pyplot as plt
import numpy as np
ydata = [100, 90, 120, 150, 200, 300, 210]
sumydata = np.cumsum(ydata)
plt.plot(ydata)
plt.plot(sumydata)
plt.legend(['新增粉丝', '累计粉丝'])
plt.title("我的粉丝")
Text(0.5, 1.0, '我的粉丝')


png

3.1 散点图

  • 显示若干数据系列中各数值之间的关系,一定程度反映数据分布关系;
  • 适用于维度较少数据
  • 方法:plt.scatter(x, y, s=None, c=None,marker=None,…,alpha=None,**kwargs)
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
%matplotlib inline
xValue = list(range(0, 101))
yValue = [x * np.random.rand() for x in xValue]
plt.title(u'散点图')
plt.xlabel('x-data')
plt.ylabel('y-data')
plt.scatter(xValue, yValue, s=20, marker='o')
<matplotlib.collections.PathCollection at 0x22a9f1f5190>


png

3.3 柱状图/条形图

显示一段时间内的数据变化或显示各项之间的比较情况

例如:数据对比

import matplotlib.pyplot as plt43v发v 
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
import numpy as np
plt.figure(figsize=(8,4))
x=np.arange(4)
gdp_2019 = [10.7, 9.9,7.1,6.2]
#设置柱状图的宽度
bar_width=0.3
tick_label=['广东省','江苏省','山东省','浙江省']
#绘制并列柱状图
plt.bar(x,gdp_2019,bar_width,color='salmon',label='2019')
plt.title('GDP(万亿)')
plt.legend()
_ = plt.xticks(x,tick_label)


png

import matplotlib.pyplot as plt
dic = {'a': 22, 'b': 10, 'c': 6, 'd': 4, 'e': 2, 'f': 10, 'g': 24, 'h': 16, 'i': 1, 'j': 12}
s = sorted(dic.items(), key=lambda x: x[1], reverse=False) # 对dict 按照value排序 True表示翻转 ,转为了列表形式
x_x = []
y_y = []
for i in s:
 x_x.append(i[0])
 y_y.append(i[1])
x = x_x
y = y_y
fig, ax = plt.subplots()
ax.barh(x, y)
labels = ax.get_xticklabels()
plt.setp(labels, rotation=0, horizontalalignment='right')
for a, b in zip(x, y):
 plt.text(b+1, a, b, ha='center', va='center')
plt.ylabel('name')
plt.xlabel('数量')
plt.title("分布")
Text(0.5, 1.0, '分布')


png

3.4 饼状图

总体数据中,各项与总和的比例;

各个数据对比,例如:不同地区销售额占比;

plt.pie(x, explode=None, labels=None…)

import matplotlib.pyplot as plt
# 构造数据
edu = [100,200,900, 600, 1000]
labels = ['中专','大专','本科','硕士','其他']
plt.pie(x = edu, labels=labels, autopct='%.1f%%')
plt.title('职工教育分布')
plt.show()


png

3.5 直方图

数据分布情况

plt.hist(x, bins=None, range=None,…).

import matplotlib.pyplot as plt
import numpy as np
# 构造数据
data = np.random.randn(10000)
plt.hist(data)
plt.show()


png

3.6 箱形图

箱形图可以很直观的描述数据的最大值、最小值、中位数、上四分位数、下四分位数、异常值,

plt.boxplot(x, notch=None, sym=None, vert=None, whis=None)

import matplotlib.pyplot as plt
import numpy as np
# 构造数据
data = np.random.randn(10000)
plt.boxplot(data)
plt.show()


png

练习:

'''
在Python中: x = np.random.randn(20) y = np.random.randn(20) 使用matplolit绘制散点图
'''
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(20) # 生成20个随机数作为x轴数据
y = np.random.randn(20) # 生成20个随机数作为y轴数据
plt.scatter(x, y) # 绘制散点图
plt.show() # 显示图形


png

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1429716.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

vue3、vue2以及非vue项目中拖拽改变dom结构以及数组顺序 vuedraggable

文章目录 vue3、vue2以及非vue项目中拖拽改变dom结构以及数组顺序先看效果属性名称 说明具体代码 vue3、vue2以及非vue项目中拖拽改变dom结构以及数组顺序 参考链接&#xff0c;这个还有其他库的教程&#xff0c;可以收藏的 展示效果 github地址 vue.draggable.next 是一款vu…

如何使用 ZoomEye 搜索引擎保姆级教程(附链接)

一、介绍 ZoomEye&#xff08;中文名&#xff1a;钟馗之眼&#xff09;是一款专注于网络设备和物联网设备搜索的搜索引擎。它提供了一种通过互联网上的设备进行搜索的方式&#xff0c;使用户能够发现和分析各种连接到互联网的设备&#xff0c;包括服务器、路由器、摄像头、数据…

Linux 网络编程 + 笔记

协议&#xff1a;一组规则 分层模型结构&#xff1a; OSI七层模型&#xff1a;物理层、数据链路层、网络层、传输层、会话层、表示层、应用层TCP/IP 4层模型&#xff1a;链路层/网络接口层、网络层、传输层、应用层 应用层&#xff1a;http、ftp、nfs、ssh、telnet、传输层&am…

【24美赛思路已出】2024年美赛A~F题解题思路已出 | 无偿自提

A题&#xff1a;资源可用性和性别比例 问题一&#xff1a; 涉及当灯鱼种群的性别比例发生变化时&#xff0c;对更大的生态系统产生的影响。为了分析这个问题&#xff0c;可以采用以下的数学建模思路&#xff1a;建立灯鱼种群模型&#xff1a; 首先&#xff0c;建立一个灯鱼种群…

爬虫入门到精通_基础篇4(BeautifulSoup库_解析库,基本使用,标签选择器,标准选择器,CSS选择器)

1 Beautiful说明 BeautifulSoup库是灵活又方便的网页解析库&#xff0c;处理高效&#xff0c;支持多种解析器。利用它不用编写正则表达式即可方便地实线网页信息的提取。 安装 pip3 install beautifulsoup4解析库 解析器使用方法优势劣势Python标准库BeautifulSoup(markup,…

Session与Cookie、部署redis、redis基本操作、Session共享

1 案例1&#xff1a;PHP的本地Session信息 1.1 问题 通过Nginx调度器负载后端两台Web服务器&#xff0c;实现以下目标&#xff1a; 部署Nginx为前台调度服务器调度算法设置为轮询后端为两台LNMP服务器部署测试页面&#xff0c;查看PHP本地的Session信息 1.2 方案 实验拓扑…

亚马逊新店铺视频怎么上传?视频验证失败怎么办?——站斧浏览器

亚马逊新店铺视频怎么上传&#xff1f; 登录亚马逊卖家中心&#xff1a;首先&#xff0c;卖家需要登录亚马逊卖家中心。在登录后&#xff0c;可以点击左侧导航栏上的“库存”选项&#xff0c;然后选择“新增或管理商品”。 选择商品&#xff1a;接下来&#xff0c;在“新增或…

如何部署Node.js服务并实现无公网ip远程访问本地项目【内网穿透】

文章目录 前言1.安装Node.js环境2.创建node.js服务3. 访问node.js 服务4.内网穿透4.1 安装配置cpolar内网穿透4.2 创建隧道映射本地端口 5.固定公网地址 前言 Node.js 是能够在服务器端运行 JavaScript 的开放源代码、跨平台运行环境。Node.js 由 OpenJS Foundation&#xff0…

YOLOv8进阶 | 如何用yolov8训练自己的数据集(以安全帽佩戴检测举例)

前言&#xff1a;Hello大家好&#xff0c;我是小哥谈。YOLOv8是一种目标检测算法&#xff0c;它是YOLO&#xff08;You Only Look Once&#xff09;系列算法的最新版本。本节课就带领大家如何基于YOLOv8来训练自己的目标检测模型&#xff0c;本次作者就以安全帽佩戴检测为案例进…

二叉树的最小深度

给定一个二叉树&#xff0c;找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 说明&#xff1a;叶子节点是指没有子节点的节点。 示例 1&#xff1a; 输入&#xff1a;root [3,9,20,null,null,15,7] 输出&#xff1a;2示例 2&#xff1a; 输入&…

postgres:锁申请

什么是弱锁&#xff0c;强锁&#xff1f; 为了提高并发控制&#xff0c;PG通过将锁信息在本地缓存&#xff08;**LOCALLOCK**&#xff09;和快速处理常见锁&#xff08;fastpath&#xff09;&#xff0c;减少了对共享内存的访问&#xff0c;提高性能。从而出现了弱锁和强锁的概…

如何在win系统部署开源云图床Qchan并无公网ip访问本地存储图片

文章目录 前言1. Qchan网站搭建1.1 Qchan下载和安装1.2 Qchan网页测试1.3 cpolar的安装和注册 2. 本地网页发布2.1 Cpolar云端设置2.2 Cpolar本地设置 3. 公网访问测试总结 前言 图床作为云存储的一项重要应用场景&#xff0c;在大量开发人员的努力下&#xff0c;已经开发出大…

【自然语言处理】P2 PyTorch 基础 - 张量

目录 安装 PyTorch张量创建张量操作张量索引、切片、联合操作 CUDA张量 本系列博文我们将使用 PyTorch 来实现深度学习模型等。PyTorch 是一个开源的、社区驱动的深度学习框架。拥有强大的工具和库生态系统&#xff0c;包含 TorchVision&#xff08;用于图像处理&#xff09;、…

后端软件三层架构

一、三层架构简介 三层架构是软件开发中广泛采用的一种经典架构模式&#xff0c;其核心价值在于通过清晰的任务划分来提高代码的可维护性和重用性。具体来说&#xff0c;三层架构主要包括以下三个层次&#xff1a; 持久层&#xff08;DAO层&#xff09;&#xff1a;这一层主要…

和鲸科技与智谱AI达成合作,共建大模型生态基座

近日&#xff0c;上海和今信息科技有限公司&#xff08;简称“和鲸科技”&#xff09;与北京智谱华章科技有限公司&#xff08;简称“智谱AI”&#xff09;签订合作协议&#xff0c;双方将携手推动国产通用大模型的广泛应用与行业渗透&#xff0c;并积极赋能行业伙伴探索领域大…

10:00进去,10:06就出来了,面试问的问题真难。。。

&#x1f345; 视频学习&#xff1a;文末有免费的配套视频可观看 &#x1f345; 关注公众号【互联网杂货铺】&#xff0c;回复 1 &#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 刚从小厂出来&#xff0c;没想到在另一家公司我又寄了。 在这家…

在线JSON转CSV工具

在线JSON转CSV - BTool在线工具软件&#xff0c;为开发者提供方便。本工具可以在浏览器本地将JSON转换成CSV文件,并下载转换后的CSV文件。https://www.btool.cn/json-to-csv 在大数据时代&#xff0c;数据处理与交换已经成为日常工作生活中的常态。而JSON和CSV作为两种广…

Go语言的100个错误使用场景(21-29)|数据类型

前言 大家好&#xff0c;这里是白泽。 《Go语言的100个错误以及如何避免》 是最近朋友推荐我阅读的书籍&#xff0c;我初步浏览之后&#xff0c;大为惊喜。就像这书中第一章的标题说到的&#xff1a;“Go: Simple to learn but hard to master”&#xff0c;整本书通过分析100…

Flutter向 开发人员需要了解的和颜色有关的知识

前言 构建应用前台的开发人员常常需要和颜色打交道&#xff0c;即使很多时候&#xff0c;前台人员不用自己设计颜色&#xff0c;而是由设计师给出颜色&#xff0c;不过经常和颜色打交道&#xff0c;整理和颜色有关的知识还是开卷有益的 flutter中指定颜色的常用方式 Color.f…

海外IP代理:解锁网络边界的实战利器

文章目录 引言&#xff1a;正文&#xff1a;一、Roxlabs全球IP代理服务概览特点&#xff1a;覆盖范围&#xff1a;住宅IP真实性&#xff1a;性价比&#xff1a;在网络数据采集中的重要性&#xff1a; 二、实战应用案例一&#xff1a;跨境电商竞品分析步骤介绍&#xff1a;代码示…