Solve matplotlib\cbook\deprecation.py:107: MatplotlibDeprecationWarning: Passing one of on, true,

Table of Contents

Solve matplotlib\cbook\deprecation.py:107: MatplotlibDeprecationWarning: Passing one of ‘on’, ‘true’ as a string is deprecated.

Problem Description

Solution

1. Use Boolean values instead of strings

2. Remove parameters

3. Update Matplotlib version

Summarize

1. Example 1: Draw a chart with a grid

2. Example 2: Adjust the aspect ratio of the chart

Introduction to Matplotlib plotting

1. What is Matplotlib?

2. Features of Matplotlib

3. Matplotlib drawing basics

4. Advanced usage of Matplotlib


Solution to matplotlib\cbook\deprecation.py:107: MatplotlibDeprecationWarning: Passing one of ‘on’, ‘true’ as a string is deprecated.

Problem Description

When using Matplotlib to draw, you may sometimes encounter warning messages similar to the following:

pythonCopy codec:\path\to\matplotlib\cbook\deprecation.py:107: MatplotlibDeprecationWarning: Passing one of 'on', 'true' as a string is deprecated; use an actual boolean (True/False) instead.
  warnings.warn(message, mplDeprecation, stacklevel=1)

This warning message usually appears when calling certain functions of Matplotlib. It reminds us that the parameters passed to the function should use Boolean values (True/False) instead of the strings ‘on’ or ‘true’. Although this warning does not affect the plotting results, in some cases we may wish to eliminate this warning message.

Solution

To resolve this warning message, we need to modify our code and change the value of the parameter to a Boolean value (True/False). Here are some solutions to common situations:

1. Use Boolean values to replace strings

In the warning message, it clearly states that we should use Boolean values (True/False), not the strings ‘on’ or ‘true’. So we just replace these strings with their corresponding boolean values. For example:

pythonCopy code# uses the string 'on'
plt.grid('on')
# Modify to Boolean True
plt.grid(True)
# Use the string 'true'
plt.set_aspect('true')
# Change to Boolean value False
plt.set_aspect(False)

2. Remove parameters

In some cases, it is not necessary to specify the value of some parameters as the string ‘on’ or ‘true’, and we can directly remove these parameters. For example:

pythonCopy code# Remove 'on' parameter
plt.grid()
# Or set it directly to False
plt.grid(False)

3. Update Matplotlib version

Some older versions of Matplotlib may still use the strings ‘on’ or ‘true’ and produce this warning message. Therefore, a simple solution is to update Matplotlib to a newer version. You can update using pip using the following command:

plaintextCopy codepip install --upgrade matplotlib

After the update is completed, run the code again and this warning message may not appear again.

Summary

Matplotlib provides powerful drawing functions, but you may encounter some warning messages during use. This article describes how to resolve a common warning in Matplotlib: ??MatplotlibDeprecationWarning: Passing one of 'on', 'true' as a string is deprecated; use an actual boolean (True/False) instead. ??We can simply replace the value of the parameter with a Boolean value (True/False), or simply remove unnecessary parameters. In the meantime, updating to the latest Matplotlib version may also resolve this issue. Through these methods, we can eliminate this warning message and make our code more standardized and easier to maintain.

1. Example 1: Draw a chart with a grid

If we want to draw a chart with a grid, we can use the ??plt.grid()?? function. Normally, we pass a string parameter ‘on’ to turn on the grid. However, this triggers a warning message. To solve this problem, we just need to replace the string ‘on’ with the boolean True. For example:

pythonCopy codeimport matplotlib.pyplot as plt
# Draw a chart
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
# Turn on grid
plt.grid(True)
# show chart
plt.show()

By changing the parameter value to Boolean True, we avoided the warning message and successfully drew the chart with the grid.

2. Example 2: Adjust the aspect ratio of the chart

If we want to adjust the aspect ratio of the chart, we can use the ??plt.set_aspect()?? function. In older versions of Matplotlib, we could pass the string argument ‘true’ to set the aspect ratio. However, this creates a caveat. To solve this problem, we simply replace the string ‘true’ with the boolean value False. For example:

pythonCopy codeimport matplotlib.pyplot as plt
# Draw a chart
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
#Set the aspect ratio
plt.set_aspect(False)
# show chart
plt.show()

By changing the parameter value to a Boolean value of False, we resolved the warning message and successfully adjusted the chart’s aspect ratio.

