【数据分析】基础技术篇 三——matplotlib
程序员文章站
2022-09-14 13:55:00
matplotlibhttps://matplotlib.org/基本线图绘制import matplotlib.pyplot as pltimport numpy as npdef main(): x = np.linspace(-np.pi, np.pi, 256, endpoint=True) c, s = np.cos(x), np.sin(x) plt.figure(1) plt.plot(x, c) plt.plot(x, s)...
matplotlib
基本线图绘制
1.
import matplotlib.pyplot as plt
import numpy as np
def main():
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
c, s = np.cos(x), np.sin(x)
plt.figure(1)
plt.plot(x, c)
plt.plot(x, s)
plt.savefig('1.png')
plt.show()
if __name__ == '__main__':
main()
2.添加属性:
def main2():
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
c, s = np.cos(x), np.sin(x)
plt.figure(1)
plt.plot(x, c, color='blue', linewidth=1.0, linestyle='-', label='COS', alpha=0.5)
plt.plot(x, s, 'r*', label='SIN')
plt.savefig('2.png')
plt.show()
注意:‘r*’,表示的是red,和线形状是*,于是有了图中看起来不平滑。
3.
def main3():
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
c, s = np.cos(x), np.sin(x)
plt.figure(1)
plt.plot(x, c, color='blue', linewidth=1.0, linestyle='-', label='COS', alpha=0.5)
plt.plot(x, s, 'r*', label='SIN')
plt.savefig('3.png')
plt.title('COS & SIN')
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))
plt.show()
4.
def main4():
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
c, s = np.cos(x), np.sin(x)
plt.figure(1)
plt.plot(x, c, color='blue', linewidth=1.0, linestyle='-', label='COS', alpha=0.5)
plt.plot(x, s, 'r*', label='SIN')
plt.savefig('3.png')
plt.title('COS & SIN')
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.xticks([-np.pi, -np.pi / 2.0, np.pi / 2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$-\pi/2$', r'$+\pi$'])
plt.yticks(np.linspace(-1, 1, 5, endpoint=True))
for label in ax.get_xticklabels()+ax.get_yticklabels():
label.set_fontsize(16)
label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.2))
plt.show()
5.
def main5():
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
c, s = np.cos(x), np.sin(x)
plt.figure(1)
plt.plot(x, c, color='blue', linewidth=1.0, linestyle='-', label='COS', alpha=0.5)
plt.plot(x, s, 'r*', label='SIN')
plt.savefig('5.png')
plt.title('COS & SIN')
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.xticks([-np.pi, -np.pi / 2.0, np.pi / 2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$-\pi/2$', r'$+\pi$'])
plt.yticks(np.linspace(-1, 1, 5, endpoint=True))
for label in ax.get_xticklabels()+ax.get_yticklabels():
label.set_fontsize(16)
label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.2))
plt.legend(loc='upper left')#设置左上角图例
# plt.axis([-1, 1, 0.5, 1])#设置显示范围
plt.fill_between(x, np.abs(x) < 0.5, c, c > 0.5, color='green', alpha=0.25)#填充颜色
plt.grid()#网格线可视
plt.show()
6.
def main6():
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
c, s = np.cos(x), np.sin(x)
plt.figure(1)
plt.plot(x, c, color='blue', linewidth=1.0, linestyle='-', label='COS', alpha=0.5)
plt.plot(x, s, 'r*', label='SIN')
plt.savefig('5.png')
plt.title('COS & SIN')
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.xticks([-np.pi, -np.pi / 2.0, np.pi / 2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$-\pi/2$', r'$+\pi$'])
plt.yticks(np.linspace(-1, 1, 5, endpoint=True))
for label in ax.get_xticklabels()+ax.get_yticklabels():
label.set_fontsize(16)
label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.2))
plt.legend(loc='upper left')#设置左上角图例
# plt.axis([-1, 1, 0.5, 1])#设置显示范围
plt.fill_between(x, np.abs(x) < 0.5, c, c > 0.5, color='green', alpha=0.25)#填充颜色
t = 1
plt.plot([t, t], [0, np.cos(t)], 'y', linewidth=3, linestyle='--')
#添加注释
plt.annotate('cos(1)', xy=(t, np.cos(1)), xycoords='data', xytext=(+10, +30), textcoords="offset points", arrowprops=dict(arrowstyle='->', connectionstyle='arc3, rad=.2'))
plt.grid()#网格线可视
plt.show()
子图与多种图形绘制
1.
#scatter 散点图
def main7():
fig = plt.figure()
fig.add_subplot(3, 3, 1)
n = 128
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])
plt.scatter(X, Y, s=75, c=T, alpha=0.5)
plt.xlim(-1.5, 1.5)
plt.xticks([])
plt.ylim(-1.5, 1.5)
plt.yticks([])
plt.axis()
plt.title('scatter')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
2.柱状图
#bar 柱状图
def main8():
fig = plt.figure()
# ax = fig.add_subplot(332)
n = 10
X = np.arange(n)
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.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y1, 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.show()
3.饼状图
#pie 饼图
def main9():
fig = plt.figure()
# fig.add_subplot(333)
n = 20
Z = np.ones(n)
Z[-1] *= 2
plt.pie(Z, explode=Z * .05, colors=['%f' % (i / float(n)) for i in range(n)],
labels=['%.2f' % (i / float(n)) for i in range(n)])
plt.gca().set_aspect('equal')
plt.xticks([])
plt.yticks([])
plt.show()
set_aspect('equal')设置成正的圆形
4.极值图
#polar 极值图
def main10():
fig = plt.figure()
# fig.add_subplot(polar=True)
n = 20
theta = np.arange(0.0, 2 * np.pi, 2 * np.pi / n)
radii = 10 * np.random.rand(n)
plt.plot(theta, radii)
plt.show()
稍微调整一下 !!!
#polar 极值图
def main10():
fig = plt.figure()
fig.add_subplot(polar=True)
n = 20
theta = np.arange(0.0, 2 * np.pi, 2 * np.pi / n)
radii = 10 * np.random.rand(n)
plt.plot(theta, radii)
plt.show()
还可以这样
#polar 极值图
def main10():
fig = plt.figure()
# fig.add_subplot(polar=True)
n = 20
theta = np.arange(0.0, 2 * np.pi, 2 * np.pi / n)
radii = 10 * np.random.rand(n)
# plt.plot(theta, radii)
plt.polar(theta, radii)
plt.show()
5.热图
#heatMap 热图
from matplotlib import cm
def main11():
fig = plt.figure()
data = np.random.rand(3, 3)
cmap = cm.Blues
map = plt.imshow(data, interpolation='nearest',
cmap=cmap, aspect='auto', vmin=0, vmax=1)
plt.show()
6.3D图
#3D图
from mpl_toolkits.mplot3d import Axes3D
#引入3D坐标系
def main12():
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.scatter(1, 1, 3, s=100)
plt.show()
7. hot map 热力图
#hot map 热力图
def main13():
fig = plt.figure()
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.contourf(X, Y, f(X, Y), 8, alpha=.75,
cmap=plt.cm.hot)
plt.show()
本文地址:https://blog.csdn.net/weixin_44566432/article/details/107183212
推荐阅读
-
【数据分析】基础技术篇 三——matplotlib
-
从零搭建企业大数据分析和机器学习平台-技术栈介绍(三)
-
初识人工智能(一):数据分析(三):numpy科学计算基础库(二)
-
Vue 2.0 深入源码分析(三) 基础篇 模板渲染 el、emplate、render属性详解
-
Python由放弃到入门,基础篇一( 最常用的三种数据类型)
-
MySql数据库的学习(基础篇三)
-
从零搭建企业大数据分析和机器学习平台-技术栈介绍(三)
-
【数据分析】基础技术篇 三——matplotlib
-
dojo 之基础篇(三)之向服务器发送数据_dojo
-
Python数据分析三大框架之matplotlib(三)3D图像绘制