Is there a way to create a single hollow marker in a matplotlib plot?

Here is my code snippet:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand(5)
y = np.random.rand(5)

markerfacecolors = ['k','k','none','k','k']

plt.plot(x,y,'--o',markerfacecolor=markerfacecolors)

After running the above code, I encountered the following error message-

ValueError: RGBA sequence should have length 3 or 4

Can someone please guide me on how to fix this issue? Or do you know of any alternative solutions that can be used instead of splitting into multiple figures?

Answer №1

Upon reviewing the documentation for plt.plot, it is evident that the properties influencing markers include:

- marker: marker style string
- markeredgecolor or mec: color
- markeredgewidth or mew: float
- markerfacecolor or mfc: color
- markerfacecoloralt or mfcalt: color
- markersize or ms: float
- markevery: None or int or (int, int) or slice or list[int] or float or (float, float) or list[bool]

It seems there is no direct way to apply custom styles to individual markers. For instance, using markerfacecolor=markerfacecolors is incorrect as markerfacecolor expects a single color value.

To work around this limitation, multiple plot commands can be used with masking of specific values. Here's an example:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

x = np.random.rand(5)
y = np.random.rand(5)

mask1 = np.ones_like(x, dtype=bool)
mask1[3] = False
mask2 = np.logical_not(mask1)

# Using Tab10 as default color loop in Matplotlib
color = cm.tab10.colors[0]

plt.figure()
plt.plot(x,y,'--')
plt.plot(np.ma.masked_array(x, mask2),y,'o', color=color)
plt.plot(np.ma.masked_array(x, mask1),y,'o', color=color, markerfacecolor='none')

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

Python encountering errors while attempting to load JSON file

I have a text file containing the following json: { "data sources" : [ "http://www.gcmap.com/" ] , "metros" : [ { "code" : "SCL" , "name" : "Santiago" , "country" : "CL" , "continent" : "South America" , "timezone" : -4 , "coordinates" : {"S" : 33, "W" : ...

Upon attempting to import Numpy, an AttributeError is raised: 'module' object does not possess the attribute 'core'

import numpy as np File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/__init__.py", line 180, in <module> from . import add_newdocs File "/home/anirrudh/.virtualenvs/ml4t2/local/lib/python2.7/site-packages/numpy/a ...

Getting the stack with Python selenium webdriver

I tried running this code snippet to test Selenium, but encountered an issue where the Firefox window did not open and no error messages were displayed. The print statement was also not executed. from selenium import webdriver driver = webdriver.Firefox ...

Python SocketTypeError: troubleshooting socket issues

import socket, sys, string if len(sys.argv) !=4: print ("Please provide the server, port, and channel as arguments: ./ninjabot.py <server> <port> <channel>") sys.exit(1) irc = sys.argv[1] port = int(sys.argv[2]) chan = sys.argv ...

Tips for integrating the Brave browser with Selenium on Catalina

I came across this example for using the brave browser on Windows. I'm wondering if it would also work on Catalina just by updating the driver_path and brave_path? Additionally, as Chromedriver is specifically for Chrome, how can one determine the ap ...

Exploring websites with Python's mechanize library by utilizing __doPostBack functions for

Is it possible to navigate a table on a web page using mechanize if the table uses __doPostBack functions? Here is my code snippet: import mechanize br = mechanize.Browser() br.set_handle_robots(False) br.addheaders = [('User-agent', 'Mozi ...

Typing in a tkinter entry box effortlessly without the need to first click on it

Every time I launch my tkinter program, I find myself having to click on the entry box before I can start typing. Is there a way to configure it so that I can type without the need to click on the entry box? Thanks in advance. ...

Combining login information from a successful state using selenium with a Requests session - a guide

Indeed, selenium can fulfill my current requirement, but it comes with certain limitations: it is slow and necessitates the opening of a real browser on the user's machine. My approach involves using selenium to log in and fetch some state data, whic ...

My code is in dire need of some TLC - it's extremely delicate

My Python skills are still in the early stages and I've been tasked with creating a simple game similar to 'Higher or Lower' for an assignment. I'm currently working on preventing the user from crashing the game by entering invalid inp ...

Unable to establish TCP port connection from an external host

Seeking assistance desperately! I've been grappling with this issue for hours now. I'm at my wit's end, exhausted from scouring every possible resource with still no solution in sight. The predicament I find myself in involves a Python scri ...

Is it possible for two functions to share access to an object variable in Python?

In Python, it is known that a class can have both a class variable (shared among all objects) and a unique object variable (specific to each individual object). However, there seems to be an issue when attempting to access this object's variable from ...

Establishing live video streaming with OpenCV via sockets in Python 3

I am currently working on a project to develop a simple application that can live stream video using Python 3 and OpenCV over a socket. I have limited experience with both OpenCV and socket programming, so any detailed answers would be greatly appreciated. ...

Employing recursive methods to determine the maximum number of nodes within a base 7 tree

Seeking help for the first time on this platform. I've been teaching myself Python and am interested in learning how to solve a particular problem using recursion. In our company, each employee can have up to 7 reports. Given a depth of x within the ...

Load user information for the current logged in user using Django

Greetings fellow Django beginners! I know this question has been asked before, but I'm having trouble finding a solution that fits my specific situation. Here's the issue: I have a Form with a choice field that gets its options from the databas ...

Personalize the JSON formatting in a Django Rest Framework GET request

I'm having an issue with retrieving a filtered list from my MySQL database. The query seems correct, but the JSON output is not as expected: [ { "id": "0038", "name": "Jane Doe", "total_hrs_pe ...

How can I ensure that every column in a matrix contains only a single value when working with python?

This particular array consists of 6 columns. u = np.array([[1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 0, 0]]) I am in search of a method to ensure that each column within the matrix c ...

Transforming $1 billion into numerical form

After downloading a CSV file from the Nasdaq website containing various columns (such as Ticker, MarketCap...), I utilized the read_csv function from pandas to create a dataframe. The issue lies in my inability to convert the format of the MarketCap column ...

Combining Django's CSRF token with AngularJS

I currently have Django running on an Apache server with mod_wsgi, and an AngularJS app being served directly by Apache rather than through Django. My goal is to make POST calls to the Django server that is utilizing rest_framework, but I am encountering d ...

The code is stuck in an infinite loop while attempting to convert .jp2 files to .jpg

I need help converting .jp2 files in a specific folder to .jpg format using Python scripting. import os import pyvips #Specifying the directory directory = 'D:\Anaconda\data' # Loop through all files in the directory for filename in ...

What could be the reason for the inconsistent behavior of onClick, causing it to occasionally fail to display the associated

I just started using typescript 2 days ago, mainly to create a custom component for my streamlit app. I've made a navigation bar with a tab that can be clicked on the sidebar, but it's displaying some erratic behavior. Sometimes when I click on t ...