Creating a scatter plot with separate data points and exploring the method to use a colormap for colorizing the points

My aim with matplotlib is to create a scatter plot where both markers and colors vary.

import matplotlib.pyplot as plt
import numpy as np

markers = np.array(["1","2","3","4","+",">"])

# Sample Data
x = np.array([0, 2, 3, 4, 0, 1, 5, 6, 7, 8])
y = np.array([0, 1, 0, 2, 3, 4, 5, 6, 7, 8])
c = np.array([0, 0.4, 0, 0.2, 1, 0.9, 1, 1, 1])
m = np.array([0, 0, 0, 1, 1, 1, 2, 3, 3])

# Figure
fig, ax = plt.subplots(figsize=(8,7))

# Scatter
ax.scatter(x=x, y=y, c=c, marker=markers[m], cmap = 'summer') # Not possible

Unfortunately, matplotlib doesn't currently support using an array of markers.

As a workaround, I have resorted to iterating over points, but the color remains unchanged.

# Works well for markers but all points have the same color.
for i in range(x.shape[0]):
    ax.scatter(x=x[i], y=y[i], c=c[i], marker=markers[m[i]], cmap = 'summer')

I am not experienced in manipulating ColorMap instances and couldn't find suitable examples for my specific needs.

If feasible, having a colormap would be beneficial.

Answer №1

To incorporate the color array c into the colormap cmap, utilize the ax.scatter function for each point within your second block of code. Create a new colormap named newcmp with custom arguments vmin and vmax to properly map the colors.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from matplotlib.colors import ListedColormap

markers = np.array(["1", "2", "3", "4", "+", ">"])

# Generating Dummy Data
x = np.array([0, 2, 3, 4, 0, 1, 5, 6, 7, 8])
y = np.array([0, 1, 0, 2, 3, 4, 5, 6, 7, 8])
c = np.array([0, 0.4, 0, 0.2, 1, 0.8, 0.9, 1, 1, 1])
m = np.array([0, 0, 0, 1, 1, 2, 3, 4, 5, 3])

# Creating Figure
fig, ax = plt.subplots(figsize=(8,7))

hsv_modified = cm.get_cmap('hsv', 500)
newcmp = ListedColormap(hsv_modified(np.linspace(0.0, 0.3, 500)))

for xi,yi,mi,ci in zip(x,y,m,c):
    ax.scatter(xi, yi, c=ci, marker=markers[mi], s=200, cmap=newcmp, vmin=min(c), vmax=max(c))

plt.show()

https://i.stack.imgur.com/TtFa8.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Efficient method for reading intricate data structures from disk in Python

My CSV dataset contains strings in a single field that represent lists of values. The sequences vary greatly in length, ranging from one observation to thousands. I am currently parsing these strings into nested lists within a Pandas DataFrame. Although th ...

Matplotlib has a tendency to add zeroes after the seconds when plotting time

My current plot can be seen below: https://i.stack.imgur.com/MFxai.png When I import 'time' strings like 08:12:46, I want to remove the ending zeros. However, I'm having trouble figuring out the solution. Additionally, is there a way to di ...

I am having trouble running a Python script within an Express application

I have a Python script that turns on an LED on my Raspberry Pi, and I want to execute it when a button is clicked on my Express app. I've tested the script and it works fine, but for some reason, when I try to run it from the server, it doesn't w ...

What is the best way to prevent headers from being duplicated within a loop in a Python dataframe?

Background: I recently started working with Python, and even though my code may not be perfect due to my limited experience with the language, it is functional. Currently, I am extracting data from a multi-page table. To do this, I have implemented a whil ...

Guide to implementing a rolling window in pandas with an additional criterion

I currently have a DataFrame consisting of 2 columns df = pd.DataFrame(np.random.randint(0,100,size=(100, 2)), columns=list('AB')) A B 0 11 10 1 61 30 2 24 54 3 47 52 4 72 42 ... ... ... 95 61 2 96 67 41 97 95 30 98 ...

Converting HDF5 data from MATLAB to a Pandas DataFrame using Python

