【问题标题】:Pyplot - shift position of y-axis ticks and its dataPyplot - y轴刻度及其数据的移位位置
【发布时间】:2018-09-04 18:39:42
【问题描述】:

使用 pyplot,如何修改绘图以更改 yticks 的垂直位置?例如。在我上面的情节中,我想将“Promoter”向下移动,“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')

注意:本例中的线条非常小,但为方便起见,可以将范围扩大。

提前致谢!

【问题讨论】:

  • 您能否提供一个包含数据并重现您当前图形的最小工作代码?
  • 我已经添加了最小的工作代码来重现!
  • 由于ValueError: could not convert string to float: 'CDS',它没有产生任何东西。确保粘贴为您生成绘图的代码

标签: python python-3.x matplotlib axis


【解决方案1】:

默认情况下,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()

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

【讨论】:

  • 非常感谢 - 我知道像“.margins”这样的东西必须存在于某个地方,但就是找不到。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2021-08-04
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    相关资源
    最近更新 更多