Matplotlib:如何在不透明的线边缘上有透明的框图面? [英] Matplotlib: how to have a transparent box plot face while a non-transparent line edge?

查看:83
本文介绍了Matplotlib:如何在不透明的线边缘上有透明的框图面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个绘制自定义箱形图的功能。我希望盒子的表面平整,透明,并希望在每个盒子表面周围有一条细实线(不透明)。



我一直在尝试通过调用 matplotlib.pyplot.boxplot 后设置框的参数来进行此操作下面。



当我查看要显示的绘图时,它会显示我想要的内容。但是,当我使用 matplotlib.pyplot.savefig( output.pdf)创建pdf时,该行并不牢固,它也是透明的。当我放大pdf文件时,此透明度非常明显。



有人能在保持箱形图面透明性的同时使线不透明吗?



预先感谢。

  from matplotlib import pyplot as plt 
import numpy as np

data = {}
data ['a'] = np.arange(12)+1
data ['b'] = np.arange(14)+1
data ['c'] = np .arange(8)+1

cols = ['红色','蓝色','绿色']

控件= ['trt_a','trt_b',' trt_c']

图,ax = plt.subplots()

boxplot_dict = ax.boxplot([[a],'b'中x的数据[x] ,'c']],\
position = [1,1.5,2],标签=控件,\
patch_artist = True,宽度= 0.25)

b,c in zip(boxplot_dict ['boxes'],cols):
b.set_alpha(0.6)
b.set_edgecolor(c)#或尝试'black'
b.set_facecolor( c)
b.set_linewidth(1)

ax.set_ylim([0,16])

plt.savefig( test_boxplot.pdf)
plt.close()

我尝试了对直方图给出的建议


I'm building a function to draw custom box plots. I'd like the face of the boxes to be plain with slight transparency and I'd like a thin solid (non-transparent) line around each box face.

I've been attempting this by setting the parameters of the boxes after calling matplotlib.pyplot.boxplot as shown below.

When I view the plot to screen it shows what I would like. However, when I create a pdf using, say, matplotlib.pyplot.savefig("output.pdf") the line is not solid, it's transparent also. When I zoom into the pdf file this transparency is noticable.

Does anyone have an idea of how to make the line non-transparent while keeping the transparency of the box plot face?

Thanks in advance.

from matplotlib import pyplot as plt
import numpy as np

data = {}
data['a'] = np.arange(12)+1
data['b'] = np.arange(14)+1
data['c'] = np.arange(8)+1

cols = ['red', 'blue', 'green']

controls = ['trt_a', 'trt_b', 'trt_c']

fig, ax = plt.subplots()

boxplot_dict = ax.boxplot([data[x] for x in ['a', 'b', 'c']], \
    positions = [1, 1.5, 2], labels = controls, \
    patch_artist = True, widths = 0.25)

for b, c in zip(boxplot_dict['boxes'], cols):
    b.set_alpha(0.6)
    b.set_edgecolor(c) # or try 'black'
    b.set_facecolor(c)
    b.set_linewidth(1)

ax.set_ylim([0,16])

plt.savefig("test_boxplot.pdf")
plt.close()

I've tried the suggestion that was given for histograms here, by setting the colors to include the alpha level [(0, 0, 1, 0.6), (1, 0, 0, 0.6), (0, 1, 0, 0.6)] and that didn't seem to work for box plots.

解决方案

set_alpha changes both face color and edge color, to avoid that, you may want to consider passing RGBA values directly to face color:

#cols = ['red', 'blue', 'green']
cols = [[1,0,0,0.5],
        [0,1,0,0.5],
        [0,0,1,0.5]]

controls = ['trt_a', 'trt_b', 'trt_c']

fig, ax = plt.subplots()

boxplot_dict = ax.boxplot([data[x] for x in ['a', 'b', 'c']], \
    positions = [1, 1.5, 2], labels = controls, \
    patch_artist = True, widths = 0.25)

for b, c in zip(boxplot_dict['boxes'], cols):
    #b.set_alpha(0.6)
    b.set_edgecolor('k') # or try 'black'
    b.set_facecolor(c)
    b.set_linewidth(1)

这篇关于Matplotlib:如何在不透明的线边缘上有透明的框图面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