概述
使用python进行绘图(二)
条形图:
#! conda env
# -*- coding:utf-8 -*-
# Time:2020/4/9 10:57 上午
# Author : nishizzma
# File : barEx.py
import numpy as np
import matplotlib.pyplot as plt
n = 12
X = np.arange(n)
"""
numpy.random.uniform(low,high,size)
从一个均匀分布[low,high)中随机采样,注意定义域是左闭右开,即包含low,不包含high.
"""
Y1 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)
Y2 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)
plt.axes([0.025,0.025,0.95,0.95])
"""
bar(x, height, width=0.8, bottom=None, ***, align='center', data=None, **kwargs)
"""
plt.bar(X,+Y1,facecolor='#9999FF',edgecolor='white')
plt.bar(X,-Y2,facecolor='#ff9999',edgecolor='white')
#对数轴进行一个数字表示
for x,y in zip(X,Y1):
plt.text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom')
for x,y in zip(X,Y2):
plt.text(x+0.4, -y-0.05, '%.2f' % y, ha='center', va= 'top')
plt.xlim(-.5,n), plt.xticks([])
plt.ylim(-1.25,+1.25), plt.yticks([])
plt.show()
等高线图:
#! conda env
# -*- coding:utf-8 -*-
# Time:2020/4/9 11:19 上午
# Author : nishizzma
# File : contourEx.py
import numpy as np
import matplotlib.pyplot as plt
def f(x,y):
return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)
n = 256
x = np.linspace(-3,3,n)
y = np.linspace(-3,3,n)
X,Y = np.meshgrid(x,y)
plt.axes([0.025,0.025,0.95,0.95])
#cmap:可选参数,可以转换为热力图
plt.contourf(X, Y, f(X,Y), 8, alpha=.75, cmap=plt.cm.hot)
C = plt.contour(X, Y, f(X,Y), 8, colors='black', linewidth=.5)
plt.clabel(C, inline=1, fontsize=10)
plt.xticks([]), plt.yticks([])
plt.show()
灰度图:
#! conda env
# -*- coding:utf-8 -*-
# Time:2020/4/9 11:40 上午
# Author : nishizzma
# File : imshowEx.py
import numpy as np
import matplotlib.pyplot as plt
def f(x,y):
return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)
n = 10
x = np.linspace(-3,3,3.5*n)
y = np.linspace(-3,3,3.0*n)
X,Y = np.meshgrid(x,y)
Z = f(X,Y)
plt.axes([0.025,0.025,0.95,0.95])
plt.imshow(Z,interpolation='bicubic', cmap='bone', origin='lower')
plt.colorbar(shrink=.92)
plt.xticks([]), plt.yticks([])
plt.show()
饼状图:
#! conda env
# -*- coding:utf-8 -*-
# Time:2020/4/9 11:48 上午
# Author : nishizzma
# File : pieEx.py
import numpy as np
import matplotlib.pyplot as plt
n = 20
Z = np.ones(n)
Z[-1] *= 2
plt.axes([0.025, 0.025, 0.95, 0.95])
plt.pie(Z, explode=Z*.05, colors=['%f' % (i/float(n)) for i in range(n)],
wedgeprops={"linewidth": 1, "edgecolor": "black"})
plt.gca().set_aspect('equal')
plt.xticks([]), plt.yticks([])
plt.show()
极坐标图:
#! conda env
# -*- coding:utf-8 -*-
# Time:2020/4/9 11:58 上午
# Author : nishizzma
# File : polarEx.py
import numpy as np
import matplotlib.pyplot as plt
ax = plt.axes([0.025,0.025,0.95,0.95], polar=True)
N = 20
theta = np.arange(0.0, 2*np.pi, 2*np.pi/N)
radii = 10*np.random.rand(N)
width = np.pi/4*np.random.rand(N)
bars = plt.bar(theta, radii, width=width, bottom=0.0)
for r,bar in zip(radii, bars):
bar.set_facecolor( plt.cm.jet(r/10.))
bar.set_alpha(0.5)
ax.set_xticklabels([])
ax.set_yticklabels([])
plt.show()
散点图:
#! conda env
# -*- coding:utf-8 -*-
# Time:2020/4/9 10:40 上午
# Author : nishizzma
# File : scatterDiagram.py
import numpy as np
import matplotlib.pyplot as plt
n = 1024
X = np.random.normal(0,1,n)
Y = np.random.normal(0,1,n)
T = np.arctan2(Y,X)
plt.axes([0.025,0.025,0.95,0.95])
#这里的c是控制颜色的,也应该与X,Y格式相同
plt.scatter(X,Y, s=75, c=T, alpha=.5)
plt.xlim(-1.5,1.5), plt.xticks([])
plt.ylim(-1.5,1.5), plt.yticks([])
# savefig('../figures/scatter_ex.png',dpi=48)
plt.show()
随机生成文字图:
#! conda env
# -*- coding:utf-8 -*-
# Time:2020/4/9 11:49 上午
# Author : nishizzma
# File : testEx.py
import numpy as np
import matplotlib.pyplot as plt
#首先将公式存入列表中
eqs = []
eqs.append((r"$W^{3beta}_{delta_1 rho_1 sigma_2} = U^{3beta}_{delta_1 rho_1} + frac{1}{8 pi 2} int^{alpha_2}_{alpha_2} d alpha^prime_2 left[frac{ U^{2beta}_{delta_1 rho_1} - alpha^prime_2U^{1beta}_{rho_1 sigma_2} }{U^{0beta}_{rho_1 sigma_2}}right]$"))
eqs.append((r"$frac{drho}{d t} + rho vec{v}cdotnablavec{v} = -nabla p + munabla^2 vec{v} + rho vec{g}$"))
eqs.append((r"$int_{-infty}^infty e^{-x^2}dx=sqrt{pi}$"))
eqs.append((r"$E = mc^2 = sqrt{{m_0}^2c^4 + p^2c^2}$"))
eqs.append((r"$F_G = Gfrac{m_1m_2}{r^2}$"))
plt.axes([0.025,0.025,0.95,0.95])
#采用循环进行多次打印
for i in range(24):
index = np.random.randint(0,len(eqs))
eq = eqs[index]
size = np.random.uniform(12,32)
x,y = np.random.uniform(0,1,2)
alpha = np.random.uniform(0.25,.75)
plt.text(x, y, eq, ha='center', va='center', color="#11557c", alpha=alpha,
transform=plt.gca().transAxes, fontsize=size, clip_on=True)
plt.xticks([]), plt.yticks([])
plt.show()
"""
如果对于循环的变量进行一定的设置,然后在写入一些限制条件,就可以多样化的输出文字
"""
三维立体图:
#! conda env
# -*- coding:utf-8 -*-
# Time:2020/4/9 11:57 上午
# Author : nishizzma
# File : threeD.py
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.hot)
ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.cm.hot)
ax.set_zlim(-2,2)
plt.show()
最后
以上就是阔达书本为你收集整理的使用python代替MATLAB进行绘图(二)使用python进行绘图(二)的全部内容,希望文章能够帮你解决使用python代替MATLAB进行绘图(二)使用python进行绘图(二)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复