1

I need a red line, but the following code part doesn't work:

import matplotlib
matplotlib.rc("lines", marker="x", linewidth=5, color="r")
import pylab
pylab.plot([1,2,3])
pylab.show()

Line is not red.

marker and linewidth changed, but not color.

Used environment:

  • Operation system: Windows
  • IDE: PyCharm.
  • Python version: Python3.5 with Anaconda3

2 Answers 2

2

That's not really how matplotlib.rc should be used. It's more global configuration. I also don't think color alone is a valid parameter.

For just a single plot, do this:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6], linestyle='-', color='r', linewidth=2)
fig.savefig('plot_with_red_line.png', dpi=100)

Also, don't use the pylab interface. Use pyplot.

Sign up to request clarification or add additional context in comments.

1 Comment

Why do you say that's not how rc should be used? This is actually how it's used in the documentation. And notably, the example there doesn't change the color either.
1

By default, colors are specified by a Cycler object. Manually specifying a color in the pyplot.plot() (or axes.Axes.plot()) command (see Paul's answer) will change the color for a single plot. If you want to change the default color for all line plots, read on.

I'll start with an example derived from the matplotlib documentation:

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

plt.ion()  # Interactive mode on.
data = np.random.randn(50)

# Want to change default line color to cyan.
mpl.rc('lines', linewidth=4, color='c')

# Plot; linewidth changes, color doesn't.
plt.plot(data)

Color is still <code>'tab:blue'</code>, not cyan.

The color didn't change to cyan as desired.

Now, I noticed the following line at the bottom of the page in the sample matplotlibrc file:

#lines.color : C0 ## has no affect on plot(); see axes.prop_cycle

It turns out that in order to cycle through colors in default matplotlib plots, a Cycler is assigned to axes.Axes objects. We need to provide a different cycler with the color property we want. Continuing from the previous example...

plt.close()

from cycler import cycler

custom_cycler = (cycler(color=['c', 'm', 'y', 'k']) + cycler(linewidth=[1, 2, 3, 4]))
mpl.rc('axes', prop_cycle=custom_cycler)

for i in range(5):
    plt.plot(data + i)

Cycler has changed axes properties.

Woohoo! We changed the default color cycle as desired and learned about Cyclers in the process, which can cycle through other properties like linewidth too. Of course, if you want all plots to be of one color, just provide a list containing a single value to the Cycler.


NOTE: It's also possible to change the property cycler for an axes.Axes instance via axes.Axes.set_prop_cycle().

NOTE 2: As Paul said, don't use pylab; use pyplot. pylab is deprecated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.