Custom Colormap

2024/10/12 22:29:56

I want to plot a heatmap with a custom colormap similar to this one, although not exactly.

enter image description here

I'd like to have a colormap that goes like this. In the interval [-0.6, 0.6] the color is light grey. Above 0.6, the color red intensifies. Below -0.6 another color, say blue, intensifies.

How can I create such a colormap using python and matplotlib?

What I have so far: In seaborn there is the command seaborn.diverging_palette(220, 10, as_cmap=True) which produces a colormap going from blue-light grey-red. But there is still no gap from [-0.6, 0.6].

enter image description here

Answer

Colormaps are normalized in the 0..1 range. So if your data limits are -1..1, -0.6 would be normalized to 0.2, +0.6 would be normalized to 0.8.

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colorsnorm = matplotlib.colors.Normalize(-1,1)
colors = [[norm(-1.0), "darkblue"],[norm(-0.6), "lightgrey"],[norm( 0.6), "lightgrey"],[norm( 1.0), "red"]]cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", colors)fig, ax=plt.subplots()
x = np.arange(10)
y = np.linspace(-1,1,10)
sc = ax.scatter(x,y, c=y, norm=norm, cmap=cmap)
fig.colorbar(sc, orientation="horizontal")
plt.show()

enter image description here

https://en.xdnf.cn/q/69601.html

Related Q&A

Whats the point of @staticmethod in Python?

Ive developed this short test/example code, in order to understand better how static methods work in Python.class TestClass:def __init__(self, size):self.size = sizedef instance(self):print("regul…

logical or on list of pandas masks

I have a list of boolean masks obtained by applying different search criteria to a dataframe. Here is an example list containing 4 masks: mask_list = [mask1, mask2, mask3, mask4]I would like to find th…

How to view the implementation of pythons built-in functions in pycharm?

When I try to view the built-in function all() in PyCharm, I could just see "pass" in the function body. How to view the actual implementation so that I could know what exactly the built-in f…

How to gracefully fallback to `NaN` value while reading integers from a CSV with Pandas?

While using read_csv with Pandas, if i want a given column to be converted to a type, a malformed value will interrupt the whole operation, without an indication about the offending value.For example, …

Python - object layout

can somebody describe the following exception? What is the "object layout" and how it is defined? ThanksTraceback (most recent call last):File "test_gui.py", line 5, in <module…

Using Tor proxy with scrapy

I need help setting up Tor in Ubuntu and to use it within scrapy framework.I did some research and found out this guide:class RetryChangeProxyMiddleware(RetryMiddleware):def _retry(self, request, reaso…

Best practice for structuring module exceptions in Python3

Suppose I have a project with a folder structure like so./project__init__.pymain.py/__helpers__init__.pyhelpers.py...The module helpers.py defines some exception and contains some method that raises th…

How can you read a gzipped parquet file in Python

I need to open a gzipped file, that has a parquet file inside with some data. I am having so much trouble trying to print/read what is inside the file. I tried the following: with gzip.open("myFil…

Pandas - combine row dates with column times

I have a dataframe:Date 0:15 0:30 0:45 ... 23:15 23:30 23:45 24:00 2004-05-01 3.74618 3.58507 3.30998 ... 2.97236 2.92008 2.80101 2.6067 2004-05-02 3.09098 3.846…

How to extract tables in Images

I wanted to extract tables from images.This python module https://pypi.org/project/ExtractTable/ with their website https://www.extracttable.com/pro.html doing the job very well but they have limited f…