可视化之用matplotlib.pyplot画图

1. 折线图

官网

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

1.1 参数解释

x, y: array-like or scalar(标量)

x可选,默认是range(len(y));y可以是二维数组,此时会在同一个坐标系里出现多条折线;

fmt:str,optional

控制线条格式的参数,比如线条的颜色、形状等。
fmt = '[marker][line][color]'

常见的marker参数:
详见 matplotlib.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参数:

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

常见的Colors参数:

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

**kwargsLine2D: properties, optional

也能通过这类参数来控制线条。
比如marker控制形状,c控制颜色

1.2 举例

不传入x参数

plt.plot([1, 4, 9, 16], marker='o', c='r')
plt.show()

在这里插入图片描述
画多条折线

x1 = [1,2,3]
y1=[1,3,5]
x2=[1,2,3]
y2=[2,4,6]
plt.plot(x1, y1, 'g^', x2, y2, 'g-') # 此时不能通过marker,c来控制线条的形状和颜色

在这里插入图片描述

2. 散点图

官网

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)

2.1 参数解释

x, y: float or array-like, shape (n, )
# 数据点的位置
s: float or array-like, shape (n, ), optional
# 数据点的大小
c: array-like or list of colors or color, optional
# 数据点的颜色
marker: MarkerStyle, default: rcParams["scatter.marker"] (default: 'o')
# 数据点的形状
alpha: float, default: None
# 数据点的透明程度,0表示完全透明,1表示不透明
linewidths: float or array-like, default: rcParams["lines.linewidth"] (default: 1.5)
# 数据点的线宽
edgecolors: {'face', 'none', None} or color or sequence of color, default: rcParams["scatter.edgecolors"] (default: 'face')
# 数据点的线的颜色

2.2 例子

设置大小、颜色和透明度

# Fixing random state for reproducibility
np.random.seed(19680801)

N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = (30 * np.random.rand(N))**2  # 0 to 15 point radii

plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()

在这里插入图片描述
设置线宽

x = [1, 2, 3]
y = [1, 4, 9]
plt.scatter(x, y, c='r', alpha=0.5, linewidths=15, edgecolors=None)

在这里插入图片描述

### 回答1: 如果你在PyCharm中无法使用matplotlib.pyplot,可能是因为你没有安装matplotlib库。你可以通过在PyCharm的终端中运行以下命令来安装matplotlib: ``` pip install matplotlib ``` 如果你已经安装了matplotlib库,但仍然无法使用matplotlib.pyplot,可能是因为你没有正确导入它。你可以在代码中添加以下行来导入matplotlib.pyplot: ``` import matplotlib.pyplot as plt ``` 然后你就可以使用plt来调用matplotlib.pyplot了。 ### 回答2: ### 回答3: PyCharm 是一个很流行的 Python 编辑器,它的用户群体非常广泛,也包括很多使用 Matplotlib 包的 Python 开发者。但是,有些时候,PyCharm 编辑器中可能找不到 Matplotlibpyplot 模块,这就需要我们手动安装才能使用。下面我会具体阐述 pycharm 缺少 matplotlib.pyplot 的原因以及解决方法。 一、pycharm 缺失 matplotlib.pyplot 是因为matplotlib未安装 MatplotlibPython 可视化库,常用于绘制各种图表,是数据分析、机器学习、科学计算等领域的必备工具之一。PyCharm 本身并不自带 Matplotlib 库,因此如果在 PyCharm 中缺少 Matplotlibpyplot 模块,需要自己手动安装 Matplotlib 库。 二、pycharm 用户需要安装 Matplotlib 库 要安装 Matplotlib 库,可以选择使用 pip 或 conda 命令,在命令行中输入以下命令: 1. 使用pip安装 Matplotlib: ```python pip install matplotlib ``` 2. 使用conda安装 Matplotlib: ```python conda install matplotlib ``` 三、配置 PyCharm 编辑器让其使用 matplotlib.pyplot 模块 在安装了 Matplotlib 库之后,可能还需要配置一下 PyCharm 的项目解释器,才能在编写代码实现画图功能。具体步骤如下: 1. 在 PyCharm 中打开需要使用 Matplotlib 的项目。(新建一个项目也可以) 2. 在 PyCharm 菜单栏中依次点击 File -> Settings -> Project: Project_Name -> Project Interpreter。 3. 在 Project Interpreter 面板中,如果能够看到 Matplotlib,说明已经安装成功了。如果找不到 Matplotlib,请点击右上角的“+”,在搜索栏中输入 Matplotlib,然后选择要安装的版本进行安装。 4. 在 Installation Tab 中,将 Show paths for Selected Interpreter 选项打开。 5. 在 Paths Tab 中,可以看到当前项目使用的 Python 解释器的路径。在界面的上方可以找到“+”,点击后可以添加 Python 环境。如果没有选择的解释器,可以选择 New Environment 创建一个。 6. 配置完成后,重启 PyCharm。 7. 最后在代码中导入 matplotlib.pyplot 模块即可,示例代码如下: ```python import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.show() ``` 综上所述,如果在 PyCharm 中使用时缺少 Matplotlibpyplot 模块,请先自行安装 Matplotlib 库,并配置好 PyCharm 的项目解释器。这样就能愉快地使用 Matplotlib 了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值