我正在嘗試對我的資料進行清晰的可視化。這是我的代碼:
width = 0.5
plt.figure(figsize=(10,8))
counts1, bins, bars = plt.hist(data=df1_small, x='fz', bins=np.arange(-5, 5.5, width), color='dodgerblue', alpha=0.3) #rwidth can change the space between bars
#plt.plot(bins[:-1] width/2, counts1, color='#FF6103', linewidth=2)
counts2, bins, bars = plt.hist(data=df2_big, x='fz', bins=np.arange(-5, 5.5, width), color='red', alpha=0.1)
#plt.plot(bins[:-1] width/2, counts2, color='#76EEC6', linewidth=2)
labels = ["small", "big"]
plt.legend(labels)
plt.grid(False)
#plt.savefig("./figure/acce.eps", dpi=300)
plt.savefig("./figure/acce.png", dpi=300)
plt.show()
每當我繪圖時,我發現如果繪圖重疊,顏色就會重疊。
有沒有辦法避免這種重疊?
這是我現在的情節:

我想避免顏色重疊并僅用兩種顏色使其清晰。
謝謝
uj5u.com熱心網友回復:
如果您想避免兩個直方圖之間的任何重疊,您可以在單獨的子圖中繪制兩者。在這種情況下,我認為為軸命名比添加標簽更有意義,但您也可以這樣做。見下文:
# Create two subplots, aligned horizontally
fig, axes = plt.subplots(1, 2, figsize=(10, 8))
width = 0.5
# Plot each histogram in its own axis
counts1, bins, bars = axes[0].hist(data=df1_small, x='fz', bins=np.arange(-5, 5.5, width), color='dodgerblue', alpha=0.3) #rwidth can change the space between bars
counts2, bins, bars = axes[1].hist(data=df2_big, x='fz', bins=np.arange(-5, 5.5, width), color='red', alpha=0.1)
# Add title
axes[0].set_title("small")
axes[1].set_title("small")
# Add labels
axes[0].legend("small")
axes[1].legend("big")
# Need to disable the grid for both axes
axes[0].grid(False)
axes[1].grid(False)
plt.savefig("./figure/acce.png", dpi=300)
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/478898.html
標籤:Python python-3.x matplotlib 阴谋 颜色
上一篇:如何用顏色填充對數螺旋
