[Matplotlib drawing]

Hits: 0

directory ~ python

Personal nickname: [lxw-pro]
Personal homepage: Welcome to [my]
homepage Personal insight: “Failure is the mother of success”, this is the same truth, only by summing up in failure and growing in failure can you become a generation master in the IT industry.

[Matplotlib] learning (continued)

The Python extension library Matplotlibdepends on the extension library NumPyand the standard library Tkinter, and
can draw various forms of graphics, includingLine, Scatter, Pie, Bar, Radarand many more.

The Python extension library Matplotlib includes drawing modules such as pylab and pyplot, as well as a large number of modules for the management and control of graphic elements such as fonts, colors, and legends. The neutralization pylabmodule pyplotprovides a drawing interface similar to MATLAB, supports the management and control of line styles, font properties, axis properties and other properties, and can use very concise codes to draw beautiful patterns.

General procedure for using pylabor pyplotplotting:

  1. read data
  2. Draw line charts, scatter charts, column charts, radar charts or three-dimensional curves and surfaces according to actual needs
  3. Next set the axes and image properties
  4. Display or save plot results

line chart

# Line chart 
import pandas as pd
 import pylab as plt

plt.rc( 'font' , family= 'SimHei' )      # Used to display Chinese labels 
normally plt.rc( 'font' , size= 20 )              # Set the display font size

# plt.rcParams['font.sans-serif'] = ['SimHei']
# plt.rcParams['axes.unicode_minus'] = False

lxw = pd.read_excel('data2.xlsx', header=None)
tsj = lxw.values                     ​​# Extract the data in it 
# print(tsj)

x = tsj[ 0 ]                           # Extract the data of the first row 
y = tsj[ 1 :]                          # Extract the data except the first row

plt.plot(x, y[ 0 ], '-*b' , label= 'Water consumption' )
plt.plot(x, y[ 1 ], '--pr' , label= 'electricity consumption' )

plt.xlabel( 'month' )
plt.ylabel( 'Monthly usage' )

plt.legend(loc='upper left')
plt.grid()
plt.show()

The relevant operating effects are as follows:

Histogram

# Histogram 
import pandas as pd
 import pylab as plt

# Used to display Chinese labels 
normally plt.rc( 'font' , family= 'SimHei' )
plt.rc('font', size=20)

lxw2 = pd.read_excel('data2.xlsx', header=None)

sz = lxw2.T
sz.plot(kind='bar')
plt.legend([ 'water consumption' , 'electricity consumption' ])

plt.xticks(range(6), sz[0], rotation=0)
plt.ylabel( 'amount' )

plt.show()

The relevant operating effects are as follows:

subgraph

# subgraph 
import numpy as np
 import pylab as plt

# plt.rc('text', usetex=True)
y1 = np.random.randint(2, 6, 6)
y1 = y1/sum(y1)

plt.subplot(2, 2, 1)
su = ['Python', 'Java', 'MySQL', 'C', 'Math', 'English']

plt.barh(su, y1)
plt.subplot(222)
plt.pie(y1, labels=su)
plt.subplot(212)

x2 = np.linspace(0.01, 10, 100)
y2 = np.sin(10*x2)/x2

plt.plot(x2, y2)

plt.xlabel('$x$')
plt.ylabel('$\\mathrm{sin}(10x)/x$')

plt.show()

The relevant operating effects are as follows:

——————————————————————————————————————————

3D graph

# 3D curve
import pylab as plt
import numpy as np

swx = plt.axes(projection='3d')

z = e.g. linspace (-50, 50, 1000)
x = z ** 2 * np.sin(z)
y = z ** 2 * np.cos(z)

swx.plot(x, y, z, 'b')

plt.show()

The relevant operating effects are as follows:

3D Surface Plot

# 3D surface plot 
import pylab as plt
 import numpy as np

swt = plt.axes(projection='3d')

x = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, x)

z = 50 * np.sin(x+y)

swt.plot_surface(x, y, z, color='r')

plt.show()

The relevant operating effects are as follows:

3D surface map

# 3D surface map
import pylab as plt
import numpy as np

swb = plt.axes(projection='3d')

x = np.arange(-8, 8, 0.25)
y = np.arange(-8, 8, 0.25)
x, y = np.meshgrid(x, y)

z = np.sin(np.sqrt(x**2+y**2))

tx = swb.plot_surface(x, y, z, cmap='coolwarm')

plt.colorbar(tx)

plt.show()

The relevant operating effects are as follows:

Word of the day:

This is how life is, the feet are long on oneself, it is right to walk forward, until the longing scenery becomes the place to walk!

Continuing to update…

Welcome to followWeChat public account[ Program Life 6 ], let’s discuss and learn together! ! !

You may also like...

Leave a Reply

Your email address will not be published.