gpt4 book ai didi

python - Pyplot axvspan : Multiple colors in one span (vertically)

转载 作者:行者123 更新时间:2023-12-01 04:00:38 26 4
gpt4 key购买 nike

我正在尝试使用多种颜色突出显示 x 轴上的区域。我设法通过沿 x 轴剖切区域来找到解决方案,如下图所示: enter image description here

但是,我想要一个在 y 轴上进行切片的解决方案。以图中的6362为例。有没有什么方法可以创建类似虚线条的东西,其中每个其他破折号(或任何名称)都是紫色和红色的?

编辑这是水平突出显示每个小节的相关代码

# Find exon's index
e_index = sorted(list(all_samples.ensembl_exon_id.unique())).index(exon)

# Total x-axis span incl offsets
xmin = e_index-0.25 # Start of x-span
xmax = e_index+0.25 # End of x-span
diff = xmax-xmin # Length of entire span
buf = diff / len(s_names) # Length of each subsection

# Go through each sample
for sname in s_names:
# Get color of this sample
s_color = colors[sname]

# Get index of this sample
order = list(s_names).index(sname)

# Calc xmin and xmax for subsection
s_xmin = xmin + (buf * order)
s_xmax = s_xmin + buf

# Highlight
plt.axvspan(xmin=s_xmin, xmax=s_xmax, alpha=0.25, color=s_color, zorder=0.6, ymin=0, ymax=1)

最佳答案

您可以使用 axvspanyminymax 选项来创建每个“破折号”。通过循环轴间隔 0-1,您可以构建所有破折号。

这是我整理的一个快速函数,使其在一定程度上自动化。使用所需的选项调用 vspandash 以用破折号填充该区域。

import matplotlib.pyplot as plt
import numpy as np

fig,ax = plt.subplots(1)

x=y=np.arange(11)

ax.plot(x,y,'go-')

def vspandash(thisax,xmark,xwidth=0.6,ndash=10,colour1='r',colour2='m'):

interval = 1./ndash
hxwidth = xwidth/2.

for j in np.arange(0,1,interval*2):
thisax.axvspan(
xmin=xmark-hxwidth,xmax=xmark+hxwidth,
ymin=j,ymax=j+interval,
facecolor=colour1,alpha=0.25,edgecolor='None'
)
thisax.axvspan(
xmin=xmark-hxwidth,xmax=xmark+hxwidth,
ymin=j+interval,ymax=j+interval*2.,
facecolor=colour2,alpha=0.25,edgecolor='None'
)

# Lets explore the different options
vspandash(ax,2) # Default width, number of dashes, and colours
vspandash(ax,4,ndash=20) # Increase number of dashes
vspandash(ax,6,xwidth=0.3) # Change width of shaded region
vspandash(ax,8,colour1='b',colour2='g') # Change colours of dashes

plt.show()

enter image description here

关于python - Pyplot axvspan : Multiple colors in one span (vertically),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36618397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com