Pyplot - shift position of y-axis ticks and its data

2024/10/12 21:23:54

Visual summary of what I want to do

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

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 pltx_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.

Thanks in advance!

Answer

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 pltx_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

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.

https://en.xdnf.cn/q/118156.html

Related Q&A

How to exit a Python program or loop via keybind or macro? Keyboardinterrupt not working

I am trying to complete a simple GUI automation program that merely opens a web page and then clicks on a specific spot on the page every 0.2 seconds until I tell it to stop. I want my code to run and …

SKlearn prediction on test dataset with different shape from training dataset shape

Im new to ML and would be grateful for any assistance provided. Ive run a linear regression prediction using test set A and training set A. I saved the linear regression model and would now like to use…

How to eliminate suspicious barcode (like 123456) data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…

how to get href link from onclick function in python

I want to get href link of website form onclick function Here is html code in which onclick function call a website <div class="fl"><span class="taLnk" onclick="ta.tr…

Python tkinters entry.get() does not work, how can I fix it? [duplicate]

This question already has answers here:Why is Tkinter Entrys get function returning nothing?(6 answers)Closed 7 years ago.I am building a simple program for university. We have to convert our code to …

Pandas secondary y axis for boxplots

Id like to use a secondary y-axis for some boxplots in pandas, but it doesnt seem available. import numpy as np import pandas as pddata = np.random.random((10, 5)) data[:,-1] += 10 # offset one column…

Fixing Negative Assertion for end of string

I am trying to accept a capture group only if the pattern matches and there is not a specific word before the end of the group. Ive tried a # of approaches and none seem to work, clearly Im not getting…

Two Sorted Arrays, sum of 2 elements equal a certain number

I was wondering if I could get some help. I want to find an algorithm that is THETA(n) or linear time for determining whether 2 numbers in a 2 sorted arrays add up to a certain number.For instance, let…

I cant seem to install numpy

I tried to install numpy, but whenever I start my program, I get these messages.Error importing numpy: you should not try to import numpy fromits source directory; please exit the numpy source tree, an…

Using slices in Python

I use the dataset from UCI repo: http://archive.ics.uci.edu/ml/datasets/Energy+efficiency Then doing next:from pandas import * from sklearn.neighbors import KNeighborsRegressor from sklearn.linear_mode…