I am facing an issue while trying to load .mat files with HDF5 data into Python as a Pandas DataFrame. Here is my current code: f2 = h5py.File("file.mat") f2['data'] Upon running this, I get the following output: <HDF5 dataset "data": shape ...

Analyzing GC log files with python programming language

Recently started learning Python with the goal of creating tools to streamline daily tasks. I have a log file that contains heap memory details at specific time stamps. For example: ####<2020/04/21 00:00:00.977 +0200> <XYZ> <123> <14 ...

How can I generate a PDF file directly from byte data in Python without having to write it to a binary file first?

This Python code retrieves data from a database table and converts it into a PDF file without saving it to disk, then displays it in the browser. l_statement = "select srno,attachment from dbo.attachments where srno = 1" sqlcursor.execute(l_stat ...

Selenium web scraping yielding different results than displayed on the user interface

Last week, User @KunduK was generous enough to assist me in scraping a website to retrieve the address of a specific record. The record in question can be found at this link: We used the following code snippet: address=WebDriverWait(driver,10).until(EC.vi ...

generate various instances in model serializer generate function

I have created a DRF API View in my Django project to manage Withdraw records. Below is the implementation: class WithdrawListCreateAPIView(PartnerAware, WithdrawQuerySetViewMixin, generics.ListCreateAPIView): permission_classes = (TokenMatchesOASRequi ...

How to differentiate specific points in a Plotly Express Scatterplot using various colors

At the moment, I have a scatterplot showcasing different directors based on their production budget and profit. I am looking to pick out specific directors by highlighting their points with unique colors and creating a legend identifying each one. For ins ...

Setting up Python ARM in a Docker environment can be quite time-consuming due to the

Looking to launch a Docker container containing Python bots that require libraries like pandas, numpy, and matplotlib. While everything runs smoothly in an x86 environment, the process of building the image on an Arm environment is significantly slower. I ...

Learn the steps to utilize pywinauto and selenium for logging into any account

I attempted to create a script for logging in using pywinauto and selenium, but it failed. I searched for a solution, but it seems there isn't much information available on this topic. from pywinauto import application from selenium import webdriver ...

Struggling with Python version selection when creating egg files in Eclipse for Python development

My current setup involves using CentOS with Python 2.6 (/usr/bin/python2.6), but I have also installed Python 2.7.8 (/usr/local/lib/python2.7). However, when running a script in Eclipse, the egg files are being created in /usr/bin/python2.6/.. instead of ...

Pandas does not have the capability to interpret Excel data as plain text

Currently, I am attempting to iterate through a series of xls files in a loop and combine them into one master dataframe. While each file contains the same columns, some have a column as a string while others have it as an integer. To avoid any potential ...

Securing a worksheet in Excel with Openyxl and tkinter

In my Python project, I am attempting to secure an Excel sheet using Openyxl. After experimenting with various methods, I have not been successful. The objective is to enable data entry for users to input information and then view it in Excel without the a ...

Python Query Azure Table Storage entities by date and time

I am attempting to execute a query to retrieve entities from an Azure table based on the Timestamp property. 1) To start, I obtain the current time in UTC: currTime = datetime.utcnow().strftime("%Y-%m-1T%H:%M:%S") 2) Proceeding with several steps to cre ...

Unable to modify module variable through monkey patching in Python unit tests

I am currently testing the module: package/module.py DATA_PATH = os.path.join(os.path.dirname(__file__), "data") class SomeClass: def __init__(self): self.filename = os.path.join(DATA_PATH, "ABC.txt") within my tests in module_test.py I have ...

BeautifulSoup Pagination in Python: A Complete Guide

I am currently working on a web scraping project for the following website: I have successfully scraped the data, but I am facing challenges with pagination. My goal is to create a loop that scrapes the next page button and uses the URL from that button t ...

Is there a way to successfully run Python in a QwikLabs Linux VM using Windows PuTTY, as it currently does not work? Interestingly, it does work when using a Mac

Recently, I started a course that requires accessing a Qwiklabs Linux virtual machine via SSH to write and run Python scripts. Surprisingly, when using PuTTY on Windows 10, everything works smoothly until it's time to execute the script - then I encou ...