Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to show y-axis of subtracks? #51

Closed
phyto opened this issue Jul 9, 2024 · 1 comment
Closed

How to show y-axis of subtracks? #51

phyto opened this issue Jul 9, 2024 · 1 comment
Labels
question Further information is requested

Comments

@phyto
Copy link

phyto commented Jul 9, 2024

Dear Moshi4,
Do you have any suggestions on how to show the y axis for subtracks? For instance, with the first "subtrack" example in your "plot tips" doc section, how to display the y axis and '50', '75' and '100' ticks for the subtrack? Thank you!

@moshi4 moshi4 added the question Further information is requested label Jul 9, 2024
@moshi4
Copy link
Owner

moshi4 commented Jul 9, 2024

Code Example

from pygenomeviz import GenomeViz
import numpy as np
np.random.seed(0)

genome_list = [
    dict(name="genome 01", size=1000, features=((150, 300, 1), (500, 700, -1), (750, 950, 1))),
    dict(name="genome 02", size=1300, features=((50, 200, 1), (350, 450, 1), (700, 900, -1), (950, 1150, -1))),
    dict(name="genome 03", size=1200, features=((150, 300, 1), (350, 450, -1), (500, 700, -1), (700, 900, -1))),
]

gv = GenomeViz(fig_track_height=0.7, feature_track_ratio=0.5, track_align_type="center")
gv.set_scale_bar()

for genome in genome_list:
    name, size, features = genome["name"], genome["size"], genome["features"]
    track = gv.add_feature_track(name, size)
    track.add_subtrack(ylim=(0, 100)) # Add subtrack
    for idx, feature in enumerate(features, 1):
        start, end, strand = feature
        track.add_feature(start, end, strand, plotstyle="bigarrow", lw=1)

fig = gv.plotfig()

# Plot user-defined graph to subtrack axes
for track in gv.feature_tracks:
    subtrack = track.get_subtrack()
+   subtrack.ax.tick_params(left=True, labelleft=True)
+   subtrack.ax.spines["left"].set_position(("data", track.transform_coord(0)))
+   subtrack.ax.set_yticks([0, 50, 100], [0, 50, 100])
    for segment in track.segments:
        # Plot y = (50 - 100) random values to subtrack
        # Tranform segment-level x coordinate to track-level coordinate
        x = np.arange(segment.start, segment.end, 10)
        x = segment.transform_coord(x)
        y = np.random.randint(50, 100, len(x))
        subtrack.ax.fill_between(x, y, color="grey")

fig.savefig("result.png")

result.png

result

Knowledge of matplotlib is required for additional plots on subtracks.
If you want to know more details, please learn about Axes in matplotlib.

@moshi4 moshi4 closed this as completed Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants