【发布时间】:2021-12-23 18:11:31
【问题描述】:
我想在同一轴上绘制条形图和折线图。
线图应该在条形图上方,颜色应该像预期的那样循环,连续两次调用plt.plot()。
import matplotlib.pyplot as plt
import math
if __name__ == "__main__":
xs = range(1,10)
data = [0.3, 0.2, 0.1, 0.05, 0.05, 0.11, 0.09, 0.05, 0.07]
benfords = [math.log10(float(d + 1)/d) for d in xs]
plt.bar(xs, data, ec='black', label='Data1')
plt.plot(xs, benfords, linestyle='--', marker='o', label="Benford's")
plt.xlabel('Digit')
plt.ylabel('Frequency')
plt.legend()
plt.show()
此代码生成:
同时添加 zorder=1 和 zorder=2(或任意两个连续数字,如 3 和 4 等)也无济于事。
当bar 替换为plot 时,两条不同颜色的线图按预期出现。
Python 版本:2.7.8 Matplotlib 版本:2.2.5 操作系统:Windows 10 x64
【问题讨论】:
-
它是匹配的,因为它是相同的颜色。将“color = 'red'”添加到线图线,你会看到不同
-
对不起,看来他们的zorder确实是对的!谢谢)但是颜色呢?为什么不通过
prop_cycler进一步循环? -
“为什么不通过
prop_cycler进一步循环?” 因为条和线不共享一个prop_cycle,所以每个艺术家都使用自己的...查看axes.bar和axes.plot的源代码。
标签: python-2.7 matplotlib