我如何避免Matplot Python中的颜色重叠?

发布于 2025-01-30 15:22:23 字数 923 浏览 4 评论 0原文

我正在尝试清楚地了解我的数据。 这是我的代码:

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()

每当我绘制时,我都会发现如果绘图重叠,则颜色重叠。

有什么想法避免这种重叠吗?

这是我现在的情节:

我想避免颜色重叠,并仅用两种颜色清晰。

谢谢

I am trying to make a clear visualizatin of my data.
This is my code:

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()

Whenever I plot, I found that color is overlapped if plot is overlapped.

Is there any idea to avoid this overlap?

This is my plot right now:
enter image description here

I want to avoid color overlapping and make it clear with just two colors.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2025-02-06 15:22:23

如果要避免两个直方图之间的任何重叠,则可以在单独的子图中绘制两个。在这种情况下,我认为标题轴比添加标签更有意义,但是您也可以做。见下文:

# 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()

If you want to avoid any overlap between the two histograms, you could plot both in separate subplots. In this case, I think it makes more sense to title the axes than to add labels, but you could do either. See below:

# 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文