python 为什么数据样本的曲线由一条线连接?

5lwkijsr  于 2023-06-04  发布在  Python
关注(0)|答案(2)|浏览(306)

我附上我的txt文件,图形和代码。你能告诉我在这段代码中要修改什么吗,或者为什么这第三条直线会出现在我的图中,因为我只需要两条曲线。在其他软件如xmgrace中,它只显示两条曲线。

import numpy as np
import matplotlib.pyplot as plt

deformation, potential = np.loadtxt("poten-Rf259.txt", unpack=True)
plt.subplot(1,3,1)
plt.plot(deformation, potential, linewidth=1, color='b')
plt.xlabel("$deformation$")
plt.ylabel("potential")
plt.yscale("log")
plt.show()

0.60     1996.95397779
0.61     1995.35525840
0.62     1993.86701437
0.63     1992.48491231
0.64     1991.19969171
0.65     1990.00485364
0.66     1988.89605689
0.67     1987.86875188
0.68     1986.91599808
0.69     1986.03420343
0.70     1985.21922699
0.71     1984.46631470
0.72     1983.77171151
0.73     1983.13117131
0.74     1982.53983312
0.75     1981.99532235
0.76     1981.49445904
0.77     1981.03536392
0.78     1980.61563713
0.79     1980.23282155
0.80     1979.88357050
0.81     1979.56605195
0.82     1979.27790062
0.83     1979.01687947
0.84     1978.78288516
0.85     1978.57209950
0.86     1978.38399957
0.87     1978.21704688
0.88     1978.06886192
0.89     1977.93793974
0.90     1977.82377479
0.91     1977.72453886
0.92     1977.64028757
0.93     1977.56966186
0.94     1977.51247702
0.95     1977.46734438
0.96     1977.43305218
0.97     1977.40866283
0.98     1977.39347411
0.99     1977.38486567
1.00     1977.38198044
1.01     1977.38578333
1.02     1977.39435293
1.03     1977.40721066
1.04     1977.42425865
1.05     1977.44721979
1.06     1977.47357918
1.07     1977.50320577
1.08     1977.53535737
1.09     1977.56925776
1.10     1977.60496583
1.11     1977.64155671
1.12     1977.67899730
1.13     1977.71674843
1.14     1977.75457845
1.15     1977.79221495
1.16     1977.82838072
1.17     1977.86261243
1.18     1977.89473051
1.19     1977.92436517
1.20     1977.95062327
1.21     1977.97352605
1.22     1977.99387754
1.23     1978.01047267
1.24     1978.02233261
1.25     1978.03018886
1.26     1978.03434229
1.27     1978.03431686
1.28     1978.02782602
1.29     1978.01489388
1.30     1977.99620759
1.31     1977.97164846
1.32     1977.94011930
1.33     1977.90108165
1.34     1977.85474975
1.35     1977.80116069
1.36     1977.73920528
1.37     1977.66702371
1.38     1977.58440415
1.39     1977.49291347
1.40     1977.39203011
1.41     1977.28170103
1.42     1977.16108588
1.43     1977.02866014
1.44     1976.88410316
1.45     1976.72847328
1.46     1976.56259216
1.47     1976.38386074
1.48     1976.20047863
1.49     1976.00349668
1.50     1975.79062208
1.51     1975.56427272
1.52     1975.32369916
1.53     1975.06525907
1.54     1974.78917861
1.55     1974.49730653
1.56     1974.19357061
1.57     1973.87528322
1.58     1973.54134915
1.59     1973.19101633
1.60     1972.82834582
1.61     1972.45110208
1.62     1972.05553210
1.63     1971.64144442
1.64     1971.20783885
1.65     1970.75735680
1.66     1970.29140464
1.67     1969.81219620
1.68     1969.31582059
1.69     1968.80021418
1.70     1968.26283644
1.71     1967.70510563
1.72     1967.13020137
1.73     1966.53988789
1.74     1965.93070810
1.75     1965.30035432
1.76     1964.64993511
1.77     1963.98214335
1.78     1963.29607120
1.79     1962.58677705
1.80     1961.85278252
1.81     1961.10290054
1.82     1960.33687553
1.83     1959.54927874
1.84     1958.74001445
1.85     1957.90788391
1.86     1957.05397043
1.87     1956.17614396
1.88     1955.26999324
1.89     1954.33375080
1.90     1953.37236582
1.91     1952.38212458
1.92     1951.35983354
1.93     1950.30220457
1.94     1949.20603907
1.95     1948.05714844
1.96     1946.84779027
1.97     1945.58008527
1.98     1944.23420931
1.99     1942.79805826
2.00     1941.26241606
2.01     1939.59635092
2.02     1937.76411055
2.03     1935.72007499
2.04     1933.39927526
2.05     1930.72795563
2.06     1927.60977402
2.07     1923.90784077
2.08     1919.45804354
2.09     1914.31285484

