fig = plt.figure() ax1 = plt.subplot(121) ax2 = plt.subplot(122) ,怎么让其使用相同的x,y刻度
时间: 2023-11-19 16:24:29 浏览: 32
你可以使用`sharex`和`sharey`参数来让两个子图共享相同的x,y刻度。具体做法如下:
```python
fig = plt.figure()
ax1 = plt.subplot(121)
ax2 = plt.subplot(122, sharex=ax1, sharey=ax1) # 使用ax1的x,y刻度
# 在ax1和ax2上分别绘制图形
plt.show()
```
这样,两个子图就会使用相同的x,y刻度了。
相关问题
from sklearn.metrics import ConfusionMatrixDisplay,RocCurveDisplay fig = plt.figure(figsize=(10,5)) ax1 = fig.add_subplot(1,2,1)
抱歉,这不是一个问题,而是一段 Python 代码。它导入了 sklearn.metrics 库中的 ConfusionMatrixDisplay 和 RocCurveDisplay 类,并创建了一个大小为 (10,5) 的图形窗口,然后添加了一个子图 ax1。如果您有关于这段代码的问题,我可以尝试回答。
import matplotlib.pyplot as plt fig=plt.figure() ax1=fig.add_subplot(2,2,1) ax2=fig.add_subplot(2,2,2) ax3=fig.add_subplot(2,2,3) ax1.plot([1.5,2,3.5,-1,1.6])
这段代码的作用是创建一个包含3个子图的画布,并在第一个子图中绘制一个折线图。其中,fig.add_subplot(2,2,1)表示在2行2列的画布中创建第1个子图,ax1.plot([1.5,2,3.5,-1,1.6])表示在第1个子图中绘制一条由给定点构成的折线。需要注意的是,由于没有给定x轴坐标,所以默认使用数据点的下标作为x轴坐标。如果需要自定义x轴坐标,可以将x轴坐标作为第一个参数传给plot()函数。