Conditional color with matplotlib scatter

2024/10/9 8:28:41

I have the following Pandas Dataframe, where column a represents a dummy variable:

Panda Dataframe

What I would like to do is to give my markers a cmap='jet' color following the value of column b, except when the value in column a is equal to 1 - in this case I want it to be the color grey.

Any idea how I can do this?

Answer

You have to mark your values which are equal to one and plot:

import matplotlib.pyplot as plt
import numpy as np# test data
t = np.linspace(0, 2 * np.pi, 30)
x = np.sin(t)
x[3] = 1
y = np.cos(t)# indices for 'bad' values
indices = x == 1
# calc colors from jet cmap
cmap = plt.get_cmap('jet')
colors = cmap((y - y.min()) / y.ptp())# normal values
plt.scatter(t[~indices], x[~indices], c = colors[~indices], cmap = cmap)
# bad values
plt.scatter(t[indices], x[indices], c = 'grey')
plt.show()

enter image description here

Arrays t, x, y represent pandas series.

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

Related Q&A

Cumulative sum with list comprehension

I have a list of integers:x = [3, 5, 2, 7]And I want to create a new list where the nth element is the sum of elements in x from 0 to n-1.This would result in:y = [0, 3, 8, 10]How can I do this with li…

How to get image size in KB while using Pillow-python before storing to disk?

Im using Pillow for image processing in Python,url="http://www.image.com/some_image.jpg";path = io.BytesIO(urllib.request.urlopen(url).read())original_image = Image.open(path)Any idea how i c…

Python: Print only one time inside a loop

I have a code where I want capture a video from a camera. I want to use Logging library of Python to get messages on the shell or export them to a text file.Here is a part of my code where inside the w…

Calling Different Functions in Python Based on Values in a List

I have a script that takes a list of metrics as an input, and then fetches those metrics from the database to perform various operations with them. My problem is that different clients get different s…

takes 1 positional argument but 2 were given

I would like to run a command line tool to run in a separate function and passed to the button click the additional command for this program but each time I get this as a response.takes 1 positional ar…

With py.test, database is not reset after LiveServerTestCase

I have a number of Django tests and typically run them using py.test. I recently added a new test case in a new file test_selenium.py. This Test Case has uses the LiveServerTestCase and StaticLiveSer…

Using flask wtforms validators without using a form

Im receiving user registration data from an iOS application and Id like to use the validators that come with wtforms to make sure the email and password are acceptable. However, Im not using a flask f…

How to install graph-tool for Anaconda Python 3.5 on linux-64?

Im trying to install graph-tool for Anaconda Python 3.5 on Ubuntu 14.04 (x64), but it turns out thats a real trick.I tried this approach, but run into the problem:The following specifications were foun…

How to quickly encrypt a password string in Django without an User Model?

Based on my current Django app settings, is there a function or a snippet that allows me to view the encrypted password, given a raw string? I am testing some functionality and this would be useful fo…

Embedding multiple gridspec layouts on a single matplotlib figure?

I am using the python graphing library matplotlib to graph several things in a report, and I found myself needing to have several fixed-count graphs above an arbitrary grid of smaller graphs. I search…