Home > Enterprise >  Putting patterns in Matplotlib
Putting patterns in Matplotlib

Time:08-21

I need to designe a plot. I mostly use python and specially Matplotlib library for drawing plots. . I want to put patterns between two lines in python using Matplotlib library. The lines are drawn using two datasets. I have written the code like this-

import numpy as np    
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.ticker as tck


from operator import itemgetter
def Extractx(lst):
    return list( map(itemgetter(0), lst ))
def Extracty(lst):
    return list( map(itemgetter(1), lst ))


mpl.rcParams['text.latex.preamble'] = r'\usepackage{mathpazo}'

plt.rcParams['axes.linewidth'] = 2
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.rcParams['axes.linewidth'] = 2









data1=np.array([[ 0.99279306, -0.88652153],
       [ 0.94748646, -0.84746458],
       [ 0.90216618, -0.81201794],
       [ 0.86066134, -0.76929599],
       [ 0.83042503, -0.75168207],
       [ 0.7851321 , -0.70901482],
       [ 0.75492314, -0.6841803 ],
       [ 0.72095345, -0.65217986],
       [ 0.67942126, -0.61667852],
       [ 0.65298671, -0.5953996 ],
       [ 0.62656583, -0.57051037],
       [ 0.57372409, -0.52073191],
       [ 0.54350145, -0.49950769],
       [ 0.49820852, -0.45684044],
       [ 0.46798589, -0.43561621],
       [ 0.43401619, -0.40361578],
       [ 0.38870959, -0.36455883],
       [ 0.36228872, -0.3396696 ],
       [ 0.31696844, -0.30422296],
       [ 0.29809638, -0.28644494],
       [ 0.2603386 , -0.25449921],
       [ 0.23014332, -0.22605437],
       [ 0.16595099, -0.17282971],
       [ 0.09420984, -0.11249385],
       [ 0.01115913, -0.03788086],
       [-0.04924512,  0.0153985 ],
       [-0.12097259,  0.07934467],
       [-0.18893934,  0.13612494],
       [-0.26821563,  0.20718232],
       [-0.32861988,  0.26046168],
       [-0.40413544,  0.32435315],
       [-0.47963733,  0.39185493],
       [-0.54381598,  0.4486899 ],
       [-0.61555714,  0.50902576],
       [-0.6835102 ,  0.56941633],
       [-0.75525135,  0.6297522 ],
       [-0.83075324,  0.69725398],
       [-0.8949319 ,  0.75408894],
       [-0.94399923,  0.8003118 ],
       [-0.98553143,  0.83581314]])




data2=np.array([[ 1.14077457, -0.81940266],
       [ 1.10676385, -0.79823314],
       [ 1.05013402, -0.74850938],
       [ 0.99721022, -0.72039276],
       [ 0.95943876, -0.69205733],
       [ 0.88016246, -0.62099995],
       [ 0.81597013, -0.56777529],
       [ 0.75557956, -0.51088562],
       [ 0.64986872, -0.41854931],
       [ 0.58191565, -0.35815874],
       [ 0.50640009, -0.29426727],
       [ 0.44600952, -0.23737761],
       [ 0.37805645, -0.17698704],
       [ 0.28366884, -0.09531754],
       [ 0.23079974, -0.0527597 ],
       [ 0.16284667,  0.00763087],
       [ 0.09111919,  0.07157705],
       [ 0.01560363,  0.13546852],
       [-0.06367267,  0.2065259 ],
       [-0.1203025 ,  0.25624966],
       [-0.19581806,  0.32014113],
       [-0.26754554,  0.3840873 ],
       [-0.33927302,  0.44803348],
       [-0.41101417,  0.50836935],
       [-0.47519282,  0.56520431],
       [-0.54315956,  0.62198457],
       [-0.62998468,  0.70015316],
       [-0.70927466,  0.76760024],
       [-0.76966523,  0.82448991],
       [-0.83387123,  0.87410426],
       [-0.9093868 ,  0.93799573],
       [-0.96976369,  0.99849571],
       [-0.99993162,  1.03416115]])
    












fig = plt.figure(figsize=(6,5))
fig.tight_layout()
plt.subplots_adjust(wspace=0.35)
ax = fig.add_subplot(111)
ax.tick_params(which='major',direction='in',width=2,length=10,top=True,right=True)
ax.tick_params(which='minor',direction='in',width=1,length=7,top=True,right=True)
    
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)






plt.plot(Extractx(data1),Extracty(data1), 'k-', linewidth=3.5)
plt.plot(Extractx(data2),Extracty(data2), 'k-', linewidth=3.5)


plt.ylabel(r'$y$',fontsize=22)
plt.xlabel(r'$x$',fontsize=22)



ax.set_xlim([-1,1])
ax.set_ylim([-1,1])

ax.xaxis.set_minor_locator(tck.AutoMinorLocator())
ax.yaxis.set_minor_locator(tck.AutoMinorLocator())

ax.set_xticks([-1.0,-0.50,0.0,0.50, 1.0])
ax.set_xticklabels([-1.0,-0.50,0.0,0.50, 1.0])
ax.set_yticks([-1.0,-0.50,0.0,0.50, 1.0])
ax.set_yticklabels([-1.0,-0.50,0.0,0.50, 1.0])


plt.show()

But I want to put patterns between two lines. So, please help me to make the code in Matplotlib.

CodePudding user response:

The start and end points of each polygon shape are specified, hatching is set, and a fill color is specified.

from matplotlib import patches
points = [[Extractx(data1)[0], Extracty(data1)[0]],
          [Extractx(data1)[-1], Extracty(data1)[-1]],
          [Extractx(data2)[-1], Extracty(data2)[-1]],
          [Extractx(data2)[0], Extracty(data2)[0]]
         ]
patch = patches.Polygon(xy=points, hatch='\\/...', fc='c', alpha=0.5, closed=True)
ax.add_patch(patch)

plt.show()

enter image description here

  • Related