如何避免 matplot python 中的颜色重叠?
How can I avoid color overlap in matplot python?
我正在尝试对我的数据进行清晰的可视化。
这是我的代码:
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()
每次绘图时,我发现如果绘图重叠,颜色也会重叠。
有没有办法避免这种重叠?
这是我现在的情节:
我想避免颜色重叠,只用两种颜色就很清楚。
谢谢
如果你想避免两个直方图之间的任何重叠,你可以在单独的子图中绘制它们。在这种情况下,我认为给坐标轴加标题比添加标签更有意义,但您也可以这样做。见下文:
# 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()
我正在尝试对我的数据进行清晰的可视化。 这是我的代码:
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()
每次绘图时,我发现如果绘图重叠,颜色也会重叠。
有没有办法避免这种重叠?
这是我现在的情节:
我想避免颜色重叠,只用两种颜色就很清楚。
谢谢
如果你想避免两个直方图之间的任何重叠,你可以在单独的子图中绘制它们。在这种情况下,我认为给坐标轴加标题比添加标签更有意义,但您也可以这样做。见下文:
# 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()