Can you write a function like that but using different image making lib? Should work in colab and automatically making pixels bigger if it's e.g. 30 x 30 input array
Below is a reimplementation of your draw function, but using a different image-making library: Plotly. Plotly is a great alternative because it's interactive, works seamlessly in Google Colab (and other Jupyter environments), and allows us to display arrays as images with custom colorscales (similar to Matplotlib's colormaps).
Key changes and features:
Library used: Plotly (specifically, plotly.graph_objects) instead of Matplotlib.
Automatic pixel enlargement: If the input array is small (e.g., 30x30), the function scales up the display by calculating a pixel size multiplier. For instance:
For a 30x30 array, it might make each "pixel" larger (e.g., 20x20 in the output figure) to make it more visible.
I introduced a simple heuristic: If the array's smaller dimension is less than 50, use a larger pixel size (e.g., 20). Otherwise, use a default (e.g., 10). You can adjust this as needed.
Colormap handling: Plotly uses "colorscales" instead of Matplotlib's colormaps. I mapped your 'copper' cmap to Plotly's 'hot' colorscale as a close approximation (it's reddish-yellow, similar to copper). If you need exact matching, you can customize it.
Colab compatibility: This function uses fig.show(), which works directly in Colab without issues.
Other details: The function assumes the input array is a 2D NumPy array (e.g., grayscale values in [0, 1] or [0, 255]). It normalizes the array if needed and hides axes for a clean display.
Here's the code:
python
import numpy as np
import plotly.graph_objects as go
def draw(arr):
# Ensure arr is a 2D NumPy array
if not isinstance(arr, np.ndarray) or len(arr.shape) != 2:
raise ValueError("Input must be a 2D NumPy array.")
# Normalize the array to [0, 1] for Plotly colorscale
arr_normalized = (arr - arr.min()) / (arr.max() - arr.min()) # Scales to [0, 1]
height, width = arr_normalized.shape
# Determine pixel size based on array size (automatic enlargement)
if min(height, width) < 50: # Example threshold: if array is small (e.g., 30x30)
pixel_size = 20 # Make pixels bigger for better visibility
else:
pixel_size = 10 # Default pixel size
# Calculate the figure size based on the array dimensions and pixel size
fig_width = width * pixel_size
fig_height = height * pixel_size
# Create the Plotly figure
fig = go.Figure(go.Imshow(
z=arr_normalized, # The normalized array
colorscale='hot', # Approximate 'copper' colormap; you can change to 'Viridis', 'Cividis', etc.
showscale=False # Hide the color scale bar for a cleaner look
))
# Update layout to remove axes and set the figure size
fig.update_layout(
width=fig_width,
height=fig_height,
margin=dict(l=0, r=0, t=0, b=0), # Remove margins
xaxis=dict(visible=False, showticklabels=False), # Hide x-axis
yaxis=dict(visible=False, showticklabels=False) # Hide y-axis
)
# Display the figure (works in Colab)
fig.show()
# No need for fig.close() as Plotly handles it automatically in notebooks
# Example usage:
# import numpy as np
# arr = np.random.rand(30, 30) # A small 30x30 array
# draw(arr)
How to Use This in Colab:
Make sure you have the necessary libraries installed. In Colab, Plotly is usually pre-installed, but if not, you can run:
code
!pip install plotly
Call the function with your array, e.g.:
python
import numpy as np
arr = np.array([[1, 0.5, 0], [0.5, 0, 1], [0, 1, 0.5]]) # Example 3x3 array
draw(arr)
For a 30x30 array, it will automatically enlarge the pixels, making the image more readable.
Why This Works Well:
Pixel Enlargement: The function dynamically adjusts the figure size based on the array's dimensions, so small arrays (like 30x30) will appear larger without losing detail.
Performance in Colab: Plotly renders interactively in Colab, and the output is a static image by default when exported or saved.
Customizations:
If you want to change the colorscale, refer to Plotly's documentation for available options (e.g., 'hot', 'Viridis'). You can pass a custom one if needed.
If your array is not in [0, 1], the normalization step will handle it.
Adjust the pixel_size values (e.g., 10 or 20) or the threshold (e.g., <50) to fine-tune the enlargement.
This should give you a similar result to your original function but with a fresh library. If you need further tweaks (e.g., exact colormap matching or handling for color images), let me know!