2d图导入plt.figure
时间: 2023-10-21 22:00:39 浏览: 30
要将2D图导入到`plt.figure`中,需要使用`matplotlib.pyplot`模块中的`imshow`函数。以下是一个简单的示例:
```python
import matplotlib.pyplot as plt
import numpy as np
# 生成一个随机的2D数组
arr = np.random.rand(10, 10)
# 绘制2D图像
plt.imshow(arr)
# 显示图像
plt.show()
```
在这个示例中,我们使用`numpy`模块生成一个10x10的随机2D数组,然后使用`imshow`函数将它绘制出来。最后,我们使用`show`函数显示图像。
你可以根据需要调整数组的大小、颜色映射等参数来定制你的2D图像。
相关问题
import matplotlib.colors as mcolors colors=list(mcolors.TABLEAU_COLORS.keys()) # 定义可视化函数 def vis_result_2D(df,label): fig = plt.figure(figsize=(16,6)) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False ax = fig.add_subplot(121) for i in df['label'].unique(): temp_df = df[df['label']==i] ax.scatter(temp_df['WindSpeed'],temp_df['Power'],s=0.3,color=mcolors.TABLEAU_COLORS[colors[i]],label=i) plt.legend() plt.title("各类异常的分布") plt.grid(True) ax = fig.add_subplot(122) df = df[df['label']==0] ax.scatter(df['WindSpeed'],df['Power'],s=0.3) plt.title("清洗后的结果") plt.grid(True) plt.show()详细解释
这段代码主要用于可视化数据集的聚类结果,具体解释如下:
1. 导入 matplotlib.colors 模块,并将所有 TABLEAU_COLORS 的 key 存储在 colors 列表中。
2. 定义了一个名为 vis_result_2D 的可视化函数,该函数接受两个参数,一个是数据集 df,另一个是聚类的标签 label。
3. 创建一个大小为 16x6 的图形窗口,并设置字体和负轴。
4. 在左边的子图中,循环遍历聚类标签的唯一值,对于每个标签,将数据集中标签值等于该标签值的数据筛选出来,并使用 scatter 函数将其可视化到图中,每个标签使用不同的颜色。同时,添加图例、标题和网格线。
5. 在右边的子图中,筛选出标签值等于 0 的数据,使用 scatter 函数将其可视化到图中,不添加图例,添加标题和网格线。
6. 显示图形窗口。
ax = plt.axes(position='3d')运行报错TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' 请告诉我这个报错是什么意思,并且告诉我怎么解决
这个报错是由于在使用Matplotlib的plt.axes()函数时传入了错误的参数而引发的。具体来说,position参数应该是一个四元组,表示绘图的位置和大小。而在你的代码中,position参数应该是用于2D绘图的,而你却将其传入了plt.axes()函数中,导致了类型错误。
要解决这个问题,你可以将position参数从plt.axes()函数中移除,或者将其替换为适用于3D绘图的参数。如果你想要绘制3D图形,可以尝试使用mpl_toolkits.mplot3d模块中的Axes3D类来创建3D坐标轴对象,示例如下:
```python
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
```
这样就可以创建一个3D坐标轴对象,并且不会再出现上述报错。请注意,你可能还需要导入其他需要的模块或库来完成你的3D绘图任务。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)