Python 如何更改标记边框宽度和填充宽度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14325773/
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
How to change marker border width and hatch width?
提问by David E
In this example of a marker from my scatter plot I have set the color to green, and edge color to black, and hatch to "|". For the hatch pattern to show up at all I must set the edgecolor, however when I do, I get a very thick border around the marker. Two questions:
在这个散点图中的标记示例中,我将颜色设置为绿色,边缘颜色设置为黑色,剖面线设置为“|”。为了完全显示填充图案,我必须设置边缘颜色,但是当我这样做时,我会在标记周围得到一个非常粗的边框。两个问题:
1) How can I to set the size of this border (preferably to 0)?
1) 如何设置此边框的大小(最好为 0)?
2) How can I increase the thickness of the hatch lines?
2) 如何增加影线的粗细?
采纳答案by Bonlenfum
- You just need to set the
linewidthto control the marker border thickness. - You can increase the density of hatching, by repeating symbols (in the example below, the
'|'is repeated in the R/H pane; note that to obtain NW->SE diagonal lines the symbol must be escaped so needs twice as many characters to really double it --'\\\\'is density 2 while'||||'is density 4). However, I don't think the thickness of individual lines within hatching is controllable.
- 您只需要设置
linewidth来控制标记边框的粗细。 - 您可以通过重复符号来增加阴影的密度(在下面的示例中,
'|'在 R/H 窗格中重复;请注意,要获得 NW->SE 对角线,必须对符号进行转义,因此需要两倍的字符才能真正加倍——'\\\\'密度为 2,'||||'密度为 4)。但是,我不认为影线内单个线条的粗细是可控的。
See the code example below to produce scatter plots such as these:

请参阅下面的代码示例以生成如下散点图:

import matplotlib.pyplot as plt
# generate some data
x = [1,2,3,4,5,8]
y= [i**2 for i in x]
y2= [60-i**2+3*i for i in x]
# plot markers with thick borders
plt.subplot(121)
plt.scatter(x,y, s=500, marker='s', edgecolor='black', linewidth='3', facecolor='green', hatch='|')
# compare with no borders, and denser hatch.
plt.subplot(122)
plt.scatter(x,y, s=500, marker='s', edgecolor='black', linewidth='0', facecolor='green', hatch='||||')
plt.show()
matplotlib documentation on collectionsand scatter.
回答by Jacob
This is several years after you asked the question, but the only way I've found to do it is to change the matplotlib.rc.
这是你问这个问题几年后,但我发现这样做的唯一方法是更改 matplotlib.rc。
You can do this either in the actual .rc file or within your python script, e.g.
您可以在实际的 .rc 文件中或在您的 python 脚本中执行此操作,例如
import matplotlib as mpl
import matplotlib as mpl
mpl.rc('hatch', color='k', linewidth=1.5)
mpl.rc('hatch', color='k', linewidth=1.5)
This will make all of the hatch lines in your script black and thickness 1.5 rather than the default 1.0.
这将使脚本中的所有剖面线为黑色,粗细为 1.5,而不是默认的 1.0。

