Get the App
SLTechnology News&Howtos  ›  Development  › 

How to install and use matplotlib

Shulou Source: shulou.com Published: 2022-06-02 08:44:46 09月20日 Update

This article will explain in detail how to install and use matplotlib for everyone. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.

1. Matplotlib Profile

Matplotlib is a Python 2D drawing library that generates publication quality data in a variety of hardcopy formats and cross-platform interactive environments. Matplotlib is available for Python scripts, Python and IPython shells, Jupyter notebooks, Web application servers and four graphical user interface toolkits.

2. matplotlib installation

Matplotlib installation can be done using source installation and pip installation. Pip is installed as follows:

pip install matplotlib

The latest version is installed by default, or a specified version can be installed

pip install matplotlib==2.2.03. matplotlib Plot Example 3.1 Compiling Common Statistics Graphs

scatter plot

x = np.arange(50)y = x + 5 * np.random.rand(50)plt.scatter(x, y)plt.title ('scatter plot ') #Add title plt.xlabel ('argument ') #Add abscissa plt.ylabel ('dependent variable') #Add ordinate plt.xlim(xmin=0, xmax=50) #Add abscissa range plt.ylim(ymin=0, ymax=50) #Add ordinate range

histogram

plt.hist(x=np.random.randn(100), bins=10, color='b', alpha=0.3)

line chart

plt.plot([1,2,3,4,5],[1,4,5,2,7])

histogram

x = np.arange(5)y1, y2 = np.random.randint(1, 25, size=(2, 5))width = 0.25plt.bar(x, y1, width, color='r')plt.bar(x+width, y2, width, color='g')

pie chart

explode=(0,0.1,0,0,0)partions = [0.30,0.20,0.1,0.15,0.25]labels =<$'Apple ',' Samsung','Millet',' Huaweis','others'] plt.pie(partions,labels=labels,explode=explode,autopct='%1.0f%%')

3.2 Converting mathematical function curves

trigonometric function

x = np.arange(-np.pi,np.pi,0.01)y1 = np.sin(x)y2 = np.cos(x)plt.plot(x,y1,color='green', linewidth=1,linestyle='-',label=' sinusoidal') plt.plot(x,y2,color='blue', linewidth=1,linestyle='--',label=' cosine curve') plt.legend() #Add labels

exponential function

t = np.linspace(-50.0,50.0,1000)func_exp = np.exp(-0.1*t)plt.plot(t,func_exp)plt.title('exp(-0.1*t)')

logarithmic function

t = np.linspace(-10.0,10.0,1000)func_log2 = np.log2(t)plt.plot(t,func_log2)plt.title('log2(t)')plt.grid()

About "matplotlib how to install and use" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Tags: Functions curves articles graphics more Abscissa exchange system version ordinate range drawing good practical trigonometric function exponential function interactive cosine dependent variable instance logarithm Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MySQL macOS Xiaomi Huawei