Python Matplotlib库实现画局部图

在画图中,我们需要放大图像的某一部分,看清其变化。最近在写论文时,就遇到了这个问题,还有坐标轴加粗、控制线型和大小等要求。这些,都可以通过Python Matplotlib库实现。具体看下面的代码:

import matplotlib.pyplot as plt

init_np = np.array(x0_list)
xopt_net_np = np.array(xopt_net)

plt.figure(figsize=(8,5))
plt.subplot(311)
ax = plt.gca() # 获取坐标轴
bwith = 1.2
ax.spines['bottom'].set_linewidth(bwith)
ax.spines['left'].set_linewidth(bwith)
ax.spines['top'].set_linewidth(bwith)
ax.spines['right'].set_linewidth(bwith)
plt.yticks([])
plt.xlim([-21,21])
plt.plot(init_np, np.zeros_like(init_np), '.b', markersize=2, label='initial value Distri.')
plt.legend()

plt.subplot(312)
ax = plt.gca()
bwith = 1.2
ax.spines['bottom'].set_linewidth(bwith)
ax.spines['left'].set_linewidth(bwith)
ax.spines['top'].set_linewidth(bwith)
ax.spines['right'].set_linewidth(bwith)
plt.yticks([])
plt.xlabel('x')
plt.plot(xopt_gd, np.zeros_like(xopt_gd), '.r', markersize=2, label='optimal value Distri. by GD')
plt.xlim([-21,21])
plt.legend()

plt.subplot(313)
ax = plt.gca()
bwith = 1.2
ax.spines['bottom'].set_linewidth(bwith)
ax.spines['left'].set_linewidth(bwith)
ax.spines['top'].set_linewidth(bwith)
ax.spines['right'].set_linewidth(bwith)
plt.yticks([])
plt.plot(xopt_net_np, np.zeros_like(xopt_net_np), '.r', markersize=2, label='optimal value Distri. by Network')
plt.xlim([-21,21])
plt.legend()

plt.axes([0.125,0.275,0.25,0.07])   # list:[左下角水平坐标, 左下角垂直坐标, 宽度, 高度]
# plt.xticks(())
plt.yticks(())
plt.plot(xopt_net_np, np.zeros_like(xopt_net_np), '.r', markersize=1, label='optimal value Distr.')
plt.savefig('sol_scipy_ai.png', dpi=400, bbox_inches='tight')
plt.show()

得到了下面的效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Python使用matplotlib绘制多个图形单独显示的方法示例

    本文实例讲述了Python使用matplotlib绘制多个图形单独显示的方法.分享给大家供大家参考,具体如下: 一 代码 import numpy as np import matplotlib.pyplot as plt #创建自变量数组 x= np.linspace(0,2*np.pi,500) #创建函数值数组 y1 = np.sin(x) y2 = np.cos(x) y3 = np.sin(x*x) #创建图形 plt.figure(1) ''' 意思是在一个2行2列共4个子图的图中,

  • Python利用matplotlib.pyplot绘图时如何设置坐标轴刻度

    前言 matplotlib.pyplot是一些命令行风格函数的集合,使matplotlib以类似于MATLAB的方式工作.每个pyplot函数对一幅图片(figure)做一些改动:比如创建新图片,在图片创建一个新的作图区域(plotting area),在一个作图区域内画直线,给图添加标签(label)等.matplotlib.pyplot是有状态的,亦即它会保存当前图片和作图区域的状态,新的作图函数会作用在当前图片的状态基础之上. 在开始本文之前,不熟悉的朋友可以先看看这篇文章:Python

  • python Matplotlib画图之调整字体大小的示例

    一张字体调整好的示例图: 字体大小就是 fontsize 参数 import matplotlib.pyplot as plt # 代码中的"..."代表省略的其他参数 ax = plt.subplot(111) # 设置刻度字体大小 plt.xticks(fontsize=20) plt.yticks(fontsize=20) # 设置坐标标签字体大小 ax.xlabel(..., fontsize=20) ax.ylabel(..., fontsize=20) # 设置图例字体大小

  • Python+matplotlib绘制不同大小和颜色散点图实例

     具有不同标记颜色和大小的散点图演示. 演示结果: 实现代码: import numpy as np import matplotlib.pyplot as plt import matplotlib.cbook as cbook # Load a numpy record array from yahoo csv data with fields date, open, close, # volume, adj_close from the mpl-data/example directory

  • Python数据分析matplotlib设置多个子图的间距方法

    注意,要看懂这里,必须具备简单的Python数据分析知识,必须知道matplotlib的简单使用! 例1: plt.subplot(221) # 第一行的左图 plt.subplot(222) # 第一行的右图 plt.subplot(212) # 第二整行 plt.title('xxx') plt.tight_layout() #设置默认的间距 例2: for i in range(25): plt.subplot(5,5,i+1) plt.tight_layout() 例3: # 设定画图板

  • 学习python中matplotlib绘图设置坐标轴刻度、文本

    总结matplotlib绘图如何设置坐标轴刻度大小和刻度. 上代码: from pylab import * from matplotlib.ticker import MultipleLocator, FormatStrFormatter xmajorLocator = MultipleLocator(20) #将x主刻度标签设置为20的倍数 xmajorFormatter = FormatStrFormatter('%1.1f') #设置x轴标签文本的格式 xminorLocator = M

  • python学习之matplotlib绘制散点图实例

    要绘制单个点,可使用函数scatter(),并向其传递一对x和y坐标,它将在指定位置绘制一个点: """使用scatter()绘制散点图""" import matplotlib.pyplot as plt plt.scatter(2, 4) plt.show() 下面来设置输出的样式:添加标题,给轴加上标签,并确保所有文本都大到能够看清.并使用scatter()绘制一系列点 """使用scatter()绘制散点图&

  • Python绘图Matplotlib之坐标轴及刻度总结

    学习https://matplotlib.org/gallery/index.html 记录,描述不一定准确,具体请参考官网 Matplotlib使用总结图 import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号 import pandas as pd import nump

  • python使用matplotlib绘制柱状图教程

    Matplotlib的概念这里就不多介绍了,关于绘图库Matplotlib的安装方法:点击这里 小编之前也和大家分享过python使用matplotlib实现的折线图和制饼图效果,感兴趣的朋友们也可以点击查看,下面来看看python使用matplotlib绘制柱状图的方法吧,具体如下: 1. 基本的柱状图 import matplotlib.pyplot as plt data = [5, 20, 15, 25, 10] plt.bar(range(len(data)), data) plt.s

  • python中Matplotlib实现绘制3D图的示例代码

    Matplotlib 也可以绘制 3D 图像,与二维图像不同的是,绘制三维图像主要通过 mplot3d 模块实现.但是,使用 Matplotlib 绘制三维图像实际上是在二维画布上展示,所以一般绘制三维图像时,同样需要载入 pyplot 模块. mplot3d 模块下主要包含 4 个大类,分别是: mpl_toolkits.mplot3d.axes3d() mpl_toolkits.mplot3d.axis3d() mpl_toolkits.mplot3d.art3d() mpl_toolkit

随机推荐