Pyplot - y轴刻度的位移及其数据

问题描述 投票:0回答:2

Visual summary of what I want to do

使用pyplot,如何修改我的绘图来改变我的yticks的垂直位置?例如。在上面的情节中,我想将'Promoter'向下移动'CDS'(以及在情节中的'line')。

对于上面的图,我的x数据是一系列数字,而我的y数据是分类的。重现情节的代码如下:

import matplotlib.pyplot as plt

x_CDS = list(range(661, 668))
y_CDS = ["CDS"] * len(x_CDS)

x_RBS = list(range(649, 656))
y_RBS = ["RBS"] * len(x_RBS)

x_prom = list(range(570, 601))
y_prom = ["Promoter"] * len(x_prom)

plt.figure(figsize=(10,6))
plt.xlim(1, 3002)
plt.xlabel('Nucleotide position')

plt.plot(x_CDS, y_CDS, label='CDS')
plt.plot(x_RBS, y_RBS, label='RBS')
plt.plot(x_prom, y_prom, label='Promoter')

注意:在这种情况下,线条非常小,但为方便起见,可以使范围更大。

提前致谢!

python python-3.x matplotlib axis
2个回答
2
投票

默认情况下,matplotlib在数据的每一侧产生约5%的边距。这里似乎你想增加垂直方向的这个余量。也许你想要40%,即plt.margins(y=0.4)

import matplotlib.pyplot as plt

x_CDS = list(range(661, 668))
y_CDS = ["CDS"] * len(x_CDS)

x_RBS = list(range(649, 656))
y_RBS = ["RBS"] * len(x_RBS)

x_prom = list(range(570, 601))
y_prom = ["Promoter"] * len(x_prom)

plt.figure(figsize=(10,6))

plt.xlabel('Nucleotide position')

plt.plot(x_CDS, y_CDS, label='CDS')
plt.plot(x_RBS, y_RBS, label='RBS')
plt.plot(x_prom, y_prom, label='Promoter')

plt.margins(y=0.4)

plt.show()

enter image description here

在这里使用margins而不是更改ylim的优点是,您不需要计算类别以找出为限制选择的有用值。但是当然你可以通过plt.ylim(-0.8,2.8) toc同样改变限制来实现相同的情节。


1
投票

plt.margins(y=10)应在y轴刻度的顶部和底部提供填充。我使用y = 10作为示例,请根据需要进行调整。希望,这就是你要找的东西。

© www.soinside.com 2019 - 2024. All rights reserved.