Pyplot-y轴刻度的移动位置及其数据 [英] Pyplot - shift position of y-axis ticks and its data

查看:267
本文介绍了Pyplot-y轴刻度的移动位置及其数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用pyplot,如何修改图以更改yticks的垂直位置?例如.在上面的情节中,我想向下移动促销员"并向上移动"CDS"(以及情节中的线").

Using pyplot, how do I modify my plot to change the vertical position of my yticks? E.g. in my plot above, I want to move 'Promoter' down and 'CDS' up (along with their 'lines' in the plot).

对于上面的图,我的x数据是一个数字范围,而我的y数据是分类的.复制图的代码如下:

For the above plot, my x-data is a range of numbers, while my y-data is categorical. Code to reproduce plot as follows:

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')

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

Note: the lines in this case are quite small, but the ranges can be made larger for convenience.

提前谢谢!

推荐答案

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

By default matplotlib produces some 5% margins on each side of the data. Here it seems you want to increase this margin for the vertical direction. Maybe you want 40%, i.e. 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()

在此处使用margins而不是更改ylim的优点是,您无需对类别进行计数即可找出为限制选择的有用值.当然,您也可以通过plt.ylim(-0.8,2.8) toc更改限制以达到相同的图.

The advantage of using margins here instead of changing the ylim is that you do not need to count the categories to find out what useful value to choose for the limits. But of course you may equally change the limits via plt.ylim(-0.8,2.8) toc achieve the same plot.

这篇关于Pyplot-y轴刻度的移动位置及其数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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