Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6549667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:07:16+00:00 2026-05-25T12:07:16+00:00

I am using matplotlib to create 2d line-plots. For the purposes of publication, I

  • 0

I am using matplotlib to create 2d line-plots. For the purposes of publication, I would like to have those plots in black and white (not grayscale), and I am struggling to find a non-intrusive solution for that.

Gnuplot automatically alters dashing patterns for different lines, is something similar possible with matplotlib?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:07:17+00:00Added an answer on May 25, 2026 at 12:07 pm

    Below I provide functions to convert a colored line to a black line with unique style. My quick test showed that after 7 lines, the colors repeated. If this is not the case (and I made a mistake), then a minor adjustment is needed for the “constant” COLORMAP in the provided routine.

    Here’s the routine and example:

    import matplotlib.pyplot as plt
    import numpy as np
    
    def setAxLinesBW(ax):
        """
        Take each Line2D in the axes, ax, and convert the line style to be 
        suitable for black and white viewing.
        """
        MARKERSIZE = 3
    
        COLORMAP = {
            'b': {'marker': None, 'dash': (None,None)},
            'g': {'marker': None, 'dash': [5,5]},
            'r': {'marker': None, 'dash': [5,3,1,3]},
            'c': {'marker': None, 'dash': [1,3]},
            'm': {'marker': None, 'dash': [5,2,5,2,5,10]},
            'y': {'marker': None, 'dash': [5,3,1,2,1,10]},
            'k': {'marker': 'o', 'dash': (None,None)} #[1,2,1,10]}
            }
    
    
        lines_to_adjust = ax.get_lines()
        try:
            lines_to_adjust += ax.get_legend().get_lines()
        except AttributeError:
            pass
    
        for line in lines_to_adjust:
            origColor = line.get_color()
            line.set_color('black')
            line.set_dashes(COLORMAP[origColor]['dash'])
            line.set_marker(COLORMAP[origColor]['marker'])
            line.set_markersize(MARKERSIZE)
    
    def setFigLinesBW(fig):
        """
        Take each axes in the figure, and for each line in the axes, make the
        line viewable in black and white.
        """
        for ax in fig.get_axes():
            setAxLinesBW(ax)
    
    xval = np.arange(100)*.01
    
    fig = plt.figure()
    ax = fig.add_subplot(211)
    
    ax.plot(xval,np.cos(2*np.pi*xval))
    ax.plot(xval,np.cos(3*np.pi*xval))
    ax.plot(xval,np.cos(4*np.pi*xval))
    ax.plot(xval,np.cos(5*np.pi*xval))
    ax.plot(xval,np.cos(6*np.pi*xval))
    ax.plot(xval,np.cos(7*np.pi*xval))
    ax.plot(xval,np.cos(8*np.pi*xval))
    
    ax = fig.add_subplot(212)
    ax.plot(xval,np.cos(2*np.pi*xval))
    ax.plot(xval,np.cos(3*np.pi*xval))
    ax.plot(xval,np.cos(4*np.pi*xval))
    ax.plot(xval,np.cos(5*np.pi*xval))
    ax.plot(xval,np.cos(6*np.pi*xval))
    ax.plot(xval,np.cos(7*np.pi*xval))
    ax.plot(xval,np.cos(8*np.pi*xval))
    
    fig.savefig("colorDemo.png")
    setFigLinesBW(fig)
    fig.savefig("bwDemo.png")
    

    This provides the following two plots:
    First in color:
    enter image description here
    Then in black and white:
    enter image description here

    You can adjust how each color is converted to a style. If you just want to only play with the dash style (-. vs. — vs. whatever pattern you want), set the COLORMAP corresponding ‘marker’ value to None and adjusted the ‘dash’ pattern, or vice versa.

    For example, the last color in the dictionary is ‘k’ (for black); originally I had only a dashed pattern [1,2,1,10], corresponding to one pixel shown, two not, one shown, 10 not, which is a dot-dot-space pattern. Then I commented that out, setting the dash to (None,None), a very formal way of saying solid line, and added the marker ‘o’, for circle.

    I also set a ‘constant’ MARKERSIZE, which will set the size of each marker, because I found the default size to be a little large.

    This obviously does not handle the case when your lines already have a dash or marker patter, but you can use these routines as a starting point to build a more sophisticated converter. For example if you original plot had a red solid line and a red dotted line, they both would turn into black dash-dot lines with these routines. Something to keep in mind when you use them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently using matplotlib.pyplot to create graphs and would like to have the
I would like to create a graph like this one using gnuplot (or matplotlib,
I have a working Python 2.6 code using matplotlib, and would like to get
I'm using python with matplotlib to create plots out of data, an I'd like
I am using matplotlib in a django app and would like to directly return
I would like to draw histogram using matplotlib. However, due to the huge data(a
I'm trying to create an image using matplotlib.pyplot.imshow() . However, when I run the
I am working on using Matplotlib to produce plots of implicit equations (eg. y^x=x^y).
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
I am new to using matplotlib. I am trying to create a 2D grid,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.