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

Assign different portions of a single colormap to different sub-colormaps #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/plot_example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ tight_layout()
# Wiggle plot
figure(figsize=(10, 5))
wiggle_plot(shot[1:5:end, 1:10:end], xloc[1:10:end], 0:0.02:4.6; new_fig=false)

# plot with multiple colorscales
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would appreciate an actual illustrative example like all the other ones that use proper images/shot records/.... because this doesn't really show anything.

figure(figsize=(10, 5))
intervals = [(-3,-2),(-1,1),(2,3)]
cmap_types = [matplotlib.cm.YlGn,matplotlib.cm.Purples,matplotlib.cm.Reds]
cmap = create_multi_color_map(intervals, cmap_types)
plot_simage(randn(Float32, 100, 100), (1, 1); cmap=cmap, new_fig=false, interp="none", name="colorful white noise")
40 changes: 40 additions & 0 deletions src/SlimPlotting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ end

export plot_fslice, plot_velocity, plot_simage, plot_sdata, wiggle_plot
export colorschemes
export create_multi_color_map

"""
_plot_with_units(image, spacing; perc=95, cmap=:cet_CET_L1,
Expand Down Expand Up @@ -286,4 +287,43 @@ function wiggle_plot(data::Array{Td, 2}, xrec=nothing, time_axis=nothing;
ax.set_ylabel("Time")
end

"""
create_multi_color_map(intervals, cmap_types)

Create a single colormap that assigns different portions of it to different sub-colormaps.

# Arguments
- `intervals::Vector{Tuple{T, T}}`: intervals of values where each interval will be assigned to a different colormap
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not super practical especially considering it plots with perc. Working with actual portion in % or something would be more user friendly.

- `cmap_types::Vector{ColorMap}`: colormaps for each interval
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just cmaps

- `out_of_interval_color`: (Optional) the color assigned to values that live outside of all the intervals, default is black [0.,0.,0.,1.]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someting like taht is usually just called padding


# Example
create_multi_color_map([(1,2),(3,4),(5,6)], [matplotlib.cm.YlGn,matplotlib.cm.Purples,matplotlib.cm.Reds])
"""
function create_multi_color_map(intervals::Vector{Tuple{T, T}}, cmap_types::Vector{ColorMap}; out_of_interval_color=[0.,0.,0.,1.]) where T

@assert length(intervals) == length(cmap_types) "number of intervals and number of colormaps do not match"

# ensure at least 100 points in each interval
colorrange = intervals[1][1]:minimum([intervals[i][2]-intervals[i][1] for i = 1:length(intervals)])/1f2:intervals[end][2]

# number of points in each colormap
colorlength = [Int.(round.((intervals[i][2]-intervals[i][1])./(colorrange[2]-colorrange[1]))) for i = 1:length(intervals)]

# create each colormap
cmap_sections = [cmap_types[i](range(0f0, stop=1f0, length=colorlength[i])) for i = 1:length(cmap_types)]

# fill in out-of-interval by out_of_interval_color
between_interval_sections = [reshape(repeat(out_of_interval_color, inner=Int(round((intervals[i][1]-intervals[i-1][2])/(colorrange[2]-colorrange[1])))), :, 4) for i = 2:length(intervals)]

# concatenate all the sections to get the entire colormap
total_sections = vcat(cmap_sections[1], vcat([vcat(between_interval_sections[i], cmap_sections[i+1]) for i = 1:length(cmap_sections)-1]...))

# construct the colormaps
cmaps = matplotlib.colors.ListedColormap(total_sections)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks bit overly intricated but that's fine


return cmaps

end

end # module