Is it possible to ignore Matplotlib first default color for plotting?

2024/10/14 11:17:41

Matplotlib plots each column of my matrix a with 4 columns by blue, yellow, green, red. enter image description here

Then, I plot only the second, third, fourth columns from matrix a[:,1:4]. Is it possible to make Matplotlib ignore blue from default and start from yellow (so my every lines get the same color as previous)? enter image description here

a = np.cumsum(np.cumsum(np.random.randn(7,4), axis=0), axis=1)lab = np.array(["A","B","C","E"])fig, ax = plt.subplots()
ax.plot(a)
ax.legend(labels=lab )
# plt.show()
fig, ax = plt.subplots()
ax.plot(a[:,1:4])
ax.legend(labels=lab[1:4])
plt.show()
Answer

The colors used for the successive lines are the one from a color cycler. In order to skip a color in this color cycle, you may call

ax._get_lines.prop_cycler.next()  # python 2
next(ax._get_lines.prop_cycler)   # python 2 or 3

The complete example would look like:

import numpy as np
import matplotlib.pyplot as plta = np.cumsum(np.cumsum(np.random.randn(7,4), axis=0), axis=1)
lab = np.array(["A","B","C","E"])fig, ax = plt.subplots()
ax.plot(a)
ax.legend(labels=lab )fig, ax = plt.subplots()
# skip first color
next(ax._get_lines.prop_cycler)
ax.plot(a[:,1:4])
ax.legend(labels=lab[1:4])
plt.show()
https://en.xdnf.cn/q/69419.html

Related Q&A

`sock.recv()` returns empty string when connection is dead on non-blocking socket

I have a non-blocking socket in Python called sock. According to my understanding the recv() method should raise an exception if the connection has been closed by the peer, but it returns an empty stri…

Iteration order with pandas groupby on a pre-sorted DataFrame

The SituationIm classifying the rows in a DataFrame using a certain classifier based on the values in a particular column. My goal is to append the results to one new column or another depending on cer…

How do I pass an exception between threads in python

I need to pass exceptions across a thread boundary.Im using python embedded in a non thread safe app which has one thread safe call, post_event(callable), which calls callable from its main thread.I am…

How to check if a docker instance is running?

I am using Python to start docker instances.How can I identify if they are running? I can pretty easily use docker ps from terminal like:docker ps | grep myimagenameand if this returns anything, the i…

Retrieving facets and point from VTK file in python

I have a vtk file containing a 3d model,I would like to extract the point coordinates and the facets.Here is a minimal working example:import vtk import numpy from vtk.util.numpy_support import vtk_to_…

Tensorflow: feed dict error: You must feed a value for placeholder tensor

I have one bug I cannot find out the reason. Here is the code:with tf.Graph().as_default():global_step = tf.Variable(0, trainable=False)images = tf.placeholder(tf.float32, shape = [FLAGS.batch_size,33,…

Pass many pieces of data from Python to C program

I have a Python script and a C program and I need to pass large quantities of data from Python script that call many times the C program. Right now I let the user choose between passing them with an AS…

Parse JavaScript to instrument code

I need to split a JavaScript file into single instructions. For examplea = 2; foo() function bar() {b = 5;print("spam"); }has to be separated into three instructions. (assignment, function ca…

Converting all files (.jpg to .png) from a directory in Python

Im trying to convert all files from a directory from .jpg to .png. The name should remain the same, just the format would change.Ive been doing some researches and came to this:from PIL import Image im…

AssertionError: Gaps in blk ref_locs when unstack() dataframe

I am trying to unstack() data in a Pandas dataframe, but I keep getting this error, and Im not sure why. Here is my code so far with a sample of my data. My attempt to fix it was to remove all rows whe…