如何使用Matplotlib在Python中绘制无Y轴的一维点图?
使用Matplotlib实现目标图形并隐藏Y轴
完全可以用Matplotlib实现你要的效果,下面是一个示例代码,模拟图中的横向条形类图形并隐藏Y轴:
import matplotlib.pyplot as plt # 准备示例数据 categories = ["类别1", "类别2", "类别3"] values = [75, 90, 60] # 创建画布和轴对象 fig, ax = plt.subplots(figsize=(8, 2)) # 绘制横向条形图(对应图中样式) bars = ax.barh(categories, values, color="#4a90e2") # 隐藏Y轴 ax.yaxis.set_visible(False) # 可选:调整其他样式,比如隐藏顶部和右侧边框,添加数据标签 ax.spines[['top', 'right']].set_visible(False) for bar in bars: width = bar.get_width() ax.text(width + 1, bar.get_y() + bar.get_height()/2, f'{width}%', va='center') # 调整布局,避免标签被截断 plt.tight_layout() plt.show()
如果你需要更贴合图中具体样式(比如不同颜色、进度条样式),可以修改color参数或者改用ax.fill_between等函数实现,核心隐藏Y轴的逻辑都是ax.yaxis.set_visible(False)。
内容的提问来源于stack exchange,提问作者YYY
相关产品推荐
相关产品推荐

