如何在matplotlib中隐藏特定范围?

问题描述 投票:0回答:2

我想绘制电子结构数据图,如下所示:

    0.0000  -79.9447
    0.0179  -79.9447
    0.0357  -79.9447
    0.0536  -79.9447
    0.0714  -79.9446
    0.0893  -79.9446
    0.1071  -79.9445
    0.1250  -79.9444
    0.1429  -79.9443
    0.1607  -79.9443
    0.1786  -79.9441
    0.1964  -79.9440
.....

每 216 行,就有一个空行,因此我写了这样的代码:

import matplotlib.pyplot as plt
import numpy as np

with open('Mn3PtN_bands.dat.gnu', 'r') as f:
    lines = f.readlines()
    
k_points = []
bands = []
current_band = []
for line in lines:
    if line.strip():  
        data = line.split()
        k_point = float(data[0])
        energy = float(data[1])-17.8059 #Fermi energy level
        
        # K_point space between X &R high symmetry 
        if k_point > 3.2801:
            k_point += 0.2
        
        current_band.append((k_point, energy))
    else: 
        if current_band:
            k_points.append([point[0] for point in current_band])
            bands.append([point[1] for point in current_band])
            current_band = []


plt.figure(figsize=(8, 6))
for i in range(len(bands)):
    plt.plot(k_points[i], bands[i], label=f'Band {i+1}',color='k')
plt.xlabel('k-points')
plt.ylabel('Energy (eV)')
plt.title('Electronic Band Structure')
plt.axis([0, 3.7802, -2, 2]) 
#  high symmetry points
plt.axvline(0.5000, linewidth=0.75,linestyle=(0, (5, 5)), color='r', alpha=0.75)
plt.axvline(1.0000, linewidth=0.75,linestyle=(0, (5, 5)),color='r', alpha=0.75)
plt.axvline(1.7071, linewidth=0.75,linestyle=(0, (5, 5)),color='r', alpha=0.75)
plt.axvline(2.5731, linewidth=0.75,linestyle=(0, (5, 5)),color='r', alpha=0.75)
plt.axvline(3.2802, linewidth=0.75,linestyle=(0, (5, 5)),color='r', alpha=0.75)
plt.axvline(3.4802, linewidth=0.75,linestyle=(0, (5, 5)),color='r', alpha=0.75)
plt.xticks(ticks= [0, 0.5,1.0000, 1.7071, 2.5731, 3.2802, 3.4802,3.9802],\
           labels=['$\Gamma$', 'X','M','$\Gamma$','R','X','R','M'])
#Fermi level
plt.axhline(0.0, linestyle=(0, (5, 5)), linewidth=0.75, color='r', alpha=0.75)
plt.show()

with open('bands_output.dat', 'w') as output_file:
    for i in range(len(bands)):
        for j in range(len(k_points[i])):
            output_file.write('{:.5f} {:.5f}\n'.format(k_points[i][j],bands[i][j]))
        output_file.write('\n')

得到的图是这样的:

enter image description here

正如您所看到的,即使不是,看起来 X 点和 R 点之间也有一些数据。

我想要在绘图内部一定范围内的两个特定 x 点之间有一个空格。我怎样才能做到这一点?

python numpy matplotlib plot
2个回答
0
投票

我认为您正在尝试绘制具有不连续性的函数。由于您没有提供数据集,让我们创建一些代表阶跃函数的玩具数据。

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# Create data
x=np.linspace(0,5,10001)    
y=np.zeros(len(x))
y[x>2.5] = 1

# Plot step function
plt.figure(figsize=(8, 6))

plt.plot(x, y, color='k')

plt.xlabel('x')
plt.ylabel('y')
plt.title('Step Function with Range')    
plt.show()

您可以通过定义一个范围并填充该范围内的 nan 值来处理不连续性。

# Specifying the range within which to introduce NaN values
x1, x2 = 2.45, 2.55  # Example range to avoid plotting

# introduce NaN values for specified x range
y[((x >= x1) & (x <= x2))] = np.nan


plt.figure(figsize=(8, 6))

plt.plot(x, y, color='k')

plt.xlabel('x')
plt.ylabel('y')
plt.title('Step Function with Excluded Range')
plt.show()

然后,您应该将其应用到您所展示的每个功能的案例中。


0
投票

非常感谢,只需更改此即可解决我的问题:

K_point space between X &R high symmetry 
         if k_point > 3.2801:
             k_point += 0.2
             if k_point<=3.4802 and k_point>3.2802:
                 energy=np.nan
© www.soinside.com 2019 - 2024. All rights reserved.