Matplotlib is a powerful drawing library, but you may encounter warning messages during use. This article describes two ways to resolve one of the common warnings, along with specific example code. By changing the parameter passing method from string to boolean, we can eliminate warning messages and make our code more standardized and maintainable. In practical applications, we can use appropriate solutions based on specific drawing needs to obtain better results.

Matplotlib drawing introduction

1. What is Matplotlib?

Matplotlib is a Python plotting library for creating static, dynamic and interactive charts. It is one of the important tools for data visualization, providing numerous plotting options and customization features. Matplotlib enables users to create various types of charts, including line charts, scatter plots, bar charts, pie charts, contour charts, 3D charts, and more. It is commonly used in data analysis, scientific research, report generation and other fields.

2. Features of Matplotlib

Here are some of the main features of Matplotlib:

  • Easy to use: Matplotlib provides a simple and intuitive drawing interface and rich drawing style options, allowing users to easily draw various types of charts.
  • Highly customizable: Matplotlib allows users to customize the appearance and style of charts by modifying plotting properties and setting custom styles, making charts more readable and beautiful.
  • Multiple output formats: Matplotlib supports outputting charts into various formats, such as image files (such as PNG, JPEG, SVG), PDF documents, etc., making it easy to use and share on different occasions.
  • Rich chart types: Matplotlib provides a variety of common chart types, such as line charts, scatter plots, histograms, pie charts, contour charts, 3D charts, etc., to meet different data types and display needs.

3. Matplotlib drawing basics

To draw a chart using Matplotlib, you first need to import the Matplotlib library and create a chart object. You can then add data, set properties, and generate charts by calling methods on the chart object. For example, you can use the ??plt.plot()?? method to draw a line plot in a chart, and the ??plt.scatter()?? method to draw a scatter plot wait. Here is a simple Matplotlib plotting example that plots a simple line graph:

pythonCopy codeimport matplotlib.pyplot as plt
#Create chart object
plt.figure()
# adding data
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
# Draw line graph
plt.plot(x, y)
# Set title
plt.title("Simple line graph")
#Set the horizontal and vertical axis names
plt.xlabel("x-axis")
plt.ylabel("y-axis")
# show chart
plt.show()

In the above code, the ??matplotlib.pyplot?? module is first imported and a chart object ??plt.figure()?? is created. Then, the data was added using the ??plt.plot()?? method, and ??plt.title()??, ??plt. The xlabel() and plt.ylabel() methods set the chart's title and axis labels. Finally, use the ??plt.show()?? method to display the chart.

4. Advanced usage of Matplotlib

In addition to basic plotting functions, Matplotlib also provides many advanced functions and extensions to meet more complex plotting needs. Here are some advanced uses of Matplotlib:

  • Subplot and layout management: Matplotlib allows multiple charts to be combined in one image and provides a variety of layout management methods. You can use the ??plt.subplot()?? method to create subplots, and the ??plt.subplots()?? method to create multiple subplots. You can also use the ??plt.subplots_adjust()?? method to adjust the layout of the subplots.
  • Chart style and color: Matplotlib allows users to customize the style and color of charts. You can use the ??plt.style?? module to select different styles, such as default style, ggplot style, etc. The default style can be modified globally through the ??plt.rc()?? method. Custom colors can be set using the ??plt.colors?? module.
  • Chart annotations and annotations: Matplotlib allows users to add annotations, annotations, arrows, etc. to charts. You can use the ??plt.annotate()?? method to add annotations, use the ??plt.text()?? method to add text annotations, and use the ?? plt.arrow()?? method adds arrows, etc.
  • 3D drawing: Matplotlib provides the function of drawing 3D charts, such as surface plots, scatter plots, and contour plots. You can create and customize 3D charts using the classes and methods in the mpl_toolkits.mplot3d module.

Matplotlib is a powerful, flexible and easy-to-use Python plotting library for creating various types of charts. It has rich features and flexible setting options to meet data visualization needs in different fields. With Matplotlib, users can create high-quality images and customize the style and layout of charts. In addition, Matplotlib also provides many advanced functions and extension options, such as subplots, chart styles, color settings, and 3D plots. Whether you are a beginner or a professional, you can achieve great data visualization with Matplotlib.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeHomepageOverview 383925 people are learning the system

syntaxbug.com © 2021 All Rights Reserved.