[python] Method to darken RGB value in hexadecimal notation

python


def darken(rgb, rate=0.5):
    rgb = rgb.replace('#', '')
    s = '#'
    for i in [0,2,4]:
        c = rgb[i:i+2]
        c = int(c, 16)
        c = int(c * rate)
        c = format(c, '02x')
        s += c
    return s

#Example of use
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
color_orig = [
    '#C5DAEE',
    '#ABCFE5',
    '#8DC0DD',
    '#6AADD5',
    '#4F9BCB',
    '#3787C0',
    '#2070B4',
    '#0F5BA3',
    '#08458B',
    '#08306B',
]
color = [
    [darken(c) for c in color_orig],
    color_orig,
]
df = pd.DataFrame([np.ones(10), np.ones(10)], columns=[x[1] + '\n' + x[0] for x in np.array(color).T])
df.T.plot.bar(color=color, stacked=True)
plt.legend().remove()
plt.yticks([])
plt.xticks(rotation=0)
plt.show()

out:

スクリーンショット 2021-01-06 午後2.59.40.png

Recommended Posts

[python] Method to darken RGB value in hexadecimal notation
[python] Method to darken RGB value in hexadecimal notation
Shapley value calculation in Python
Convert exponential notation float to str in Python
Method to build Python environment in Xcode 6
To dynamically replace the next method in python
Convert exponential notation float to str in Python
Simplex method (simplex method) in Python
Private method in python
Hexadecimal handling in Python 3
Class notation in Python
How to retrieve the nth largest value in Python
[Python] No value for argument'self' in unbound method call
Convert CIDR notation netmask to dotted decimal notation in Python
[Python] Created a method to convert radix in 1 second
To flush stdout in Python
Login to website in Python
Shapley value calculation in Python
Convert CIDR notation in Python
Speech to speech in python [text to speech]
Implement method chain in Python
How to develop in Python
Suppressing method overrides in Python
Post to Slack in Python
How to use the __call__ method in a Python class
I tried "How to get a method decorated in Python"
Convenient writing method when appending to list continuously in Python
How to get the last (last) value in a list in Python
[Python] How to do PCA in Python
Included notation in Python function arguments
Convert markdown to PDF in Python
How to collect images in Python
Try implementing extension method in python
How to use SQLite in Python
In the python command python points to python3.8
Try to calculate Trace in Python
Implemented label propagation method in Python
[Python] The value written in Openpyxl is displayed in exponential notation (E).
Simulate Monte Carlo method in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
Hash method (open address method) in Python
How to use PubChem in Python
Solve the smallest value in Python (equivalent to paiza rank D)
How to handle Japanese in Python
An alternative to `pause` in Python
What to do when the value type is ambiguous in Python?
How to get a value from a parameter store in lambda (using python)
[Introduction to Python] How to use class in Python?
Install Pyaudio to play wave in python
How to access environment variables in Python
I tried to implement permutation in Python
[Introduction to Udemy Python3 + Application] 25. Dictionary-type method
Convert Python> two value sequence to dictionary
[Introduction to Udemy Python3 + Application] 13. Character method
How to dynamically define variables in Python
How to do R chartr () in Python
Pin current directory to script directory in Python
[Itertools.permutations] How to put permutations in Python
PUT gzip directly to S3 in Python
Electron Microscopy Simulation in Python: Multislice Method (1)