-
Notifications
You must be signed in to change notification settings - Fork 2
Color swatches from color maps that are suitable for grayscale printing #12
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
Merged
Merged
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
cb4e839
functions for generating color swatches from color maps
sajidah-ahmed 12d2f47
update init files with colors.py
sajidah-ahmed f796b91
update pyproject file with colour package
sajidah-ahmed 022be58
update poetry lock file with colour package
sajidah-ahmed dff71b2
update README with generate_hex_colors
sajidah-ahmed d3dd7a0
update README
sajidah-ahmed 624f1b8
update README
sajidah-ahmed ceaefde
figure of color swatches
sajidah-ahmed 235ebe7
Add comment about hex numbers
sajidah-ahmed fe4e387
Move colors paragraph to the bottom of README
sajidah-ahmed 1a7e0cd
update doc strings
sajidah-ahmed 56d0a61
update doc string
sajidah-ahmed e752c52
Correct the path
sajidah-ahmed 55ea1d0
show output of color_list in README
sajidah-ahmed 99cada5
example sine function
sajidah-ahmed d3b4a3c
remove example-sine-function
sajidah-ahmed 5e7e62e
add hex color example to Readme
gregordecristoforo d361d92
Merge branch 'hex_colors' of github.com:uit-cosmo/cosmoplots into hex…
gregordecristoforo 622d5a2
put example figures next to each other
gregordecristoforo fca8b89
remove unwanted ticks
gregordecristoforo ac14d01
Update description of hex_colors
gregordecristoforo 3fdefa5
Adjust documentation style
sajidah-ahmed edb49d9
Adjust indentation
sajidah-ahmed 1cc415f
Add figure declaration
sajidah-ahmed dabd5fe
chore(type): add return type to make_color_swatch
engeir ff0d2a6
chore: remove duplicate file
engeir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .figure_defs import * | ||
from .axes import * | ||
from .colors import * | ||
|
||
__version__ = "0.1.6" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import numpy as np | ||
from colour import Color | ||
from matplotlib.colors import LinearSegmentedColormap | ||
import matplotlib.pyplot as plt | ||
from matplotlib import cm, colors | ||
from typing import List | ||
|
||
|
||
def make_color_swatch(color_list: list): | ||
""" | ||
Generate plot of the desired color swatches. This is useful if you want to see what the swatches look like. | ||
This function is used in generate_hex_colors() where the plot of the swatches will show by default if used in Jupyter Notebook. | ||
|
||
Parameters | ||
---------- | ||
color_list: list | ||
List of colors from color map | ||
|
||
Returns | ||
------- | ||
color_swatch: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing return type |
||
A plot of the color swatches | ||
""" | ||
|
||
color_swatch = LinearSegmentedColormap.from_list( | ||
"my_list", [Color(x).rgb for x in color_list] | ||
) | ||
|
||
plt.figure(figsize=(5, 3)) | ||
plt.imshow( | ||
[list(np.arange(0, len(color_list), 1))], | ||
interpolation="nearest", | ||
origin="lower", | ||
cmap=color_swatch, | ||
) | ||
plt.xticks([]) | ||
plt.yticks([]) | ||
|
||
return color_swatch | ||
|
||
|
||
def generate_hex_colors( | ||
number_data_points: int, | ||
color_map: str, | ||
show_swatch: bool = True, | ||
ascending: bool = True, | ||
) -> List[str]: | ||
""" | ||
Function to generate colors picked out of the matplolib color maps as hex numbers. | ||
sajidah-ahmed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
If using this function in Jupyter Notebook, by default since show_swatch = True, a plot of the swatches will automatically appear. | ||
|
||
Parameters | ||
---------- | ||
number_data_points: int | ||
The number of data points | ||
color_map: str | ||
The name of the colour map from matplotlib. Grayscale friendly colors are: | ||
'viridis', 'plasma', 'inferno', 'magma', 'cubehelix', 'cividis' | ||
See more here: https://matplotlib.org/stable/tutorials/colors/colormaps.html | ||
show_swatch: bool = True | ||
Show the color swatches based on the number of data points. 'True' means that a plot will show containing the swatches. | ||
ascending: bool = True | ||
Show the color swatches and list in ascending order, from light to dark. Set to True by default. | ||
|
||
Returns | ||
------- | ||
color_list: List[str] | ||
List of hex numbers from the desired color map. Simply do print(color_list) to get this list. | ||
|
||
""" | ||
|
||
cmap = cm.get_cmap(color_map, number_data_points) | ||
|
||
color_list = [] | ||
|
||
for i in range(cmap.N): | ||
rgba = cmap(i) | ||
|
||
# rgb2hex accepts rgb or rgba | ||
color_list.append(colors.rgb2hex(rgba)) | ||
|
||
# return light to darkest if ascending | ||
if ascending: | ||
if show_swatch: | ||
make_color_swatch(color_list[::-1]) | ||
return color_list[::-1] | ||
else: | ||
if show_swatch: | ||
make_color_swatch(color_list) | ||
return color_list |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate file |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing return type