- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python 数据科学 Matplotlib图库详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
matplotlib 是 python 的二维绘图库,用于生成符合出版质量或跨平台交互环境的各类图形.
图形解析 。
工作流 。
matplotlib 绘图的基本步骤: 1 准备数据 。
2 创建图形 。
3 绘图 。
4 自定义设置 。
5 保存图形 。
6 显示图形 。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import
matplotlib.pyplot as plt
x
=
[
1
,
2
,
3
,
4
]
# step1
y
=
[
10
,
20
,
25
,
30
]
fig
=
plt.figure()
# step2
ax
=
fig.add_subplot(
111
)
# step3
ax.plot(x, y, color
=
'lightblue'
, linewidth
=
3
)
# step3\4
ax.scatter([
2
,
4
,
6
],
[
5
,
15
,
25
],
color
=
'darkgreen'
,
marker
=
'^'
)
ax.set_xlim(
1
,
6.5
)
plt.savefig(
'foo.png'
)
# step5
plt.show()
# step6
|
一维数据 。
1
2
3
4
5
|
import
numpy as np
x
=
np.linspace(
0
,
10
,
100
)
y
=
np.cos(x)
z
=
np.sin(x)
|
二维数据或图片 。
1
2
3
4
5
6
7
|
data
=
2
*
np.random.random((
10
,
10
))
data2
=
3
*
np.random.random((
10
,
10
))
y, x
=
np.mgrid[
-
3
:
3
:
100j
,
-
3
:
3
:
100j
]
u
=
-
1
-
x
*
*
2
+
y
v
=
1
+
x
-
y
*
*
2
from
matplotlib.cbook
import
get_sample_data
img
=
np.load(
'e:/anaconda3/envs/torch/lib/site-packages/matplotlib/mpl-data/aapl.npz'
)
|
绘制图形 。
1
|
import
matplotlib.pyplot as plt
|
画布 。
1
2
|
fig
=
plt.figure()
fig2
=
plt.figure(figsize
=
plt.figaspect(
2.0
))
|
坐标轴 。
图形是以坐标轴为核心绘制的,大多数情况下,子图就可以满足需求。子图是栅格系统的坐标轴.
1
2
3
4
5
|
fig.add_axes()
ax1
=
fig.add_subplot(
221
)
# row-col-num
ax3
=
fig.add_subplot(
212
)
fig3, axes
=
plt.subplots(nrows
=
2
,ncols
=
2
)
fig4, axes2
=
plt.subplots(ncols
=
3
)
|
一维数据 。
1
2
3
4
5
6
7
8
9
|
fig, ax
=
plt.subplots()
lines
=
ax.plot(x,y)
# 用线或标记连接点
ax.scatter(x,y)
# 缩放或着色未连接的点
axes[
0
,
0
].bar([
1
,
2
,
3
],[
3
,
4
,
5
])
# 绘制等宽纵向矩形
axes[
1
,
0
].barh([
0.5
,
1
,
2.5
],[
0
,
1
,
2
])
# 绘制等高横向矩形
axes[
1
,
1
].axhline(
0.45
)
# 绘制与轴平行的横线
axes[
0
,
1
].axvline(
0.65
)
# 绘制与轴垂直的竖线
ax.fill(x,y,color
=
'blue'
)
# 绘制填充多边形
ax.fill_between(x,y,color
=
'yellow'
)
# 填充y值和0之间
|
二维数据或图片 。
1
2
3
4
5
6
7
8
9
10
11
|
import
matplotlib.image as imgplt
img
=
imgplt.imread(
'c:/users/administrator/desktop/timg.jpg'
)
fig, ax
=
plt.subplots()
im
=
ax.imshow(img, cmap
=
'gist_earth'
, interpolation
=
'nearest'
, vmin
=
-
200
, vmax
=
200
)
# 色彩表或rgb数组
axes2[
0
].pcolor(data2)
# 二维数组伪彩色图
axes2[
0
].pcolormesh(data)
# 二维数组等高线伪彩色图
cs
=
plt.contour(y,x,u)
# 等高线图
axes2[
2
].contourf(data)
axes2[
2
]
=
ax.clabel(cs)
# 等高线图标签
|
向量场 。
1
2
3
|
axes[
0
,
1
].arrow(
0
,
0
,
0.5
,
0.5
)
# 为坐标轴添加箭头
axes[
1
,
1
].quiver(y,z)
# 二维箭头
axes[
0
,
1
].streamplot(x,y,u,v)
# 二维箭头
|
数据分布 。
1
2
3
|
ax1.hist(y)
# 直方图
ax3.boxplot(y)
# 箱形图
ax3.violinplot(z)
# 小提琴图
|
自定义图形 颜色、色条与色彩表 。
1
2
3
4
5
6
|
plt.plot(x, x, x, x
*
*
2
, x, x
*
*
3
)
ax.plot(x, y, alpha
=
0.4
)
ax.plot(x, y, c
=
'k'
)
fig.colorbar(im, orientation
=
'horizontal'
)
im
=
ax.imshow(img,
cmap
=
'seismic'
)
|
标记 。
1
2
3
|
fig, ax
=
plt.subplots()
ax.scatter(x,y,marker
=
"."
)
ax.plot(x,y,marker
=
"o"
)
|
线型 。
1
2
3
4
5
|
plt.plot(x,y,linewidth
=
4.0
)
plt.plot(x,y,ls
=
'solid'
)
plt.plot(x,y,ls
=
'--'
)
plt.plot(x,y,
'--'
,x
*
*
2
,y
*
*
2
,
'-.'
)
plt.setp(lines,color
=
'r'
,linewidth
=
4.0
)
|
文本与标注 。
1
2
3
4
5
6
7
8
9
10
11
|
ax.text(
1
,
-
2.1
,
'example graph'
,
style
=
'italic'
)
ax.annotate(
"sine"
,
xy
=
(
8
,
0
),
xycoords
=
'data'
,
xytext
=
(
10.5
,
0
),
textcoords
=
'data'
,
arrowprops
=
dict
(arrowstyle
=
"->"
,
connectionstyle
=
"arc3"
),)
|
数学符号 。
1
|
plt.title(r
'$sigma_i=15$'
, fontsize
=
20
)
|
尺寸限制与自动调整 。
1
2
3
4
|
ax.margins(x
=
0.0
,y
=
0.1
)
# 添加内边距
ax.axis(
'equal'
)
# 将图形纵横比设置为1
ax.
set
(xlim
=
[
0
,
10.5
],ylim
=
[
-
1.5
,
1.5
])
# 设置x轴与y轴的限
ax.set_xlim(
0
,
10.5
)
|
图例 。
1
2
3
4
|
ax.
set
(title
=
'an example axes'
,
ylabel
=
'y-axis'
,
xlabel
=
'x-axis'
)
# 设置标题与x、y轴的标签
ax.legend(loc
=
'best'
)
# 自动选择最佳的图例位置
|
标记 。
1
2
3
4
5
|
ax.xaxis.
set
(ticks
=
range
(
1
,
5
),
ticklabels
=
[
3
,
100
,
-
12
,
"foo"
])
# 手动设置x轴刻度
ax.tick_params(axis
=
'y'
,
direction
=
'inout'
,
length
=
10
)
# 设置y轴长度与方向
|
子图间距 。
1
2
3
4
5
6
7
|
fig3.subplots_adjust(wspace
=
0.5
,
hspace
=
0.3
,
left
=
0.125
,
right
=
0.9
,
top
=
0.9
,
bottom
=
0.1
)
fig.tight_layout()
# 设置画布的子图布局
|
坐标轴边线 。
1
2
|
ax1.spines[
'top'
].set_visible(false)
# 隐藏顶部坐标轴线
ax1.spines[
'bottom'
].set_position((
'outward'
,
10
))
# 设置底部边线的位置为outward
|
保存 。
1
2
3
4
|
#保存画布
plt.savefig(
'foo.png'
)
# 保存透明画布
plt.savefig(
'foo.png'
, transparent
=
true)
|
显示图形 。
1
|
plt.show()
|
关闭与清除 。
1
2
3
|
plt.cla()
# 清除坐标轴
plt.clf()
# 清除画布
plt.close()
# 关闭窗口
|
以上就是python 数据科学 matplotlib的详细内容,更多关于python 数据科学 matplotlib的资料请关注我其它相关文章! 。
原文链接:https://blog.csdn.net/weixin_44593046/article/details/118529404 。
最后此篇关于Python 数据科学 Matplotlib图库详解的文章就讲到这里了,如果你想了解更多关于Python 数据科学 Matplotlib图库详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!