Skip to content

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 26 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 21 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 May 11, 2023
12d2f47
update init files with colors.py
sajidah-ahmed May 11, 2023
f796b91
update pyproject file with colour package
sajidah-ahmed May 11, 2023
022be58
update poetry lock file with colour package
sajidah-ahmed May 11, 2023
dff71b2
update README with generate_hex_colors
sajidah-ahmed May 11, 2023
d3dd7a0
update README
sajidah-ahmed May 11, 2023
624f1b8
update README
sajidah-ahmed May 11, 2023
ceaefde
figure of color swatches
sajidah-ahmed May 11, 2023
235ebe7
Add comment about hex numbers
sajidah-ahmed May 11, 2023
fe4e387
Move colors paragraph to the bottom of README
sajidah-ahmed May 11, 2023
1a7e0cd
update doc strings
sajidah-ahmed May 11, 2023
56d0a61
update doc string
sajidah-ahmed May 11, 2023
e752c52
Correct the path
sajidah-ahmed May 11, 2023
55ea1d0
show output of color_list in README
sajidah-ahmed May 11, 2023
99cada5
example sine function
sajidah-ahmed May 11, 2023
d3b4a3c
remove example-sine-function
sajidah-ahmed May 11, 2023
5e7e62e
add hex color example to Readme
gregordecristoforo May 11, 2023
d361d92
Merge branch 'hex_colors' of github.com:uit-cosmo/cosmoplots into hex…
gregordecristoforo May 11, 2023
622d5a2
put example figures next to each other
gregordecristoforo May 11, 2023
fca8b89
remove unwanted ticks
gregordecristoforo May 11, 2023
ac14d01
Update description of hex_colors
gregordecristoforo May 11, 2023
3fdefa5
Adjust documentation style
sajidah-ahmed May 11, 2023
edb49d9
Adjust indentation
sajidah-ahmed May 11, 2023
1cc415f
Add figure declaration
sajidah-ahmed May 11, 2023
dabd5fe
chore(type): add return type to make_color_swatch
engeir May 11, 2023
ff0d2a6
chore: remove duplicate file
engeir May 11, 2023
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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,29 @@ plt.show()

<!-- Links -->
[poetry]: https://python-poetry.org


## `generate_hex_colors`

This function generates the hex numbers for the colours extracted from a `matplotlib` colour map based on the number of points of interest.
The colors change gradually from bright to dark or vice versa.
```python
import matplotlib.pyplot as plt
import cosmoplots

axes_size = cosmoplots.set_rcparams_dynamo(plt.rcParams, num_cols=1, ls="thin")

color_list = cosmoplots.generate_hex_colors(5, 'viridis', show_swatch=True, ascending=True)
plt.savefig("./assets/hex_colors.png")

# Print color_list to retrieve the hex numbers
print(color_list) #['#fde725', '#5ec962', '#21918c', '#3b528b', '#440154']

for i, color in enumerate(color_list):
plt.plot([1,2],[i,i+1], c = color)

plt.savefig("./assets/hex_colors_example.png")
```
| `hex_colors.png` | hex_colors_example.png |
| :--------: | :--------: |
| ![colors](./assets/hex_colors.png) | ![colors](./assets/hex_colors_example.png) |
Binary file added assets/hex_colors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/hex_colors_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions cosmoplots/__init__.py
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"
86 changes: 86 additions & 0 deletions cosmoplots/colors.py
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):
Copy link
Member

Choose a reason for hiding this comment

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

Missing return type

"""
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:
Copy link
Member

Choose a reason for hiding this comment

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

Since numpy style is used elsewhere, maybe change from

Args:

to

Parameters
----------

and similar for the rest.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.
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
Binary file added hex_colors_example.png
Copy link
Member

Choose a reason for hiding this comment

The 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.
602 changes: 379 additions & 223 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ readme = 'README.md'
python = ">=3.8"
numpy = ">=1.15.0"
matplotlib = ">=3.3.2"
colour = ">=0.1.5"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down