Plots Styles

Update: If you have matplotlib = 1.4, there is a new style module which has a ggplot style by default. To activate this, use: from matplotlib import pyplot as plt plt.style.use('ggplot') To see all the available styles, you can check plt.style.available. Plot styles are often a subject of confusion for AutoCAD users. In the real world of CAD in the workplace, each company usually has their own individual company standards, which makes it difficult to know what is the right way, or the best way, to set these up. In this post we answer some FAQs regarding plot styles. Whenever you create a plot in MATLAB, you need to identify the sources of information using more than just the lines. Creating a plot that uses differing line types and data point symbols makes the plot much easier for other people to use. The following table contains a listing of the line plot styles.

Matplotlib is both powerful and complex: being able to adjust every aspect of a plot is powerful, but it's often time-consuming and complex to create a beautiful plot. The Matplotlib 1.5 release makes it easier to achieve aesthetically pleasing results by incorporating a set of styles [1] .

In this post I'm going to cover setting up a style, demonstrate some of the different styles in action and show how it's easy to alter matplotlibs settings to suit your own tastes.

Each style creates a common look that can be easily applied to all the different plot types. They alter all the main visual aspects of the plot such as xticks, legends and labels. There are 21 styles in the Matplotlib 1.5.1 release, they can be listed by doing:

The ones with distinctive looks are:

  • seaborn-*
    This is a set of styles from the Seaborn project [2]. The project is a complement to Matplotlib, providing additional features and improving the default matplotlib aesthetics. Ones I particularly like are seaborn-deep, seaborn-pastel and seaborn-white.
  • dark_background
    The dark_background style is the standard matplotlib one (e.g. classic) with colours changed for high contrast.
Example charts using the Matplotlib seaborn-white style
  • bmh
    This style comes from Bayesian Methods for Hackers [3] book. I find it particularly suits scientific graphing by showing the precision of the plot.
  • ggplot
    This style comes from the plotting system of the same name for the R language [4]: it takes on a lot of contemporary lessons on presenting data, focusing on simplicity.
  • fivethirtyeight
    This style emulates the look and feel of the famous data journalist Nate Silver's site fivethirtyeight.com.

The figures in this post show each of these main styles, plotting some data about an imaginary Product A's sales performance in a year. The first plot is a simple bar chart showing sales by financial quarter, the second plot is a histogram showing how long it takes to sell our imaginary product, and the final plot is a line chart showing how the different marketing channels are creating leads for sales.

There are two ways to set styles, at a library level or for a specific plot. If a style is set at a global level it will effect all figures and plots that are created. For example, if you create multiple plots in a jupyter notebook then they'll all be displayed using the style that's been defined. The call is:

An alternative, is to set the style for the specific plot:

This latter approach is advantageous if you don't want to change every plot that you're creating. I've also found that the fivethirtyeight style changes the legend box in a way I can't revert, but using it with plt.style.context() doesn't change my legend settings.

The styles create much better looking charts, but they don't get us all the way to beautiful plots. Some further alterations, particularly around font selection and font size are necessary. I'm making fairly basic changes, but you can go much further and create your own theme [5].

Simple bar chart using the ggplot style

The default is for Matplotlib to use a sans-serif font for describing the text and marking up the plot, with a different font for Maths mark-up [6]. It's possible to change these settings by specifying the font and text properties: the common aspects to define are the font type, weight, style, size and colour. The most specific way, is to change the properties of a particular command:

The parameters are pretty self-explanatory, with them we're telling Matplotlib to gives us a title using the Ubuntu font at 14 points with italic, bold and green text. This provides a great deal of control over the look of each command, particularly when used with the other properties that are available. However, the downside is that every command becomes quite long-winded.

The alternative to specifying the characteristics at a method level, is to define global settings.

Matplotlib can be configured through global settings which are defined in a configuration file, or at runtime [7]. The pyplot interface to the runtime interface is through pylot.rcParams[8] dictionary. The common aspects I override are:

Plot

The font.family specifies that we're using a serif font, if no other setting was used then Matplotlib would use the default serif font that was available. The font.serif and font.monospace set which ever font we want to use, in my case 'Ubuntu' and 'Ubuntu Mono'. We've set the default font size (font.size) to be 10 points, so any size that's not set will use this. The axes commands tell matplotlib to use 10 points and bold for the axes labels (e.g. Sales and Time (FY) in our example plot). The xtick.labelsize and ytick.labelsize sets the numbers along the axis (e.g. Q1 in our example plot), it uses the monospace font that was set earlier. The figure.titlesize specifies the overall figure title.

The pyplot.rcParams interface provides extensive access to configuring matplotlib, these are just the basics, so it's worth looking through. It covers the vast majority of what I want to configure, the only item I haven't been able to alter is the style (e.g. italics) of some titles.

Styles and Fonts example

The following example is the code that was used to create the different figures scattered through this post. Nothing too complex, the only alteration is for fivethirtyeight style to work properly we have to alter the axes a bit: it's personal taste, I just don't like the axis line cutting through the marker for points that are at zero.

My favourites are ggplot and fivethirtyeight as they're at the more sparse end of the scale. However, my needs aren't for scientific levels of precision and everyone has their own aesthetic sense.

[1]What's new in Matplotlib 1.5
[2]Controlling figure aesthetics
[3]Bayesian Methods for Hackers
[4]Ggplot2 homepage
[5]For more see Customizing Matplotlib's Plotting Styles and Customizing with style sheets
[6]Writing mathematical expressions in Matplotlib
[7]See Customising Matplotlib for the section on the matplotlibrc file.
[8]matplotlib.rcParams documentation
Posted in TechStylesSaturday 27 February 2016Styles
Tagged with pythonmatplotlib

Issue

When plotting/publishing an AutoCAD drawing, the Plot Style Tables available from the Plot dialog box do not match the Plot Style Tables saved in your Plot Style Table Search Path. Even though the desired CTB is saved in the Plot Style Table Search Path, configured from the Files tab of the Options dialog box, the CTB is missing from the Plot dialog box.

Diagnosing the Issue

AutoCAD offers two ways to manage the way drawings plot; by color, or by style. These two methods are managed through the use of CTB files for color plotting, and STB files for style-based plotting. The primary difference between these methods is the property that determines the plotted color, screening, and lineweight of the objects in your drawing. When using a CTB Plot Style Table, the color of an object determines the plotted appearance of your drawing. By contrast, when using a STB, a separate Plot Style property determines the plotted appearance of your drawing.

Hair Plots Styles

Despite each of these methods being part of the software for many years, CTB or color-based plotting remains the most popular method among AutoCAD users. Since which of these methods AutoCAD uses is determined on a drawing-by-drawing basis, the issue of missing CTB files is most often a result of a drawing being set to use an STB file instead.

To determine if your drawing is configured to use a STB:

Python Plot Styles

  1. Open the affected drawing file.
  2. Start the PLOT command.
  3. Look at the Plot Style Table group in the upper-right corner of the Plot dialog box to see whether the list of available plot style tables has a CTB or STB extension. Notice in the illustration below, the list of available styles each have a STB extension.

Solution

Plot Styles

Do the following to change a drawing from using style-based (STB) plotting to color-based (CTB) plotting:

Where Does Autocad Save Plot Styles

  1. Open the affected drawing file if it isn't already.
  2. Enter CONVERTPSTYLES at the command line. The warning dialog box (shown below) opens.
  3. Click OK from the AutoCAD dialog box (shown above) to confirm you would like to convert the drawing to use color-dependent plotting.