-
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
Changes from 21 commits
cb4e839
12d2f47
f796b91
022be58
dff71b2
d3dd7a0
624f1b8
ceaefde
235ebe7
fe4e387
1a7e0cd
56d0a61
e752c52
55ea1d0
99cada5
d3b4a3c
5e7e62e
d361d92
622d5a2
fca8b89
ac14d01
3fdefa5
edb49d9
1cc415f
dabd5fe
ff0d2a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
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. | ||
|
||
Args: | ||
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. Since numpy style is used elsewhere, maybe change from
to
and similar for the rest. 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. I've adjusted the style, how is it now? |
||
color_list: list | ||
List of colors from color map | ||
|
||
Return: | ||
color_swatch: | ||
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. | ||
|
||
Args: | ||
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 |
Large diffs are not rendered by default.
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