Python Matplotlib:绘制具有非透明边缘的透明直方图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28398200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Matplotlib: plotting transparent histogram with non transparent edge
提问by Argentina
I am plotting a histogram, and I have three datasets which I want to plot together, each one with different colours and linetype (dashed, dotted, etc). I am also giving some transparency, in order to see the overlapping bars.
我正在绘制直方图,我有三个数据集要一起绘制,每个数据集都有不同的颜色和线型(虚线、虚线等)。我还提供了一些透明度,以便看到重叠的条。
The point is that I would like the edge of each bar not to become transparent as the inner part does. Here is an example:
关键是我希望每个条的边缘不要像内部一样变得透明。下面是一个例子:
import matplotlib.pyplot as plt
import numpy as np
x = np.random.random(20)
y =np.random.random(20)
z= np.random.random(20)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist(x, bins=np.arange(0, 1, 0.1), ls='dashed', alpha = 0.5, lw=3, color= 'b')
ax.hist(y, bins=np.arange(0, 1, 0.1), ls='dotted', alpha = 0.5, lw=3, color= 'r')
ax.hist(z, bins=np.arange(0, 1, 0.1), alpha = 0.5, lw=3, color= 'k')
ax.set_xlim(-0.5, 1.5)
ax.set_ylim(0, 7)
plt.show()
采纳答案by ali_m
plt.hist
accepts additional keyword arguments that are passed to the constructor for matplotlib.patches.Patch
. In particular you can pass an fc=
argument which lets you set the patch facecolor using an (R, G, B, A)
tuple when you create the histograms. Changing the alpha value of the facecolor does not affect the transparency of the edges:
plt.hist
接受传递给 的构造函数的其他关键字参数matplotlib.patches.Patch
。特别是您可以传递一个fc=
参数,让您(R, G, B, A)
在创建直方图时使用元组设置补丁面颜色。更改 facecolor 的 alpha 值不会影响边缘的透明度:
ax.hist(x, bins=np.arange(0, 1, 0.1), ls='dashed', lw=3, fc=(0, 0, 1, 0.5))
ax.hist(y, bins=np.arange(0, 1, 0.1), ls='dotted', lw=3, fc=(1, 0, 0, 0.5))
ax.hist(z, bins=np.arange(0, 1, 0.1), lw=3, fc=(0, 0, 0, 0.5))
回答by ljetibo
I think perhaps the dumbest solution is the easiest and best? Just replot the histograms, without inner color and without alpha. That should replot just the histogram lines.
我认为也许最愚蠢的解决方案是最简单和最好的?只需重新绘制直方图,没有内部颜色和 alpha。那应该只重新绘制直方图线。
Additionally you might want to eliminate the chance of having the lines/dashes/dots not overlap by removing them completely from the first histogram plot
此外,您可能希望通过从第一个直方图中完全删除线条/虚线/点来消除它们不重叠的机会
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
x = np.random.random(20)
y = np.random.random(20)
z = np.random.random(20)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist(x, bins=np.arange(0, 1, 0.1), edgecolor='None', alpha = 0.5, color= 'b')
ax.hist(y, bins=np.arange(0, 1, 0.1), edgecolor='None', alpha = 0.5, color= 'r')
ax.hist(z, bins=np.arange(0, 1, 0.1), edgecolor="None", alpha = 0.5, color= 'k')
ax.hist(x, bins=np.arange(0, 1, 0.1), ls='dashed', lw=3, facecolor="None")
ax.hist(y, bins=np.arange(0, 1, 0.1), ls='dotted', lw=3, facecolor="None")
ax.hist(z, bins=np.arange(0, 1, 0.1), lw=3, facecolor="None")
plt.show()
without the second orders the graph should look like your figure without any borders. With the bottom 3 histogram commands borders are added to produce the image bellow. Works on Python3.4 Win7
如果没有第二个订单,图表应该看起来像你的图,没有任何边界。使用底部 3 个直方图命令添加边框以生成下面的图像。适用于 Python3.4 Win7
[cut because apparently I don't have enough rep to post pictures]
[删掉是因为显然我没有足够的代表来张贴图片]
回答by bluenote10
I needed a solution which did not require me to set the color explicitly, i.e., I wanted to still use the default color cycle. The following solution builds on @ljetibo's idea to draw the histogram twice using @ali_m's idea to extract the state of the color cycle:
我需要一个不需要我明确设置颜色的解决方案,即我想仍然使用默认颜色循环。以下解决方案建立在@ljetibo 的想法之上,使用@ali_m 的想法提取颜色循环的状态两次绘制直方图:
# increment and get the "props" cycle (and extract the color)
color = next(ax._get_lines.prop_cycler)["color"]
# 1. draw: inner area with alpha
ax.hist(data, color=color, alpha=0.3)
# 2. draw: only edges with full color
ax.hist(data, color=color, edgecolor=color, fc="None", lw=1)
Note that specifying color=color
in addition to edgecolor
seems to be necessary. Otherwise the hist
call itself will increment the color cycle again.
请注意,color=color
除了指定edgecolor
似乎是必要的。否则,hist
调用本身将再次增加颜色循环。