数据可视化的学习参考刘大成老师的《Python数据可视化之matplotlib实践》
plot()函数
功能:展现变量的变化趋势
import matplotlib.pyplot as plt
import numpy as np
# 0.05 - 10,分为1000等份
x = np.linspace(0.05,10,1000)
# 每个x值对应cos()值为y
y = np.cos(x)
plt.plot(x,y,ls="-",lw=2,label="plot figure")
plt.legend()
# jupyter notebook下自动出图
# plt.show()
参数说明:
- x:x 轴上的数值
- y:y 轴上的数值
- ls:折线图的线条风格
- lw:折线图的线条宽度
- label:标记图形内容的标签文本
# 更多详细参数,下同,函数名替换即可
help(plt.plot)
Help on function plot in module matplotlib.pyplot:
plot(*args, scalex=True, scaley=True, data=None, **kwargs)
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.DataFame` 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')
- Alternatively, if your data is already a 2d array, you can pass it
directly to *x*, *y*. A separate data set will be drawn for every
column.
Example: an array ``a`` where the first column represents the *x*
values and the other columns are the *y* columns::
>>> plot(a[0], a[1:])
- 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 the
'axes.prop_cycle' rcParam.
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)`.
Other Parameters
----------------
scalex, scaley : bool, optional, default: True
These parameters determined if the view limits are adapted to
the data limits. The values are passed on to `autoscale_view`.
**kwargs : `.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 make multiple lines with one plot command, the kwargs
apply to all those lines.
Here is a list of available `.Line2D` 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
alpha: float
animated: bool
antialiased or aa: bool
clip_box: `.Bbox`
clip_on: bool
clip_path: [(`~matplotlib.path.Path`, `.Transform`) | `.Patch` | None]
color or c: color
contains: callable
dash_capstyle: {'butt', 'round', 'projecting'}
dash_joinstyle: {'miter', 'round', 'bevel'}
dashes: sequence of floats (on/off ink in points) or (None, None)
drawstyle or ds: {'default', 'steps', 'steps-pre', 'steps-mid', 'steps-post'}, default: 'default'
figure: `.Figure`
fillstyle: {'full', 'left', 'right', 'bottom', 'top', 'none'}
gid: str
in_layout: bool
label: object
linestyle or ls: {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}
linewidth or lw: float
marker: marker style
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)
path_effects: `.AbstractPathEffect`
picker: float or callable[[Artist, Event], Tuple[bool, dict]]
pickradius: float
rasterized: bool or None
sketch_params: (scale: float, length: float, randomness: float)
snap: bool or None
solid_capstyle: {'butt', 'round', 'projecting'}
solid_joinstyle: {'miter', 'round', 'bevel'}
transform: `matplotlib.transforms.Transform`
url: str
visible: bool
xdata: 1D array
ydata: 1D array
zorder: float
Returns
-------
lines
A list of `.Line2D` objects representing the plotted data.
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
``'s'`` square marker
``'p'`` pentagon marker
``'*'`` star marker
``'h'`` hexagon1 marker
``'H'`` hexagon2 marker
``'+'`` plus marker
``'x'`` x 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'``).
scatter()函数
功能:寻找变量之间的关系
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.random.rand(1000)
plt.scatter(x,y,label="scatter figure")
plt.legend()
# plt.show()
参数说明
- x:x轴上的数值
- y:y轴上的数值
- c:散点图中的标记的颜色
- label:标记图形内容的标签文本
更多详细参数
help(plt.scatter)
xlim()函数
功能:设置x轴的数值显示范围
x = np.linspace(0.05,10,1000)
y = np.random.rand(1000)
plt.scatter(x,y,label="scatter figure")
plt.legend()
plt.xlim(0.05,10)
plt.ylim(0,1)
参数说明
- xmin:x轴上的最小值
- xmax:x轴上的最大值
- 平移性:上面的函数功能,调用签名和参数说明同样可以平移到函数 ylim()上
ylim()函数与xlim()函数类似
xlabel()函数
功能:设置 x轴的标签文本
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="plot figure")
plt.legend()
plt.xlabel("x-axis")
plt.ylabel("y-axis")
ylabel()与xlabel()函数功能类似
grid()函数
功能:绘制刻度线的网格线
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="plot figure")
plt.legend()
plt.grid(linestyle=":",color="r")
参数说明
- linestyle:网格线的线条风格
- color:网格线的线条颜色
axhline()函数
功能:绘制平行于x轴的水平参考线
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="plot figure")
plt.legend()
plt.axhline(y=0.0,c="r",ls="--",lw=2)
plt.axvline(x=4.0,c="r",ls="--",lw=2)
参数说明
- y:水平参考线的出发点
- c:参考线的线条颜色
- ls:参考线的线条风格
- lw:参考线的线条宽度
- 平移性:上面的函数功能,调用签名和参数说明同样可以平移到函数 axvline()上
axvspan()函数
功能:绘制垂直于 x轴的参考区域
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="plot figure")
plt.legend()
plt.axvspan(xmin=4.0,xmax=6.0,facecolor="y",alpha=0.3)
plt.axhspan(ymin=0.0,ymax=0.5,facecolor="y",alpha=0.3)
参数说明
- xmin:参考区域的起始位置。
- xmax:参考区域的终止位置。
- facecolor:参考区域的填充颜色。
- alpha:参考区域的填充颜色的透明度。
- 平移性:上面的函数功能、调用签名和参数说明可以平移到函数 axhspan()上
annotate()函数
功能:添加图形内容细节的指向型注释文本
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="plot figure")
plt.legend()
plt.annotate("maximum",
xy=(np.pi/2,1.0),
xytext=((np.pi/2)+1.0,.8),
weight="bold",
color="b",
arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="b"))
# plt.show()
参数说明
- string:图形内容的注释文本。
- xy:被注释图形内容的位置坐标。
- xytext:注释文本的位置坐标。
- weight:注释文本的字体粗细风格。
- color:注释文本的字体颜色。
- arrowprops:指示被注释内容的箭头的属性字典
text()函数
功能:添加图形内容细节的无指向型注释文本
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="plot figure")
plt.legend()
plt.text(3.10,0.09,"y=sin(x)",weight="bold",color="b")
参数说明
- x:注释文本内容所在位置的横坐标。
- y:注释文本内容所在位置的纵坐标。
- string:注释文本内容。
- weight:注释文本内容的粗细风格。
- color:注释文本内容的字体颜色
title()函数
功能:添加图形内容的标题
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="plot figure")
plt.legend()
plt.title("y=sin(x)")
legend()函数
功能:标示不同图形的文本标签图例
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="plot figure")
plt.legend(loc="lower left")
函数组合应用
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm as cm
# define data
x = np.linspace(0.5,3.5,100)
y = np.sin(x)
y1 = np.random.randn(100)
# scatter figure
plt.scatter(x,y1,c="0.25",label="scatter figure")
# plot figure
plt.plot(x,y,ls="--",lw=2,label="plot figure")
# some clean up(removing chartjunk)
# turn the top spine and the right spine off
for spine in plt.gca().spines.keys():
if spine == "top" or spine == "right":
plt.gca().spines[spine].set_color("none")
# turn bottom tick for x-axis on
plt.gca().xaxis.set_ticks_position("bottom")
# set tick_line position of bottom
# leave left ticks for y-axis on
plt.gca().yaxis.set_ticks_position("left")
# set tick_line position of left
# set x,yaxis limit
plt.xlim(0.0,4.0)
plt.ylim(-3.0,3.0)
# set axes labels
plt.ylabel("y_axis")
plt.xlabel("x_axis")
# set x,yaxis grid
plt.grid(True,ls=":",color="r")
# set axes labels
plt.ylabel("y_axis")
plt.xlabel("x_axis")
# set x,yaxis grid
plt.grid(True,ls=":",color="r")
# add a horizontal line across the axis
plt.axhline(y=0.0,c="r",ls="--",lw=2)
# add a vertical span across the axis
plt.axvspan(xmin=1.0,xmax=2.0,facecolor="y",alpha=.3)
# set annotating info
plt.annotate("maximum",xy=(np.pi/2,1.0),
xytext=((np.pi/2)+0.15,1.5),weight="bold",color="r",
arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="r"))
plt.annotate("spines",xy=(0.75,-3),
xytext=(0.35,-2.25),weight="bold",color="b",
arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="b"))
plt.annotate("",xy=(0,-2.78),
xytext=(0.4,-2.32),
arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="b"))
plt.annotate("",xy=(3.5,-2.98),
xytext=(3.6,-2.70),
arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="b"))
# set text info
plt.text(3.6,-2.70,"'|' is tickline",weight="bold",color="b")
plt.text(3.6,-2.95,"3.5 is ticklabel",weight="bold",color="b")
# set title
plt.title("structure of matplotlib")
# set legend
plt.legend()
# plt.show()
end~
网友评论