- Matplotlib 3.0 Cookbook
- Srinivasa Rao Poladi
- 569字
- 2025-04-04 16:06:37
How it works...
The following is the output from the code followed by an explanation of how it works:
- np.linspace(0, 10, 20) creates 20 values in the range from 0 to 20, which are mapped to the x axis.
- fig = plt.figure(figsize=(12,6)) defines the figure with a size of (12, 6), followed by six subplots using plt.subplot().
- The first plot demonstrates various ways a color can be specified in the plots. color_list is the list of colors for which line plots are drawn. The for loop that follows plot lines for each of the colors in the list. enumerate in for loop allows two variables: one for an index starting at zero, and the other for actual items in the list:
- xkcd:sky blue—This is one of the colors from the xkcd color survey. xkcd: has to precede the actual color code. Please find the full list of colors at this site (https://blog.xkcd.com/2010/05/03/color-survey-results/).
- Color codes (b, g, r, c, m, y, k, w) and their expanded names (blue, green, red, cyan, magenta, yellow, black, white).
- (tab:blue, tab:orange, tab:green, tab:red, tab:purple, tab:brown, tab:pink, tab:gray, tab:olive, tab:cyan) which are the tableau colors from T10 categorical palette (which is also the default color cycle). We can specify these colors by using tab: before them or without using tab. Both of them are acceptable.
- We can specify hexadecimal values in an RGB or an RGBA format as well. Hexadecimal values for any channel range from 00 to FF. So, RBG ranges from 000000 to FFFFFF, and RGBA ranges from 00000000 to FFFFFFFF. A stands for alpha and represents the transparency of the color; towards 00 is highly transparent and towards FF is highly opaque.
- All of these are case insensitive, so we can use uppercase or lowercase alphabets.
- One more option is CN, where C is case-sensitive and has to be an uppercase character, followed by a number, which is the index of colors in the default color cycle. As described, the first color in the default cycle is blue, followed by orange, green, red, purple, and so on. So, C0 represents blue, C1 represents orange, and so on.
- The second plot demonstrates six different line styles available. They are dash (-), solid line (--), dash, and dot (-.), colon (:), dot (.), and solid (' '). The last solid line is used to demonstrate the steps style available with the set property function, plt.setp(). This option is not available directly like other line styles.
- The third plot demonstrates various markers. Not all the available markers are plotted here. For a complete list of markers, please refer to the Matplotlib documentation here (https://matplotlib.org/api/markers_api.html?highlight=list%20markers).
- The fourth plot demonstrates how we can specify a combination of color, line style, and marker with their abbreviations or full names. Either we can specify all three parameters in a string such as g-^, we can specify even one or two of them only such as g or --, or D, in which case it uses default values for other attributes not specified here. We can also specify individually, for instance color is purple, linestyle is :, marker >, and so on.
- Plot five demonstrates how to specify the size and color parameters for the line width, marker size, edge, and face colors for markers.
- Finally, plot six demonstrates cap styles in the case of dashed line styles.