> 家居
python 100天 98 matplotlib画格子
画格子如下,我们在界面上画如下的区域,划分相应 的小格子
先画grid
import matplotlib.pyplot as pltfrom matplotlib.ticker import MultipleLocatorfig = plt.figure(figsize=(8, 6), dpi=72, facecolor=&34;)axes = plt.subplot(111)axes.set_xlim(0, 4)axes.set_ylim(0, 3)axes.xaxis.set_major_locator(MultipleLocator(1.0))axes.xaxis.set_minor_locator(MultipleLocator(0.1))axes.yaxis.set_major_locator(MultipleLocator(1.0))axes.yaxis.set_minor_locator(MultipleLocator(0.1))axes.grid(which=&39;, axis=&39;, linewidth=0.75, linestyle=&39;, color=&39;)axes.grid(which=&39;, axis=&39;, linewidth=0.25, linestyle=&39;, color=&39;)axes.grid(which=&39;, axis=&39;, linewidth=0.75, linestyle=&39;, color=&39;)axes.grid(which=&39;, axis=&39;, linewidth=0.25, linestyle=&39;, color=&39;)axes.set_xticklabels([])axes.set_yticklabels([])
刻度线
关于主刻度线位置的设置,“ax.xaxis.set_major_locator(MultipleLocator(1.0))”会在x轴的1倍处分别设置主刻度线,其中MultipleLocator(1.0)就是设置主刻度线的显示位置。次要刻度线的显示位置,通过”ax.xaxis.set_minor_locator(AutoMinorLocator(4))”设置次要刻度线的显示位置,其中AutoMinorLocator(4)表示将每一份主刻度线区间等分4份。格子线
我们可以使用 pyplot 中的 grid() 方法来设置图表中的网格线。
grid() 方法语法格式如下:
matplotlib.pyplot.grid(b=None, which=&39;, axis=&39;, )
参数说明:
b:可选,默认为 None,可以设置布尔值,true 为显示网格线,false 为不显示,如果设置 **kwargs 参数,则值为 true。which:可选,可选值有 &39;、&39; 和 &39;,默认为 &39;,表示应用更改的网格线。axis:可选,设置显示哪个方向的网格线,可以是取 &39;(默认),&39; 或 &39;,分别表示两个方向,x 轴方向或 y 轴方向。例子只划格子线
import numpy as npimport matplotlib.pyplot as pltx = np.array([1, 2, 3, 4])y = np.array([1, 4, 9, 16])plt.title(&34;)plt.xlabel(&34;)plt.ylabel(&34;)39;x& 设置 y 就在轴方向显示网格线plt.show()
最后输出连线线
from matplotlib.patches import FancyBboxPatchax = plt.gca()ax.add_patch(FancyBboxPatch((-0.05, .87),width=.66, height=.165, clip_on=False,boxstyle=&34;, zorder=3,facecolor=&39;, alpha=1.0,transform=plt.gca().transAxes))plt.text(-0.05, 1.02, &34;,horizontalalignment=&39;,verticalalignment=&39;,size=&39;,transform=axes.transAxes)plt.text(-0.05, 1.01, &34;,horizontalalignment=&39;,verticalalignment=&39;,size=&39;,transform=axes.transAxes)
完整的代码如下
&39;&39;&39;import matplotlib.pyplot as pltfrom matplotlib.ticker import MultipleLocatorfig = plt.figure(figsize=(8, 6), dpi=72, facecolor=&34;)axes = plt.subplot(111)axes.set_xlim(0, 4)axes.set_ylim(0, 3)axes.xaxis.set_major_locator(MultipleLocator(1.0))axes.xaxis.set_minor_locator(MultipleLocator(0.1))axes.yaxis.set_major_locator(MultipleLocator(1.0))axes.yaxis.set_minor_locator(MultipleLocator(0.1))axes.grid(which=&39;, axis=&39;, linewidth=0.75, linestyle=&39;, color=&39;)axes.grid(which=&39;, axis=&39;, linewidth=0.25, linestyle=&39;, color=&39;)axes.grid(which=&39;, axis=&39;, linewidth=0.75, linestyle=&39;, color=&39;)axes.grid(which=&39;, axis=&39;, linewidth=0.25, linestyle=&39;, color=&39;)axes.set_xticklabels([])axes.set_yticklabels([]) from matplotlib.patches import FancyBboxPatchax = plt.gca()ax.add_patch(FancyBboxPatch((-0.05, .87), width=.66, height=.165, clip_on=False, boxstyle=&34;, zorder=3, facecolor=&39;, alpha=1.0, transform=plt.gca().transAxes))plt.text(-0.05, 1.02, &34;, horizontalalignment=&39;, verticalalignment=&39;, size=&39;, transform=axes.transAxes)plt.text(-0.05, 1.01, &34;, horizontalalignment=&39;, verticalalignment=&39;, size=&39;, transform=axes.transAxes)plt.show()import numpy as npimport matplotlib.pyplot as pltx = np.array([1, 2, 3, 4])y = np.array([1, 4, 9, 16])plt.title(&34;)plt.xlabel(&34;)plt.ylabel(&34;)39;x& 设置 y 就在轴方向显示网格线plt.show()