In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to use Matplotlib to create a visual set of pictures in Python. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Matplotlib has a concept subplot: a small Axes object contained in a Figure object. This allows us to create many subgraphs in one picture to make it easy to compare the data.
There are 3 common ways to create subgraphs:
Fig.add_axes
Plt.subplots
Plt.GridSpec
Import numpy as npimport matplotlib.pyplot as plt%matplotlib inlineplt.style.use ("ggplot") 1. Fig.add_axes
First call plt.figure () to create a Figure object, and the chart is the container for all coordinates.
Call fig.add_axes () to add a subgraph anywhere in the chart, and the method receives a list of four numbers: $[XGray, width, height] $, which represents the coordinates of the lower left corner of the subgraph (xmai y), and the width and height of the subgraph. The value range of these four numbers is $[0jue 1] $, which represents the relative position and size.
Finally, ax.plot is called to map the data to the subgraph.
# create chart object fig = plt.figure (figsize= (10,7)) # create subgraph ax1 = fig.add_axes ([0,0.5,0.45,0.45]) # create a subgraph ax2 = fig.add_axes ([0.5,0,0.45,0.45]) in the upper left corner of the chart # create a subgraph in the lower right corner of the chart # upper left corner subgraph: graph x1 = np.linspace (- 10,10) Ax1.plot (x1, np.sin (x1), color= "red") # bottom right subgraph: bar chart x2 = ["a", "b", "c", "d", "e", "f"] y2 = [1.2,1.3,2.5,0.25,5,1.56] ax2.bar (x2, y2, color= "blue")
2. Plt.subplots
Plt.subplots is used to quickly create multiple subgraphs, which are arranged in a grid. The function returns a tuple of length 2, with the first element being the Figure object and the second element being the coordinate set.
# create a graphic object, split into 2x3 grids, containing 6 coordinate objects fig, axes = plt.subplots (nrows=2, # define the number of rows ncols=3, # define the number of columns sharex=True, # whether to share the x-axis coordinate sharey=True, # whether to share the y-axis coordinate figsize= (10,7) # image size) # axes is the numpy array of 2x3 A single coordinate object can be obtained according to the [row, col] index # print (type (axes)) # print (axes) for i in range (2): for j in range (3): axes [I, j] .text (0.5,0.5, str ((I, j)), ha= "center", fontsize=15)
3. Plt.GridSpec
The above case creates regular subgraphs (grids), but sometimes you want to create irregular subgraphs, some of which are larger to show the core information, and some of which are smaller to show auxiliary information.
Plt.GridSpec does this by creating a grid blueprint and then merging parts of the subgraph (similar to merging cells in Excel).
Call plt.GridSpec to create a grid blueprint
Merge subgraphs on demand by slicing and indexing
Call ax.plot () to map data to a chart
# create chart objects fig = plt.figure (figsize= (10, 7)) # create 2 'grid' blueprints grid = plt.GridSpec (nrows=2, ncols=3, figure=fig) # Grid objects can be indexed and sliced According to the mesh object, you can create coordinate objects for row in range (2): for col in range (3): ax = plt.subplot (grid [row, col]) ax.text (0.5,0.5, str ((row, col)), ha= "center", fontsize=15) # so far, the effect is the same as plt.subplots. Next, let's show how to merge subgraphs.
# create chart object fig = plt.figure (figsize= (10, 7)) # create a 'grid' blueprint of 2'3 'grid = plt.GridSpec (nrows=2, ncols=3, figure=fig) # merge subgraph ax1 = plt.subplot (grid [0,0]) ax2 = plt.subplot (grid [0Magne1:]) # merge (0Magne1) and (0Magne2) subgraphs ax3 = plt.subplot (grid [1) 0:2]) # subgraph ax4 = plt.subplot (grid [1,2]) x = np.linspace (0,10,30) ax1.plot (x, np.sin (x), "- r") ax2.plot (x, np.cos (x), "- ob") ax3.plot (x, np.sin (x + 10), "- oy") ax4.plot (x, np.cos (x + 10)) ("- g") this is how to create a visual set of pictures using Matplotlib in the Python shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.