python - 在 matplotlib 上绘制条形图

标签 python matplotlib

如何绘制水平条形图,其值位于条形末尾,类似于 this

我试过了

plt.barh(inc.index,inc)
plt.yticks(inc.index)
plt.xticks(inc);
plt.xlabel("Order Count")
plt.ylabel("Date")

Bar chart

最佳答案

答案可以在这里找到: How to display the value of the bar on each bar with pyplot.barh()?

只需按照 cphlewis 所说添加 for 循环即可:

for i, v in enumerate(inc):
    ax.text(v + 3, i + .25, str(v), color='blue', fontweight='bold')
plt.show()

这是我针对您的情况尝试的代码:

import matplotlib.pyplot as plt
import numpy as np
inc = [12, 25, 50, 65, 40, 45]
index = ["2019-10-31", "2019-10-30", "2019-10-29", "2019-10-28", "2019-10-27", "2019-10-26"]

fig, ax = plt.subplots()
ax.barh(index,inc, color='black')
plt.yticks(index)
plt.xticks(inc);
plt.xlabel("Order Count")
plt.ylabel("Date")

# Set xticks
plt.xticks(np.arange(0, max(inc)+15, step=10))

# Loop for showing inc numbers in the end of bar
for i, v in enumerate(inc):
    ax.text(v + 1, i, str(v), color='black', fontweight='bold')
plt.show()

情节如下: enter image description here

关于python - 在 matplotlib 上绘制条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58678862/

相关文章:

python - 为 pdf 后端设置 spines 的 capstyle

python - 如何正确旋转 ndarray(图像)?

python - Pandas :在同一张图上绘制两个数据框时出现异常

python - 读取Python中一行中的第二个 float

python - CSRF验证失败。请求被中止。 (禁止(403)) Django

pandas - Seaborn:按升序排序计数图

python - 如何将条形图值更改为百分比 (Matplotlib)

python - pandas 应用并分配给多列

python - Azure 语音到文本 0x38 (SPXERR_AUDIO_SYS_LIBRARY_NOT_FOUND)

python - 如何修复 PlotlyRequestError?