fig, ax1 = plt.subplots() color = 'tab:red' ax1.set_xlabel('Index') ax1.set_ylabel('MEDV', color=color) ax1.plot(data['MEDV'], color=color) ax1.tick_params(axis='y', labelcolor=color) ax2 = ax1.twinx() color = 'tab:blue' ax2.set_ylabel('CRIM', color=color) ax2.plot(data['CRIM'], color=color) ax2.tick_params(axis='y', labelcolor=color) fig.tight_layout() plt.title('Line plot of CRIM and MEDV') plt.show() # 绘制CRIM和MEDV的散点图 plt.scatter(data.index, data['CRIM'], s=5) plt.scatter(data.index, data['MEDV'], s=5) plt.legend() plt.ylabel('Value') plt.xlabel('Index') plt.show()
时间: 2024-01-01 17:07:04 浏览: 32
这是一个使用matplotlib库在Python中绘制线图和散点图的代码示例。第一段代码绘制了'MEDV'和'CRIM'之间的线图。通过调用subplots函数创建一个包含两个子图的图表,其中ax1是左侧子图,ax2是右侧子图。在左侧子图中,'MEDV'被设置为纵坐标,'CRIM'被设置为右侧子图的纵坐标。在右侧子图中,'CRIM'被设置为纵坐标。通过设置tick_params函数中的labelcolor参数,使得左右两个子图的刻度标签颜色不同。最后,使用tight_layout函数调整图表布局,并使用show函数显示图表。
第二段代码绘制了'MEDV'和'CRIM'之间的散点图。使用scatter函数绘制散点图,其中x轴被设置为数据的索引,y轴被设置为'MEDV'和'CRIM'。使用legend函数添加图例,使得可以区分'MEDV'和'CRIM'的散点图。最后,使用ylabel和xlabel函数设置横纵坐标的标签,并使用show函数显示图表。
相关问题
fig, ax1 = plt.subplots(
fig, ax1 = plt.subplots()是用于创建一个包含一个子图的Figure对象和一个Axes对象的函数[^1]。其中,Figure对象代表整个图形窗口,而Axes对象则代表一个具体的绘图区域。
下面是一个示例,演示了如何使用fig, ax1 = plt.subplots()创建一个包含两个子图的图形窗口,并在每个子图中绘制不同的数据[^2]:
```python
import matplotlib.pyplot as plt
import numpy as np
# 创建数据
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
# 创建图形窗口和子图
fig, ax1 = plt.subplots()
# 在第一个子图中绘制数据
ax1.plot(x, y1, 'b', lw=1.5, label='1st')
ax1.plot(x, y1, 'ro')
ax1.grid(True)
ax1.legend(loc=0)
ax1.set_xlabel('index')
ax1.set_ylabel('value 1st')
ax1.set_title('A Simplot plot')
# 创建第二个子图
ax2 = ax1.twinx()
# 在第二个子图中绘制数据
ax2.plot(x, y2, 'g', lw=1.5, label='2nd')
ax2.plot(x, y2, 'ro')
ax2.legend(loc=0)
ax2.set_ylabel('value 2nd')
# 显示图形
plt.show()
```
这段代码会创建一个包含两个子图的图形窗口,其中第一个子图绘制了y1的数据,第二个子图绘制了y2的数据。每个子图都有自己的y轴,但共享x轴。在绘制完数据后,使用plt.show()函数显示图形。
调节fig, ax1 = plt.subplots()大小
可以通过在subplots()函数中设置figsize参数来调整figure的大小,如下所示:
```python
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots(figsize=(8, 6)) # 宽度为8,高度为6
ax1.plot(x, y)
plt.show()
```
其中,参数figsize的单位是英寸,可以根据需要进行调整。这样设置之后,生成的figure的大小就会变为您指定的大小。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)