相同颜色的条形图和线条图,线条在条形图后面
Bar and line plot of the same color and line is behind the bars
我想在同一轴上绘制条形图和线图。
线图应该在条形图上方,颜色应该像连续两次调用 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
OS: Windows 10 x64
此回答是 by the OP
的后续回答
Why doesn't it cycle further by the prop_cycler?
原因是,即使 prop_cycle
被松散地定义为 axes
的 属性(您使用 plt.rcParams['axes.prop_cycle']
检索它),实际上axes.bar
方法和 axes.plot
方法,在初始化期间,实例化循环仪的单独副本,因此默认情况下(如 Matplotlib 3.4),条和线都从蓝色开始
In [15]: fig, ax = plt.subplots()
...: ax.bar((1,2,3), (5,4,6), label='b1')
...: ax.bar((1,2,3), (2,3,2), label='b2')
...: ax.plot((1,2,3),(9,6,8), label='l1')
...: ax.plot((1,2,3),(6,8,7), label='l2')
...: plt.legend()
...: plt.show()
当然,您可以明确指定每组不同的条形图或每条线的颜色,但是如果您想在相同的轴中混合颜色(以及可选的其他属性)服从单一的条形图和线条,共享循环,您必须通过 调用 轴 prop_cycle
实例化适当的 itertools.cycle
对象
In [16]: fig, ax = plt.subplots()
...: pc = plt.rcParams['axes.prop_cycle']() # we CALL the prop_cycle
...: npc = lambda: next(pc)
...: ax.bar((1,2,3), (5,4,6), label='b1', **npc())
...: ax.bar((1,2,3), (2,3,2), label='b2', **npc())
...: ax.plot((1,2,3),(9,6,8), label='l1', **npc())
...: ax.plot((1,2,3),(6,8,7), label='l2', **npc())
...: plt.legend()
...: plt.show()
我想在同一轴上绘制条形图和线图。
线图应该在条形图上方,颜色应该像连续两次调用 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 OS: Windows 10 x64
此回答是
Why doesn't it cycle further by the prop_cycler?
原因是,即使 prop_cycle
被松散地定义为 axes
的 属性(您使用 plt.rcParams['axes.prop_cycle']
检索它),实际上axes.bar
方法和 axes.plot
方法,在初始化期间,实例化循环仪的单独副本,因此默认情况下(如 Matplotlib 3.4),条和线都从蓝色开始
In [15]: fig, ax = plt.subplots()
...: ax.bar((1,2,3), (5,4,6), label='b1')
...: ax.bar((1,2,3), (2,3,2), label='b2')
...: ax.plot((1,2,3),(9,6,8), label='l1')
...: ax.plot((1,2,3),(6,8,7), label='l2')
...: plt.legend()
...: plt.show()
当然,您可以明确指定每组不同的条形图或每条线的颜色,但是如果您想在相同的轴中混合颜色(以及可选的其他属性)服从单一的条形图和线条,共享循环,您必须通过 调用 轴 prop_cycle
itertools.cycle
对象
In [16]: fig, ax = plt.subplots()
...: pc = plt.rcParams['axes.prop_cycle']() # we CALL the prop_cycle
...: npc = lambda: next(pc)
...: ax.bar((1,2,3), (5,4,6), label='b1', **npc())
...: ax.bar((1,2,3), (2,3,2), label='b2', **npc())
...: ax.plot((1,2,3),(9,6,8), label='l1', **npc())
...: ax.plot((1,2,3),(6,8,7), label='l2', **npc())
...: plt.legend()
...: plt.show()