Simple drawing of logarithmic images, polar coordinate images, histograms, scatterplots, and 3D images

logarithmic image function

Logarithmic function drawing design three functions, the analysis is as follows:

semilogx(x1, y1): semi-logarithmic function, x becomes a common logarithmic scale, and the y-axis is still a linear scale.

semilogy(x1, y1): semi-logarithmic function, x is still a linear scale, and the y-axis becomes a common logarithmic scale.

loglog(x1, y1): full logarithmic function, x, y become common logarithmic scale.

The code is displayed as follows:

import numpy as np
import matplotlib.pyplot as plt

w = np.linspace(0.1, 1000, 1000)
p = np.abs(1 / (1 + 0.1j * w))

plt.subplot(221)
plt.plot(w, p, lw=2)
plt.xlabel('X')
plt.ylabel('y')

plt.subplot(222)
plt.semilogx(w, p, lw=2)
# ylim具有设置y坐标轴范围的作用
plt.ylim(0, 1.5)
plt.xlabel('log(X)')
plt.ylabel('y')

plt.subplot(223)
plt.semilogy(w, p, lw=2)
# plt.ylim(0, 1.5)
plt.xlabel('x')
plt.ylabel('log(y)')

plt.subplot(224)
plt.loglog(w, p, lw=2)
plt.xlabel('log(x)')
plt.xlabel('log(y)')
plt.show()

The display results are as follows:

insert image description here

Polar coordinate image display

The code shows:

import numpy as np
import matplotlib.pyplot as plt

theta = np.arange(0, 2 * np.pi, 0.02)
plt.subplot(121, polar=True)
plt.plot(theta, 2 * np.ones_like(theta), lw=2)
plt.plot(theta, theta / 6, '--', lw=2)
plt.subplot(122, polar=True)
plt.plot(theta, np.cos(5 * theta), '--', lw=2)
plt.plot(theta, 2 * np.cos(4 * theta), lw=2)
plt.rgrids(np.arange(0.5, 2, 0.5), angle=45)
plt.thetagrids([0, 45, 90])
plt.show()

The image is displayed as follows:
insert image description here

Histogram image display

The code appears as:

import numpy as np
import matplotlib.pyplot as plt


n_groups = 5
means_men = (20, 35, 30, 35, 27)
means_women = (25, 32, 34, 20, 25)
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.3
opacity = 0.4
rects1 = plt.bar(index, means_men, bar_width, alpha=opacity, color='b', label='Men')
rects2 = plt.bar(index + bar_width, means_women, bar_width, alpha=opacity, color = 'r', label = 'Women')
plt.xlabel('Group')
plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(index + bar_width, ('A', 'B', 'C', 'D', 'E'))
plt.ylim(0, 40)
plt.legend()
plt.tight_layout()
plt.show()

The image appears as:
insert image description here

Scatter plot image display

import numpy as np
import matplotlib.pyplot as plt


plt.figure(figsize=(8, 4))
x = np.random.random(100)
y = np.random.random(100)
plt.scatter(x, y, s=x * 1000, c='y', marker=(5, 1), alpha=0.5, lw=2, facecolors='none')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.show()

Image display
insert image description here

3D image display

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d

x, y = np.mgrid[-2:2:20j, -2:2:20j]
z = x * np.exp(-x ** 2 - y ** 2)
ax = plt.subplot(111, projection='3d')
ax.plot_surface(x, y, z, rstride=2, cstride=1, cmap=plt.cm.coolwarm, alpha=0.8)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()

The image is displayed as follows:
insert image description here

3D ellipsoid drawing

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Make data
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))

# Plot the surface
ax.plot_surface(x, y, z, color='b')

plt.show()

Image display:
insert image description here

3D spiral drawing

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

mpl.rcParams['legend.fontsize'] = 10

fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z ** 2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve')
ax.legend()

plt.show()

Image display:
insert image description here

Original link:
https://blog.csdn.net/Mrweng1996/article/details/104352645
https://blog.csdn.net/guduruyu/article/details/78050268
https://blog.csdn.net/ikerpeng/article /details/20523679
"matplotlib manual"

Guess you like

Origin blog.csdn.net/ximu__l/article/details/129180021