Last active
March 14, 2022 17:03
-
-
Save andrewmoles2/67fbd05bae17499f77a5682d5cffc40f to your computer and use it in GitHub Desktop.
Coolors function in Python
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
# import plotnine for demo and testing | |
from plotnine import * | |
from plotnine.data import mtcars | |
def coolors(URL): | |
# function takes coolors url, extracts hex codes and adds # | |
if URL.find("palette") != -1: | |
# extract just the hex | |
cstr = URL.replace("coolors.co", "") | |
cstr = cstr.replace("https:///", "") | |
cstr = cstr.replace("palette/", "") | |
cstr = cstr.replace("-", " ") | |
# split into individual strings and add # | |
clist = cstr.split(" ") | |
cols = [] | |
for hex in range(len(clist)): | |
cols.append((str("#" + clist[hex]))) | |
return cols | |
else: | |
# extract just the hex | |
cstr = URL.replace("coolors.co", "") | |
cstr = cstr.replace("https:///", "") | |
cstr = cstr.replace("-", " ") | |
# split into individual strings and add # | |
clist = cstr.split(" ") | |
cols = [] | |
for hex in range(len(clist)): | |
cols.append((str("#" + clist[hex]))) | |
return cols | |
# test | |
url = str("https://coolors.co/00a878-6ccd8c-d8f1a0-e6d98c-f3c178-f9905d-fe5e41-853221-0b0500") | |
url2 = str("https://coolors.co/palette/f4f1de-e07a5f-3d405b-81b29a-f2cc8f") | |
print(coolors(url)) | |
print(coolors(url2)) | |
test_plot = ggplot(mtcars, aes('mpg', 'hp', color = 'factor(carb)')) +\ | |
geom_point() +\ | |
scale_color_manual(values = coolors(url)) | |
print(test_plot) | |
test_plot_2 = ggplot(mtcars, aes('mpg', 'hp', color = 'factor(cyl)')) +\ | |
geom_point() +\ | |
scale_color_manual(values = coolors(url2)) | |
print(test_plot_2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment