First we define a helper function for making a table of colors, then we use it on some common color categories. Matplotlib supports colors from the xkcd color survey, e.g. "xkcd:sky blue". Since this contains almost 1000 colors, a figure of this would be very large and is thus omitted here. You can use the following code to generate the overview yourself The use of the following functions, methods, classes and modules is shown in this example: Total running time of the script: (0 minutes 1.571 seconds)
Learn more:First we define a helper function for making a table of colors, then we use it on some common color categories. Matplotlib supports colors from the xkcd color survey, e.g. "xkcd:sky blue". Since this contains almost 1000 colors, a figure of this would be very large and is thus omitted here. You can use the following code to generate the overview yourself The use of the following functions, methods, classes and modules is shown in this example: Total running time of the script: (0 minutes 1.571 seconds)
matplotlib.org/stable/gallery/color/named_colors.htmlMatplotlib recognizes the following formats to specify a color. RGB or RGBA (red, green, blue, alpha) tuple of float values in a closed interval [0, 1]. Case-insensitive hex RGB or RGBA string. Case-insensitive RGB or RGBA string equivalent hex shorthand of duplicated characters. String representation of float value in closed interval [0, 1] for grayscale values. Single character shorthand notation for some basic colors. The colors green, cyan, magenta, and yellow do not coincide with X11/CSS4 colors. Their particular shades were chosen for better visibility of colored lines against typical backgrounds. Case-insensitive X11/CSS4 color name with no spaces. Case-insensitive color name from xkcd color survey with 'xkcd:' prefix. Case-insensitive Tableau Colors from 'T10' categorical palette. This is the default color cycle. "CN" color spec where 'C' precedes a number acting as an index into the default property cycle. Matplotlib indexes color at draw time and defaults to black if cycle does not include color. Tuple of one of the above color formats and an alpha float. Added in version 3.8. The following links provide more information on colors in Matplotlib. "Red", "Green", and "Blue" are the intensities of those colors. In combination, they represent the colorspace. Matplotlib converts "CN" colors to RGBA when drawing Artists. The Styling with cycler section contains additional information about controlling colors and style properties. The first color 'C0' is the title. Each plot uses the second and third colors of each style's rcParams["axes.prop_cycle"] (default: cycler('color', ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf'])). They are 'C1' and 'C2', respectively. The xkcd colors come from a user survey conducted by the webcomic xkcd. 95 out of the 148 X11/CSS4 color names also appear in the xkcd color survey. Almost all of them map to different color values in the X11/CSS4 and in the xkcd palette. Only 'black', 'white' and 'cyan' are identical. For example, 'blue' maps to '#0000FF' whereas 'xkcd:blue' maps to '#0343DF'. Due to these name collisions, all xkcd colors have the 'xkcd:' prefix. The visual below shows name collisions. Color names where color values agree are in bold. Total running time of the script: (0 minutes 1.573 seconds)
matplotlib.org/stable/users/explain/colors/colors.htmlSuppose I have a for loop and I want to plot points in different colors: plt.plot(x,y,col=i) How do I automatically change colors in the for loop? IMO How to get different colored lines for different plots in a single figure? is about the color cycle that's restarted in each different axes in a figure. There is a possibility I'm wrong... @tcaswell already answered, but I was in the middle of typing my answer up, so I'll go ahead and post it... There are a number of different ways you could do this. To begin with, matplotlib will automatically cycle through colors. By default, it cycles through blue, green, red, cyan, magenta, yellow, black: plt.plot(x, i * x + i, label='$y = {i}x + {i}$'.format(i=i)) If you want to control which colors matplotlib cycles through, use ax.set_color_cycle: plt.plot(x, i * x + i, label='$y = {i}x + {i}$'.format(i=i)) If you'd like to explicitly specify the colors that will be used, just pass it to the color kwarg (html colors names are accepted, as are rgb tuples and hex strings): plt.plot(x, i * x + i, color=color, label='$y = {i}x + {i}$'.format(i=i)) Finally, if you'd like to automatically select a specified number of colors from an existing colormap: plt.plot(x, i * x + i, color=color, label='$y = {i}x + {i}$'.format(i=i)) Your answer is, um, drastically better than mine. Thanks!
stackoverflow.com/questions/16006572/plotting-diff…Matplotlib has a number of built-in colormaps accessible via matplotlib.colormaps. There are also external libraries that have many extra colormaps, which can be viewed in the Third-party colormaps section of the Matplotlib documentation. Here we briefly discuss how to choose between the many options. For help on creating your own colormaps, see Creating Colormaps in Matplotlib. To get a list of all registered colormaps, you can do: The idea behind choosing a good colormap is to find a good representation in 3D colorspace for your data set. The best colormap for any given data set depends on many things including: Your knowledge of the data set (e.g., is there a critical value from which the other values deviate?) For many applications, a perceptually uniform colormap is the best choice; i.e. a colormap in which equal steps in data are perceived as equal steps in the color space. Researchers have found that the human brain perceives changes in the lightness parameter as changes in the data much better than, for example, changes in hue. Therefore, colormaps which have monotonically increasing lightness through the colormap will be better interpreted by the viewer. Wonderful examples of perceptually uniform colormaps can be found in the Third-party colormaps section as well. Color can be represented in 3D space in various ways. One way to represent color is using CIELAB. In CIELAB, color space is represented by lightness, L ∗; red-green, a ∗; and yellow-blue, b ∗. The lightness parameter L ∗ can then be used to learn more about how the matplotlib colormaps will be perceived by viewers. An excellent starting resource for learning about human perception of colormaps is from [IBM]. Colormaps are often split into several categories based on their function (see, e.g., [Moreland]): Sequential: change in lightness and often saturation of color incrementally, often using a single hue; should be used for representing information that has ordering. Diverging: change in lightness and possibly saturation of two different colors that meet in the middle at an unsaturated color; should be used when the information being plotted has a critical middle value, such as topography or when the data deviates around zero. Cyclic: change in lightness of two different colors that meet in the middle and beginning/end at an unsaturated color; should be used for values that wrap around at the endpoints, such as phase angle, wind direction, or time of day. Qualitative: often are miscellaneous colors; should be used to represent information which does not have ordering or relationships. First, we'll show the range of each colormap. Note that some seem to change more "quickly" than others.
matplotlib.org/stable/users/explain/colors/colormap…I am making a scatter plot in matplotlib and need to change the background of the actual plot to black. I know how to change the face color of the plot using: My issue is that this changes the color of the space around the plot. How to I change the actual background color of the plot? Just FYI, in addition to what @Evert said, you could just use ax.patch.set_facecolor('black') (where ax is the axes instance). fig.patch is the figure background and ax.patch is the axes background. mint green is possibly the worst color you can choose for a background. I love it :D Use the set_facecolor(color) method of the axes object, which you've created one of the following ways: plt.plot(...) Then you can use set_facecolor: As a refresher for what colors can be: Matplotlib recognizes the following formats to specify a color: a “CN” color spec, i.e. 'C' followed by a single digit, which is an index into the default property cycle (matplotlib.rcParams['axes.prop_cycle']); the indexing occurs at artist creation time and defaults to black if the cycle does not include color. All string specifications of color, other than “CN”, are case-insensitive. If there's some other common way to generate axes, let me know. Looks like this method disappeared at some point over the last 3 years... MethodNotFound @Demis this method was added in recent years. See ImportanceOfBeingEarnest's answer for how to do it in older versions. One method is to manually set the default for the axis background color within your script (see Customizing matplotlib): This is in contrast to Nick T's method which changes the background color for a specific axes object. Resetting the defaults is useful if you're making multiple different plots with similar styles and don't want to keep changing different axes objects. Note: The equivalent for from your question is: Something like this? Use the axisbg keyword to subplot: (Granted, not a scatter plot, and not a black background.) I had success with plt.subplot('111', axisbg='black') before the plotting commands, using Windows. Simpler answer: If you already have axes object, just like in Nick T 's answer, you can also use ax.patch.set_facecolor('black') The easiest thing is probably to provide the color when you create the plot : or One suggestion in other answers is to use ax.set_axis_bgcolor("red"). This however is deprecated, and doesn't work on MatPlotLib >= v2.0. There is also the suggestion to use ax.patch.set_facecolor("red") (works on both MatPlotLib v1.5 & v2.2). While this works fine, an even easier solution for v2.0+ is to use FYI, ax.set_axis_bgcolor("black") works on Python v2.7.14/MPL v1.5.1, but ax.set_facecolor() does not.
stackoverflow.com/questions/14088687/how-to-cha…What named colors are available in matplotlib for use in plots? I can find a list on the matplotlib documentation that claims that these are the only names: However, I've found that these colors can also be used, at least in this context: but these are not on the above list. Does anyone know an exhaustive list of the named colors that are available? Basically, it's all of the HTML color names, so you can always just google "HTML colors" if you want several nice charts. @BoshWash's excellent answer below gives you the exact list, though. That is a nice picture, I probably should have noticed it. To be fair, it was first posted a month before I posted this question, and I'm pretty sure I searched through the docs many times before then for the answer to this question. I constantly forget the names of the colors I want to use and keep coming back to this question =) The previous answers are great, but I find it a bit difficult to get an overview of the available colors from the posted image. I prefer the colors to be grouped with similar colors, so I slightly tweaked the matplotlib answer that was mentioned in a comment above to get a color list sorted in columns. The order is not identical to how I would sort by eye, but I think it gives a good overview. I updated the image and code to reflect that 'rebeccapurple' has been added and the three sage colors have been moved under the 'xkcd:' prefix since I posted this answer originally. I really didn't change much from the matplotlib example, but here is the code for completeness. If you would like to use additional named colors when plotting with matplotlib, you can use the xkcd crowdsourced color names, via the 'xkcd:' prefix: Now you have access to a plethora of named colors! The default Tableau colors are available in matplotlib via the 'tab:' prefix: There are ten distinct colors: You can also plot colors by their HTML hex code: This is more similar to specifying and RGB tuple rather than a named color (apart from the fact that the hex code is passed as a string), and I will not include an image of the 16 million colors you can choose from... For more details, please refer to the matplotlib colors documentation and the source file specifying the available colors, _color_data.py. Thanks for the plot! Out of curiosity, is 'y' really different from 'yellow'? The first plot has them as different colors. @ComputerScientist Yes, according to this Github issue and the linked mailling list discussion, the single letter colors were assigned RBG values based on their Matlab counterpart, while the full name correspond to the HTML colors. Matlab single letter colors currently also follows the HTML standard, so I am not sure if that is a recent Matlab change or if the matplotlib single letter colors were tweaked/chosen for reasons such as visibility, which was also mentioned in the discussions. @AdrianTorrie: you could award a bounty of your own choosing as an additional 'Thanks' ! A bounty award super-highlights this answer, and gives answerer additional points. @joelostblom, in the plot of the xkcd colors, how did you get the hex codes to display in grey, beside the color name (in black)?
stackoverflow.com/questions/22408237/named-col…Matplotlib is a powerful visualization package for Python. It is very customizable, thanks to this it is widly used in commercial and in academic use cases. In this article, I will show you 9 different ways how to set colors in Matplotlib plots. All parts of the plot can be customized with a new color. You can set colors for axes, labels, background, title. However, not every data scientist is a graphic designer that can compose nice looking colors in a single plot, so I can show you how to use predefined Matplotlib styles to get attractive plots. 1. Define color as RGB/RGBA float touple The first method to define a color in a Matplotlib is to use RGB (red, green, blue) or RGBA (red, green, blue, alpha) touple of floats. The numbers should be in range [0, 1]. Each number in the touple controls how many of base color will be in final color. For example (1, 0, 0) is red color because there is 0 of green and blue. The alpha parameter in RGBA controls transparency. It is useful when we have overlaping elements in the plot. Below is a minimal example of how to set color in the plot. We simply pass color parameter in the plot function with touple: Plot with several colors is presented below. RGB colors are randomly generated. Here is an example of histograms with overlaping distributions, we use alpha parameter to visualize them: 2. Matplotlib color as RGB/RGBA hex The RGB and RGBA colors can be defined as hex strings, similar to CSS style sheet language. The minimum value is 00 and maximum value is FF. You need to start string with #. 3. Shorthand RGB/RGBA hex The hex string can be shorted if there are the duplicate values for each channel. For example #11aa00 can be passed as #1a0 and #11aa0055 can be passed as #1a05: Please remember, that if you are using shorthand hex, then it should be applied to all channels.
mljar.com/blog/matplotlib-colors/Matplotlib is a widely used plotting library in Python. One of the most important aspects of creating visually appealing plots is the ability to control colors. Colors can be used to distinguish different data series, highlight specific regions, or add aesthetic value to the plot. Understanding how to work with colors in Matplotlib is essential for data visualization tasks. This blog post will explore the fundamental concepts, usage methods, common practices, and best practices related to Python Matplotlib colors. 2. Table of Contents 3. Fundamental Concepts of Matplotlib Colors Matplotlib supports several ways to represent colors: - Named Colors: Matplotlib has a set of named colors. For example, 'red', 'blue', 'green' etc. are well-known named colors. You can use these names directly in plot commands. RGB (Red, Green, Blue) Values: Colors can be represented as a tuple of three values between 0 and 1. For example, (0, 0, 1) represents blue (since red and green are 0, and blue is at full intensity). Hexadecimal Values: Similar to web development, hexadecimal color codes can be used. For example, '#FF0000' represents red. Color maps (colormaps) are used to map data values to colors. Matplotlib comes with a wide range of built-in colormaps. Some common ones include: - viridis: A perceptually uniform colormap, great for general data visualization. - jet: An older but still widely used colormap, although it has some issues with color perception. - hot: Goes from black to red, orange, and yellow, useful for representing heat or intensity. To use a colormap, you typically need to map your data values to a range and then use the colormap to assign colors based on those values. 4. Usage Methods Most plot functions in Matplotlib have a color parameter. For example, in plt.plot() for line plots and plt.scatter() for scatter plots: Colors can be used to distinguish different data series, highlight specific regions, or add aesthetic value to the plot. Understanding how to work with colors in Matplotlib is essential for data visualization tasks. This blog post will explore the fundamental concepts, usage methods, common practices, and best practices related to Python Matplotlib colors.
coderivers.org/blog/python-matplotlib-colors/Customizing Matplotlib colors is more than just choosing your favorite color for a plot. Colors play a crucial role in communicating your data’s message. Why is this plot so confusing… …while this one is less? The answer is color! Colors play a key role in the visual appeal of plots, their readability, and the efficiency of transmitting your data’s message. Because of its importance, all the respectable data visualization tools offer extensive options for color customization. The same is true with Matplotlib, a highly popular data visualization library in Python. In this guide, I’ll show you how to improve your Matplotlib plots with customized colors, from basic tweaks to advanced techniques. Before customizing colors, let’s make sure that you understand how Matplotlib handles colors. Matplotlib has a default color cycle and several ways of specifying colors. Matplotlib automatically applies colors from the default color cycle to plot lines and other plot elements. These colors repeat if the number of plotted elements exceeds the cycle length. You can customize this cycle for consistent themes. Colors in Matplotlib can be defined in multiple ways. Most commonly, we use one of these approaches: Hex RGB strings: You can also specify colors using the hexadecimal codes, e.g., #FF0000 for 'red'. RGB tuples: Colors are also commonly defined using RGB (red, green, blue) tuples with values between 0 and 1. For example, (1, 0, 0) is pure red. Here is a list of the standard and extended HTML colors, along with the hex RGB strings and tuples for each color. The matplotlib.colors module has utilities for converting between color formats and creating custom color schemes. You will use it when you want to go beyond predefined color options. The dictionary-like configuration system in Matplotlib, rcParams, is used for dynamically setting the default rc (runtime configuration) color schemes and other properties. You can use it to apply color customizations globally across your plots. It’s quite easy to customize colors for different plot types in Matplotlib. This is done by specifying parameters like color (for line, bar, and area plots) or c (for scatter plots and other marker-based plots). While these parameters are similar, they are still slightly different in terms of what type of color specifications they accept. The color parameter accepts all four color specifications we mentioned in the Color Specifications section, as well as a list of colors (for multiple lines or bars).
www.stratascratch.com/blog/how-to-customize-mat…There are four main groups of named colors in matplotlib: the default Tableau 10 Palette, 8 single character "base" colors, CSS colors, and all the colors from the xkcd survey. Their names and RGB tuples or HTML hex codes are available in dictionaries in the colors module: Colors in the Tableau palette must be prefaced with "tab:" A = 1. for color in mcolors. TABLEAU_COLORS: Similarly, xkcd colors must be prefaced with "xkcd:" All named colors are in ⎯𝚌𝚘𝚕𝚘𝚛𝚜⎯𝚏𝚞𝚕𝚕⎯𝚖𝚊𝚙 . You can check if a color you are thinking of will be recognized by matplotlib by searching in there: If you would like to peruse, just run this code: #recall that xkcd colors must be prefaced with "xkdc:". To save space, I'll take that out You can also pass in RGB tuples and HTML hex codes. The former must be values between 0 and 1 (divide by 255 if you have a RBG tuple not in that interval), and the latter has to be a string. If you search "color picker" in Google, the search engine will provide you an interactive tool with color sliders that provides RGB and HTML hex codes, of which there are 16 million different possible values. To get the RGB tuple or HTML hex code of a color you know the name of, you can use the 𝚝𝚘⎯𝚛𝚐𝚋 and 𝚝𝚘⎯𝚑𝚎𝚡 functions: ColorbarBase (ax, cmap=color_map, orientation = 'horizontal') This function will plot the 82 colormaps and their reverse. mpl. colorbar. ColorbarBase (ax, cmap=color_map, orientation = 'horizontal') There are four main types of colormap: sequential colormaps increase incrementally in brightness or hue. This is useful for representing data in the which the order matters: return np.exp (-np.power (x - mu, 2.) / (2 * np.power (sig, 2.))) The perceptually uniform sequential colormaps (viridis, plasma, inferno, magma, and cividis) are generally the more accessible colormaps for those with color vision deficiency. Diverging colormaps have two colors that also change in brightess and hue to meet at some neutral color in the middle. These are good to show change from some value of interest. Cyclic colormaps come back to meet each other at each end, which is good for values that repeat or come back onto themselves. Qualitative colormaps don't have specific ordering and so are better suited for data sets in which the order doesn't matter, or you can use them to choose a list of colors (which we'll get to in a later section). As I've shown above, colormaps don't only have use in imshow: you can use the color to plot a third variable in scatter plots: We can also use colormaps for some third variable in line plots. Using a line collection will make plotting several related line plots both easy and efficient:
petercbsmith.github.io/color-tutorial.htmlList of named colors — Matplotlib 3.10.1 documentation
First we define a helper function for making a table of colors, then we use it on some common color categories. Matplotlib supports colors from the xkcd color survey, e.g. "xkcd:sky blue". …
See results only from matplotlib.orgSpecifying colors
Matplotlib recognizes the following formats to specify a color. RGB or RGBA (red, green, blue, alpha) tuple of float values in a closed interval [0…
Specifying colors — Matplotlib 3.10.1 documentation
Matplotlib recognizes the following formats to specify a color. RGB or RGBA (red, green, blue, alpha) tuple of float values in a closed interval [0, 1]. Case-insensitive hex RGB or RGBA string. Case-insensitive RGB or RGBA string equivalent …
Plotting different colors in matplotlib - Stack Overflow
The color of individual lines (as well as the color of different plot elements, e.g., markers in scatter plots) is controlled by the color keyword argument, …
- Reviews: 1
Choosing Colormaps in Matplotlib — Matplotlib 3.10.1 …
9 ways to set colors in Matplotlib - MLJAR
Nov 21, 2022 · In this article, I will show you 9 different ways how to set colors in Matplotlib plots. All parts of the plot can be customized with a new color. You can set colors for axes, labels, background, title.
matplotlib.pyplot.plot — Matplotlib 3.10.1 documentation
Plot y versus x as lines and/or markers. Call signatures: The coordinates of the points or line nodes are given by x, y. The optional parameter fmt is a convenient way for defining basic formatting like color, marker and linestyle. It's a shortcut string …
- People also ask
Python Matplotlib Colors: A Comprehensive Guide - CodeRivers
Apr 14, 2025 · Matplotlib is a widely used plotting library in Python. One of the most important aspects of creating visually appealing plots is the ability to control colors. Colors can be used …
How to Set Plot Background Color in Matplotlib?
Jan 15, 2025 · By default, the color of the plot is white. If we have to set the background color of the plot so that our plot looks beautiful, we have to make the axes object, by using axes () attribute after plotting the graph. Approach: Set attribute set_facecolor () to the required color. This attribute accepts both name or color code of the color.
How to Customize Matplotlib Colors for Better Plots?
Feb 24, 2025 · In this guide, I’ll show you how to improve your Matplotlib plots with customized colors, from basic tweaks to advanced techniques. Before customizing colors, let’s make sure that you understand how Matplotlib …
Part 1: Named colors - GitHub Pages
In this tutorial I explain some of the different ways you can use and manipulate colors in matplotlib. You may already know that you can pass a color argument through commonly used plotting functions to set the color of your lines and …
- Some results have been removed