使用可视化库matplotlib绘图时,plt.show()过后只出现Figure size 640x480 with 1 Axes而没有生成图片
时间: 2024-04-29 10:26:30 浏览: 12
这可能是由于在生成图像之前,缺少对绘图函数的调用。在调用 `plt.show()` 后,图像已经被绘制并显示。要将图像保存到文件中,请使用 `plt.savefig()` 函数在调用 `plt.show()` 之前将图像保存到磁盘上。例如:
```
import matplotlib.pyplot as plt
# 绘制图像
plt.plot([1, 2, 3, 4])
# 保存为PNG格式
plt.savefig("test.png")
# 显示图像
plt.show()
```
这将显示生成的图像并保存为“test.png”文件。
相关问题
(150, 4) <Figure size 640x480 with 1 Axes> <Figure size 640x480 with 1 Axes>
根据您提供的信息,我猜测您可能已经运行了一个程序,输出了`(150, 4)`和`<Figure size 640x480 with 1 Axes>`两行结果。其中`(150, 4)`表示数据集的大小为150行4列,`<Figure size 640x480 with 1 Axes>`则表示绘图窗口的大小。
如果您想要进一步分析数据集或绘制更多的图表,可以使用适当的Python库和函数进行操作。例如,使用`numpy`库可以对数据进行数值计算和分析,使用`pandas`库可以对数据进行处理和可视化,使用`matplotlib`库可以绘制各种类型的图表。以下是一个简单示例,展示如何使用`pandas`库读取iris数据集,并绘制散点图:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取iris数据集
iris_data = pd.read_csv('iris.csv')
# 绘制散点图
plt.scatter(iris_data['sepal_length'], iris_data['sepal_width'], c=iris_data['species'])
plt.xlabel('Sepal length (cm)')
plt.ylabel('Sepal width (cm)')
plt.title('Iris dataset')
plt.show()
```
以上代码中,使用`pd.read_csv`函数读取csv格式的iris数据集,并将其保存在`iris_data`变量中。然后,使用`plt.scatter`函数绘制散点图,其中`iris_data['sepal_length']`和`iris_data['sepal_width']`分别表示数据集中的萼片长度和宽度,`c=iris_data['species']`指定了数据点的颜色,根据花的种类进行着色。最后使用`plt.xlabel`、`plt.ylabel`和`plt.title`函数设置坐标轴标签和图表标题。
希望这个示例对您有所帮助!
plot画图多个变量 python_Python可视化库Matplotlib绘图入门详解
Matplotlib 是 Python 中最常用的可视化库之一,它提供了大量的功能和选项,可以绘制各种类型的图形。在 Matplotlib 中,绘图的基本单位是 Figure 和 Axes,其中 Figure 是整个图形的窗口或容器,而 Axes 则是实际绘图的区域。
下面我们将演示如何使用 Matplotlib 绘制多个变量的图形:
首先,我们需要导入 Matplotlib 库:
```python
import matplotlib.pyplot as plt
```
接下来,我们生成一些数据,用于绘图:
```python
import numpy as np
x = np.linspace(0, 10, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
```
上述代码生成了一个 0 到 10 之间的等间距数组 x,以及三个函数 y1、y2 和 y3,分别表示 sin(x)、cos(x) 和 tan(x)。
接下来,我们可以使用 Matplotlib 绘制这些函数的图形:
```python
fig, ax = plt.subplots()
ax.plot(x, y1, label='sin(x)')
ax.plot(x, y2, label='cos(x)')
ax.plot(x, y3, label='tan(x)')
ax.legend()
plt.show()
```
上述代码中,我们首先使用 subplots() 函数创建一个 Figure 对象和一个 Axes 对象。然后,我们使用 plot() 函数在 Axes 对象上绘制三个函数的图形,并使用 label 参数为每个函数添加一个标签。最后,我们调用 legend() 函数显示图例,并调用 show() 函数显示图形。
运行上述代码,可以得到如下图形:
![image.png](attachment:image.png)
上图展示了 sin(x)、cos(x) 和 tan(x) 三个函数在 0 到 10 范围内的图形。我们可以看到,通过 Matplotlib,我们可以轻松地绘制多个变量的图形,并对图形进行进一步的定制。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)