create an asymmetric colormap

2024/9/21 3:35:42

I am creating a colormap to map colors in a folium choropleth map, using code from here:

from branca.colormap import linearcolormap = linear.RdBu.scale(df.MyValue.min(),df.MyValue.max())colormap

my colormap

As you can see, the min and max values are skewed compared to the 0, and I'd like this to be reflected in the colormap, i.e. only values < 0 should be mapped to red, while all positive values should be mapped to blue.

Is there a way to have this asymmetry in the colormap? I haven't seen many examples on the web.

I am doing like:

colormap = colormap.to_step(index=[-200, 0, 1200])

but it's not smooth:

enter image description here

Answer

You can set the index if you want an asymmetrical Color Scale (in the documentation is called irregular). Example:

import branca.colormap as cmcolormap = cm.LinearColormap(colors=['red','blue'], index=[-200,0],vmin=-200,vmax=1200)
colormap

enter image description here

The transition will be given by your index. For example, these are other transitions:

colormap = cm.LinearColormap(colors=['red','blue'], index=[0,200],vmin=-200,vmax=1200)
colormap

enter image description here

Adding a third color:

   colormap = cm.LinearColormap(colors=['red','lightblue','blue'], index=[-200,0,1200],vmin=-200,vmax=1200)colormap

enter image description here

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

Related Q&A

NLTK - Get and Simplify List of Tags

Im using the Brown Corpus. I want some way to print out all the possible tags and their names (not just tag abbreviations). There are also quite a few tags, is there a way to simplify the tags? By sim…

PolynomialFeatures object has no attribute predict

I want to apply k-fold cross validation on the following regression models:Linear Regression Polynomial Regression Support Vector Regression Decision Tree Regression Random Forest RegressionI am able t…

Error module object has no attribute freetype

I am using this code Link but it displays error of module object has no attribute i tried to pip install freetype but nothing happened. Can anyone please guide me with this.import cv2 import numpy as …

Count total number of white pixels in an image

I am trying to count total number of white pixels in the following image:But with my code, I get this errorsrc is not a numpy array, neither a scalar.This is my code: img=cv2.imread(filename,1) TP= wid…

Pass a JSON object to an url with requests

So, I want to use Kenneth excellent requests module. Stumbled up this problem while trying to use the Freebase API.Basically, their API looks like that:https://www.googleapis.com/freebase/v1/mqlread?q…

jenkinsapi python - how to trigger and track the job result

I am using JenkinsAPI to trigger parametrized jobs. I am aware of the REST API that Jenkins use, but our setup does not allow that directly; so the main mean for me to trigger jobs is through this libr…

Django test parallel AppRegistryNotReady

I am trying to understand how to run django tests in parallel with in memory sqlite3.I have django app with that structure:gbookorder...tests__init__.pytest_a1.pytest_b1.pyutils.pytest_a1.py and test_b…

ImportError: PyCapsule_Import could not import module pyexpat

I am using Jenkins to build a python (Flask) solution to deploy to Google App Engine. As part of the build process I run a few integration tests. One of them is failing with the following error. ERROR:…

Python - Get max value in a list of dict

I have a dataset with this structure :In[17]: allIndices Out[17]: [{0: 0, 1: 1.4589, 4: 2.4879}, {0: 1.4589, 1: 0, 2: 2.1547}, {1: 2.1547, 2: 0, 3: 4.2114}, {2: 4.2114, 3: 0}, {0: 2.4879, 4: 0}]Id lik…

Rescaling axis in Matplotlib imshow under unique function call

I have written a function module that takes the argument of two variables. To plot, I hadx, y = pylab.ogrid[0.3:0.9:0.1, 0.:3.5:.5] z = np.zeros(shape=(np.shape(x)[0], np.shape(y)[1]))for i in range(le…