我需要生成一个bar plot 但没有底部边缘.下面是一个示例输出和代码:

enter image description here

import numpy as np
import matplotlib.pyplot as plt
x = np.array([0.05, 0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85, 0.95])
y = np.array([0.0801 , 0.08062, 0.08136, 0.08728, 0.0836 , 0.08862, 0.09642,
              0.12204, 0.0722 , 0.20776])
plt.bar(x, y, width=0.1, alpha=0.5, align='center',
        fill=False, ls=':', lw=5, edgecolor='r')
plt.show()

我想把栏杆的底边藏起来.这能做到吗?

推荐答案

pyplot.bar(docs here)有一个名为bottom的参数,用于设置开始绘制条形图的y值—默认值为0,正如您所期望的.我们可以用它来调整我们的wine 吧的底部"边缘",然后设置你的轴的ylim,以有效地裁剪出来.然而,这里需要注意的一点是,我们需要在y个值中补偿这一点,因为这些值实际上对应于height条.

你的代码可以变成这样:

import numpy as np
import matplotlib.pyplot as plt


x = np.array([0.05, 0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85, 0.95])
y = np.array([0.0801 , 0.08062, 0.08136, 0.08728, 0.0836 , 0.08862, 0.09642,
              0.12204, 0.0722 , 0.20776])

plt.bar(x, y+1, width=0.1, alpha=0.5, bottom=-1, align='center',
        fill=False, ls=':', lw=5, edgecolor='r')

# "crop" the bottom out
plt.gca().set_ylim(bottom=0)

plt.show()

注意到,我们设置bottom=-1,与y+1镜像(实际上是height=y+1),然后设置ylim.这给了我们没有"底边"的wine 吧:

bar chart with bottom edges cropped out

Python相关问答推荐

线性模型PanelOLS和statmodels OLS之间的区别

Python json.转储包含一些UTF-8字符的二元组,要么失败,要么转换它们.我希望编码字符按原样保留

Pandas 有条件轮班操作

ModuleNotFound错误:没有名为flags.State的模块; flags不是包

. str.替换pandas.series的方法未按预期工作

为什么符号没有按顺序添加?

在vscode上使用Python虚拟环境时((env))

Pandas DataFrame中行之间的差异

如何在Python中找到线性依赖mod 2

Python列表不会在条件while循环中正确随机化'

在pandas数据框中计算相对体积比指标,并添加指标值作为新列

旋转多边形而不改变内部空间关系

matplotlib图中的复杂箭头形状

在Admin中显示从ManyToMany通过模型的筛选结果

从旋转的DF查询非NaN值

如何使用正则表达式修改toml文件中指定字段中的参数值

裁剪数字.nd数组引发-ValueError:无法将空图像写入JPEG

用来自另一个数据框的列特定标量划分Polars数据框中的每一列,

多索引数据帧到标准索引DF

将鼠标悬停在海运`pairplot`的批注/高亮显示上