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

时间:2018-09-04 18:39:42

标签: python python-3.x matplotlib axis

Visual summary of what I want to do

使用pyplot,如何修改图以更改yticks的垂直位置?例如。在上面的图中,我想向下移动“启动子”,并向上移动“ CDS”(以及图中的“线”)。

对于上面的图,我的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')

注意:在这种情况下,行数很小,但为方便起见,可以将范围扩大。

谢谢!

2 个答案:

答案 0 :(得分: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)来更改限制以实现相同的图。

答案 1 :(得分:1)

plt.margins(y=10)应该在y轴刻度线的顶部和底部提供填充。我以y = 10为例,请根据需要进行调整。希望这就是您想要的。

相关问题