python 只显示x轴坐标标签,不显示x轴线
时间: 2024-06-09 21:07:36 浏览: 100
可以使用 `matplotlib` 库来实现只显示 x 轴坐标标签但不显示 x 轴线的效果。具体实现步骤如下:
1. 导入 `matplotlib` 库:
```python
import matplotlib.pyplot as plt
```
2. 绘制图形,并设置 x 轴标签和数据:
```python
x = [1, 2, 3, 4, 5]
y = [10, 8, 6, 4, 2]
plt.plot(x, y)
plt.xlabel('x-axis')
```
3. 隐藏 x 轴线,以及 x 轴刻度和标签之间的连线:
```python
plt.gca().spines['bottom'].set_visible(False)
plt.gca().xaxis.set_ticks_position('none')
plt.gca().xaxis.set_ticklabels(x)
```
完整代码如下:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 8, 6, 4, 2]
plt.plot(x, y)
plt.xlabel('x-axis')
plt.gca().spines['bottom'].set_visible(False)
plt.gca().xaxis.set_ticks_position('none')
plt.gca().xaxis.set_ticklabels(x)
plt.show()
```
运行结果如下图所示:

阅读全文
相关推荐


