0.60     1998.69655342
0.61     1997.10205960
0.62     1995.61774169
0.63     1994.23972037
0.64     1992.95788603
0.65     1991.76641764
0.66     1990.66090368
0.67     1989.63677721
0.68     1988.68674207
0.69     1987.80810777
0.70     1986.99582463
0.71     1986.24553783
0.72     1985.55344269
0.73     1984.91507367
0.74     1984.32536754
0.75     1983.78293400
0.76     1983.28383613
0.77     1982.82671380
0.78     1982.40862045
0.79     1982.02768397
0.80     1981.67975264
0.81     1981.36364770
0.82     1981.07656995
0.83     1980.81699835
0.84     1980.58410256
0.85     1980.37438858
0.86     1980.18712107
0.87     1980.02126097
0.88     1979.87363292
0.89     1979.74345272
0.90     1979.62995824
0.91     1979.53135472
0.92     1979.44772023
0.93     1979.37757311
0.94     1979.32104058
0.95     1979.27599816
0.96     1979.24203421
0.97     1979.21798198
0.98     1979.20293129
0.99     1979.19417215
1.00     1979.19165696
1.01     1979.19541560
1.02     1979.20395824
1.03     1979.21615707
1.04     1979.23320721
1.05     1979.25599428
1.06     1979.28171220
1.07     1979.31101088
1.08     1979.34264374
1.09     1979.37618654
1.10     1979.41145970
1.11     1979.44782160
1.12     1979.48476237
1.13     1979.52196027
1.14     1979.55904133
1.15     1979.59588083
1.16     1979.63104564
1.17     1979.66430513
1.18     1979.69566099
1.19     1979.72388637
1.20     1979.74966849
1.21     1979.77150493
1.22     1979.79086274
1.23     1979.80610500
1.24     1979.81670006
1.25     1979.82369958
1.26     1979.82686535
1.27     1979.82547672
1.28     1979.81769448
1.29     1979.80330511
1.30     1979.78339939
1.31     1979.75736288
1.32     1979.72409901
1.33     1979.68352105
1.34     1979.63566920
1.35     1979.58069993
1.36     1979.51670517
1.37     1979.44233796
1.38     1979.35779846
1.39     1979.26468767
1.40     1979.16177621
1.41     1979.04976449
1.42     1978.92676326
1.43     1978.79213218
1.44     1978.64555037
1.45     1978.48837015
1.46     1978.32011092
1.47     1978.14017085
1.48     1977.95500440
1.49     1977.75460116
1.50     1977.53929775
1.51     1977.31056663
1.52     1977.06737359
1.53     1976.80606527
1.54     1976.52721955
1.55     1976.23286883
1.56     1975.92682410
1.57     1975.60574409
1.58     1975.26801315
1.59     1974.91469758
1.60     1974.54947451
1.61     1974.16923372
1.62     1973.77070376
1.63     1973.35355306
1.64     1972.91665268
1.65     1972.46280943
1.66     1971.99372187
1.67     1971.51115360
1.68     1971.01142423
1.69     1970.49285180
1.70     1969.95210065
1.71     1969.39103370
1.72     1968.81236301
1.73     1968.21808006
1.74     1967.60511707
1.75     1966.97077175
1.76     1966.31642577
1.77     1965.64473808
1.78     1964.95488565
1.79     1964.24169727
1.80     1963.50348643
1.81     1962.74968760
1.82     1961.97913235
1.83     1961.18709132
1.84     1960.37331514
1.85     1959.53686374
1.86     1958.67882118
1.87     1957.79696651
1.88     1956.88661558
1.89     1955.94582437
1.90     1954.97949954
1.91     1953.98399101
1.92     1952.95610229
1.93     1951.89377598
1.94     1950.79298520
1.95     1949.63969262
1.96     1948.42603486
1.97     1947.15347382
1.98     1945.80171537
1.99     1944.35916231
2.00     1942.81707081
2.01     1941.14446913
2.02     1939.30723411
2.03     1937.25876976
2.04     1934.93414618
2.05     1932.25762526
2.06     1929.13222293
2.07     1925.42169053
2.08     1920.96503220
2.09     1915.81720928
plupiseo

plupiseo1#

这只是因为你是为一组x,y数据绘制一条线,而不是两条线;因此,一条线必须连接所有点。试试看

p.scatter(deformation, potential, s = 2,  color='b')

并查看应该显示的数据。由于这些是数据点,无论如何,这是更合适的。

dkqlctbz

dkqlctbz2#

  • 问题是'poten-Rf259.txt'包含两个单独的数据集,由一个空行分隔,而plt.plot通过一条线连接所有的点,因为没有办法识别数据是分开的。
  • numpy.loadtxtskiprows,但是这个参数似乎没有很好的实现来跳过行的“部分”。
  • 或者,pandas.read_csv具有skiprows,其将接受行的范围(例如,skiprows=range(151, 302)
  • 加载数据的好处是,可以识别单独的数据集,分析更容易。
  • 另一个answer不能创建单独的线图。
  • pandas使用matplotlib作为默认绘图后端,numpy是一个依赖项。
    *python 3.11.3pandas 2.0.1matplotlib 3.7.1seaborn 0.12.2中测试
import pandas as pd
import seaborn as sns
import numpy as np

# load the first section of data, add column names, and a new column identifying the sample
df1 = pd.read_csv('d:/data/poten-Rf259.txt', sep='\\s+', header=None,
                  names=['deformation', 'potential'], skiprows=range(151, 302)).assign(sample=1)

# load the second section of data, add column names, and a new column identifying the sample
df2 = pd.read_csv('d:/data/poten-Rf259.txt', sep='\\s+', header=None,
                  names=['deformation', 'potential'], skiprows=range(0, 151)).assign(sample=2)

# combine the dataframes
df = pd.concat([df1, df2], ignore_index=True)

# create a figure and axes
fig, ax = plt.subplots(figsize=(5, 4))

# iterate through each sample and plot it
for sample, data in df.groupby('sample'):
    data.plot(x='deformation', y='potential', ax=ax, ylabel='potential', label=f'Sample {sample}')

  • df1df2也可以绘制在同一图上。
ax = df1.plot(x='deformation', y='potential', label='Sample 1', figsize=(5, 4))
df2.plot(x='deformation', y='potential', label='Sample 2', ax=ax)

使用seaborn绘图

  • seabornmatplotlib的高级API,可以轻松地从长格式
fig, ax = plt.subplots(figsize=(5, 4))
sns.lineplot(data=df, x='deformation', y='potential', hue='sample', palette='tab10', ax=ax)

相关问题