我想在同一軸上有一個條形圖和一個線圖。線圖應位于條形上方,顏色應回圈,如連續兩次呼叫 所預期的那樣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
uj5u.com熱心網友回復:
這個答案是后續
當然,您可以明確指定每組不同條形或每條線的顏色,但是如果要在相同的軸中混合顏色(以及其他可選屬性)遵循單個共享回圈的條形和線,則必須itertools.cycle通過呼叫軸實體化一個合適的物件prop_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()

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/357682.html
