向pyplot直方图中的条添加白色分隔符

2024-10-04 09:25:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在matplotlib中的直方图条中添加白色的“中断”,这样网格线就可以在绘图中继续显示,而不会在背景中造成干扰或繁忙。我想看起来像下面这样:

我想要的-https://imgur.com/IUr3tz6

下面是要使用的测试直方图:

vals = np.random.randn(1000)
f = plt.figure(figsize=[4,4])
ax = f.add_subplot(111)
ax.hist(vals, bins=20, normed=True)

Tags: httpscom绘图matplotlibnprandomax直方图
1条回答
网友
1楼 · 发布于 2024-10-04 09:25:45

如果使网格线与背景颜色相同,并独立管理水平和垂直网格线,则可以获得所需的“视觉打断”。你知道吗

import numpy as np
from matplotlib import pyplot as plt
vals = np.random.randn(1000)
f = plt.figure(figsize=\[4,4\])
ax = f.add_subplot(111)
ax.hist(vals, bins=20, normed=True, 
        )
ax.yaxis.grid(which="major", color='white', linestyle='-', linewidth=0.5)
ax.xaxis.grid(which="major", color='white', linestyle='-', linewidth=4)

plt.show()]

enter image description here

相关问题 更多 >