search
HomeBackend DevelopmentPython TutorialAnalysis of methods for drawing histograms and subgraphs in Python (code example)

The content of this article is about the analysis of the method of drawing histograms and subgraphs in Python (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. The drawing of histograms also requires the use of pylab under matplotlib, but when drawing line charts we use plot(), and when drawing histograms we need to use hist() . Due to the lack of real data in the drawing process, I use the random numbers generated by np.random.normal(a,b,c) to draw the histogram. a is the mean, b is the standard deviation, and c is the number of generated data. . Use np.arange(a,b,c) to determine the range and spacing of the x-axis of the histogram. a is the minimum value, b is the maximum value, and c is the spacing. Use plt.hist(a,b) to draw, a is the data, b is the characteristic of the histogram, which is optional.

import matplotlib.pylab as plt
import numpy as np
da = np.random.normal(5.0, 0.5, 3000)
dis = np.arange(3.5, 5, 0.1)
plt.hist(da, dis)
plt.show()

2. When drawing a subplot, we need to divide the space into several parts first. In this case, we need to use the command plt.subplot(a,b,c), where a represents the row, b represents the column, and c Represents the current area starting from the first line and counting from left to right to c. For example, if you want to draw three subplots in the first row and one subplot in the second row, you need to use the following code

import matplotlib.pylab as plt
import numpy as np
plt.subplot(2, 3, 1)
plt.subplot(2, 3, 2)
plt.subplot(2, 3, 3)
plt.subplot(2, 1, 2)
plt.show()

Analysis of methods for drawing histograms and subgraphs in Python (code example)

3. After the area splitting is completed, how should we draw the corresponding image in each area? We used the code to split the area into four parts earlier. If we want to draw in a certain area, we only need to write the drawing code under the code of that part.

import matplotlib.pylab as plt
import numpy as np
plt.subplot(2, 3, 1)    #下面的语句绘制第一个子图
x1 = [1, 3, 5, 7, 9, 11]
y1 = [2, 4, 6, 8, 10, 12]
plt.plot(x1, y1, 'c')
plt.subplot(2, 3, 2)    #下面的语句绘制第二个子图
x2 = [3, 5, 6, 7, 9, 13, 20]
y2 = [1, 6, 2, 3, 5, 7, 11]
plt.plot(x2, y2, 'ob')
plt.subplot(2, 3, 3)    #下面的语句绘制第三个子图
x3 = [2, 5, 7, 8, 10, 11]
y3 = [3, 5, 4, 1, 15, 10]
plt.plot(x3, y3, '-.')
plt.plot(x3, y3, 's')
plt.subplot(2, 1, 2)    #下面的语句绘制第四个子图
da = np.random.normal(5.0, 0.5, 3000)
dis = np.arange(3.5, 5, 0.1)
plt.hist(da, dis)
plt.show()

Analysis of methods for drawing histograms and subgraphs in Python (code example)

The above is the detailed content of Analysis of methods for drawing histograms and subgraphs in Python (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
How do you append elements to a Python list?How do you append elements to a Python list?May 04, 2025 am 12:17 AM

ToappendelementstoaPythonlist,usetheappend()methodforsingleelements,extend()formultipleelements,andinsert()forspecificpositions.1)Useappend()foraddingoneelementattheend.2)Useextend()toaddmultipleelementsefficiently.3)Useinsert()toaddanelementataspeci

How do you create a Python list? Give an example.How do you create a Python list? Give an example.May 04, 2025 am 12:16 AM

TocreateaPythonlist,usesquarebrackets[]andseparateitemswithcommas.1)Listsaredynamicandcanholdmixeddatatypes.2)Useappend(),remove(),andslicingformanipulation.3)Listcomprehensionsareefficientforcreatinglists.4)Becautiouswithlistreferences;usecopy()orsl

Discuss real-world use cases where efficient storage and processing of numerical data are critical.Discuss real-world use cases where efficient storage and processing of numerical data are critical.May 04, 2025 am 12:11 AM

In the fields of finance, scientific research, medical care and AI, it is crucial to efficiently store and process numerical data. 1) In finance, using memory mapped files and NumPy libraries can significantly improve data processing speed. 2) In the field of scientific research, HDF5 files are optimized for data storage and retrieval. 3) In medical care, database optimization technologies such as indexing and partitioning improve data query performance. 4) In AI, data sharding and distributed training accelerate model training. System performance and scalability can be significantly improved by choosing the right tools and technologies and weighing trade-offs between storage and processing speeds.

How do you create a Python array? Give an example.How do you create a Python array? Give an example.May 04, 2025 am 12:10 AM

Pythonarraysarecreatedusingthearraymodule,notbuilt-inlikelists.1)Importthearraymodule.2)Specifythetypecode,e.g.,'i'forintegers.3)Initializewithvalues.Arraysofferbettermemoryefficiencyforhomogeneousdatabutlessflexibilitythanlists.

What are some alternatives to using a shebang line to specify the Python interpreter?What are some alternatives to using a shebang line to specify the Python interpreter?May 04, 2025 am 12:07 AM

In addition to the shebang line, there are many ways to specify a Python interpreter: 1. Use python commands directly from the command line; 2. Use batch files or shell scripts; 3. Use build tools such as Make or CMake; 4. Use task runners such as Invoke. Each method has its advantages and disadvantages, and it is important to choose the method that suits the needs of the project.

How does the choice between lists and arrays impact the overall performance of a Python application dealing with large datasets?How does the choice between lists and arrays impact the overall performance of a Python application dealing with large datasets?May 03, 2025 am 12:11 AM

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

Explain how memory is allocated for lists versus arrays in Python.Explain how memory is allocated for lists versus arrays in Python.May 03, 2025 am 12:10 AM

InPython,listsusedynamicmemoryallocationwithover-allocation,whileNumPyarraysallocatefixedmemory.1)Listsallocatemorememorythanneededinitially,resizingwhennecessary.2)NumPyarraysallocateexactmemoryforelements,offeringpredictableusagebutlessflexibility.

How do you specify the data type of elements in a Python array?How do you specify the data type of elements in a Python array?May 03, 2025 am 12:06 AM

InPython, YouCansSpectHedatatYPeyFeLeMeReModelerErnSpAnT.1) UsenPyNeRnRump.1) UsenPyNeRp.DLOATP.PLOATM64, Formor PrecisconTrolatatypes.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function