PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 30, 2022

[FIXED] How to add titles to subplots containing images

 July 30, 2022     image, label, plotly, python, subplot     No comments   

Issue

Starting from a basic image example, I made 3 subplots:

import plotly.graph_objects as go
from plotly.subplots import make_subplots

imgs_rgb = (
    [[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
     [[0, 255, 0], [0, 0, 255], [255, 0, 0]]],
    [[[255, 127, 0], [127, 255, 0], [127, 0, 255]],
     [[127, 255, 0], [127, 0, 255], [255, 127, 0]]],
    [[[0, 255, 255], [0, 255, 127], [255, 0, 255]],
     [[255, 255, 0], [0, 255, 255], [127, 255, 0]]],
)    

fig = make_subplots(rows=len(imgs_rgb), cols=1)
for row, img_rgb in enumerate(imgs_rgb):
    fig.add_trace(
        go.Image(z=img_rgb),
        row=row+1,
        col=1
    )

fig.show()

How can we add a title/label to each individual subplot, as shown e.g here: https://plotly.com/python/imshow/#exploring-3d-images-timeseries-and-sequences-of-images-with-facetcol

image showing 3 images with different titles

Note that I don't have plotly.express, so using its functionality isn't an option.


Solution

You can just a pass the subplot titles when calling make_subplots():

import plotly.graph_objects as go
from plotly.subplots import make_subplots

imgs_rgb = (
    [[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
     [[0, 255, 0], [0, 0, 255], [255, 0, 0]]],
    [[[255, 127, 0], [127, 255, 0], [127, 0, 255]],
     [[127, 255, 0], [127, 0, 255], [255, 127, 0]]],
    [[[0, 255, 255], [0, 255, 127], [255, 0, 255]],
     [[255, 255, 0], [0, 255, 255], [127, 255, 0]]],
)    

fig = make_subplots(rows=len(imgs_rgb), cols=1, subplot_titles=("Plot1", "Plot2", "Plot3"))
for row, img_rgb in enumerate(imgs_rgb):
    fig.add_trace(
        go.Image(z=img_rgb),
        row=row+1,
        col=1
    )

fig.show()

enter image description here



Answered By - Pickniclas
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing