如何解析重叠的轴记号标签

2024-10-05 10:34:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在利用SDSS(斯隆数字天空测量)的数据来绘制和分析恒星的光谱。为此,我从他们的网站下载了一个.csv文件,从中提取每一行及其数据,然后绘制这些数据。数据的格式是波长、通量、最佳拟合、天空通量,但我只对波长和最佳拟合感兴趣。我只是想提一下,这样我的代码就更有意义了。我试着每10个和100个数据点运行一次,但我无法解决我的问题。任何帮助都将不胜感激。SDSS网站上有一张想要的结果图,我将其粘贴在下面:

The expected result

下面是我的代码生成的图表(显然非常错误):

My result

代码如下:

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

with open("spectrum.csv", "r") as test:
    rawData = test.readlines()
test.close()
dataProcessed = []
for x in range(1, len(rawData)):
    line = rawData[x].split('\n')
    line.pop(1)     # I added this because the list always had a [''] in the end so I just removed that
    for data in line:
        finalData = data.split(',')
        dataProcessed.append([finalData[0], finalData[2]])
x = []
y = []

for point in dataProcessed:
    x.append(point[0])
    y.append(point[1])
    
plt.plot(x, y)
plt.savefig("plot1.png")

示例CSV数据

Wavelength,Flux,BestFit,SkyFlux
3809.781,29.277,26.563,4.139
3810.658,28.131,25.925,4.118
3811.536,30.299,26.545,3.995
3812.414,26.515,27.773,4.162
3813.291,26.534,26.918,4.089
3814.17,30.009,27.377,4.022
3815.048,30.768,28.017,3.925
3815.926,31.629,27.321,3.812
3816.805,32.29,26.563,3.703
3817.684,30.524,26.733,3.588
3818.563,32.006,26.96,3.481
3819.443,35.618,26.816,3.376
3820.322,27.838,27.272,3.281
3821.202,28.944,26.889,3.226
3822.082,29.651,27.091,3.163
3822.962,30.12,27.965,3.177
3823.843,31.858,27.389,3.203
3824.723,30.154,27.065,3.253
3825.604,26.587,27.186,3.356
3826.485,24.657,26.922,3.518
3827.366,24.15,26.631,3.759
3828.247,24.83,27.103,4.045
3829.129,28.454,28.112,4.338
3830.011,27.9,27.816,4.695
3830.893,26.63,27.613,4.975
3831.775,25.385,27.16,5.183
3832.657,19.389,26.798,5.228
3833.54,18.346,26.861,5.02
3834.423,17.291,27.088,4.602
3835.306,13.885,27.351,4.224
3836.189,19.341,27.105,3.973
3837.072,24.488,27.506,3.772
3837.956,20.605,27.468,3.52
3838.84,17.923,26.783,3.253
3839.724,24.792,26.986,3.144
3840.608,27.875,27.21,3.351
3841.493,28.281,27.106,3.686
3842.377,26.924,27.365,3.957
3843.262,28.649,27.668,4.097
3844.147,31.422,27.66,4.197
3845.032,27.593,27.725,4.367
3845.918,36.582,28.195,4.525
3846.803,34.788,27.889,4.473
3847.689,32.116,28.016,4.212
3848.575,40.388,27.897,3.891
3849.462,40.246,27.69,3.735
3850.348,39.531,28.53,3.8
3851.235,41.553,28.021,3.895
3852.122,40.606,28.085,3.846
3853.009,39.078,28.331,3.677
3853.896,40.404,28.439,3.505
3854.784,37.674,28.335,3.481
3855.671,40.249,28.143,3.554
3856.559,33.143,28.489,3.592
3857.447,35.211,27.918,3.376
3858.336,35.984,28.307,2.946
3859.224,31.993,28.672,2.549
3860.113,30.193,28.274,2.39
3861.002,31.871,28.326,2.64
3861.891,34.52,28.605,3.182
3862.78,38.937,28.364,3.692
3863.67,37.471,28.469,3.848
3864.56,38.229,28.839,3.714
3865.449,42.878,28.906,3.509
3866.34,44.18,28.575,3.415
3867.23,39.195,28.557,3.422
3868.121,35.928,29.104,3.423
3869.011,41.08,29.793,3.267
3869.902,44.947,30.057,2.932
3870.793,37.783,29.972,2.58
3871.685,42.001,29.8,2.405
3872.576,35.932,28.642,2.47
3873.468,30.618,28.725,2.791
3874.36,32.889,28.646,3.196
3875.252,36.743,28.452,3.482
3876.145,35.698,28.355,3.549
3877.037,34.679,27.831,3.434
3877.93,34.803,27.555,3.24
3878.823,31.762,28.165,3.094
3879.717,35.486,28.789,3.025
3880.61,32.234,28.781,2.89
3881.504,43.073,28.223,2.557
3882.398,37.754,27.779,2.616
3883.292,34.786,28.093,2.948
3884.186,30.06,29.004,3.198
3885.08,30.07,28.547,3.302
3885.975,32.837,28.299,3.163
3886.87,29.607,29.131,2.91
3887.765,28.113,29.173,2.814
3888.66,16.369,28.23,2.882
3889.556,21.369,29.077,3.014
3890.451,20.612,28.845,3.129
3891.347,25.129,28.5,3.115
3892.243,30.816,29.227,3.174
3893.14,31.399,29.674,3.409
3894.036,29.617,29.363,3.613
3894.933,31.307,29.641,3.614
3895.83,35.675,29.588,3.572
3896.727,36.238,29.645,3.66
3897.625,35.882,29.691,3.968
3898.522,39.471,29.125,4.297
3899.42,38.397,30.324,4.497
3900.318,41.237,30.222,4.637
3901.216,36.932,30.664,4.659
3902.114,36.238,30.745,4.653
3903.013,36.594,30.212,4.819
3903.912,32.248,30.775,5.054
3904.811,36.856,30.942,5.201
3905.71,38.369,30.493,5.355
3906.609,38.184,30.431,5.707
3907.509,36.815,31.033,6.334
3908.409,37.914,31.328,6.872
3909.309,36.776,30.553,6.705
3910.209,36.994,30.814,6.463
3911.11,37.82,30.853,6.565
3912.01,36.776,31.642,7.134
3912.911,36.912,31.803,8.193
3913.812,39.5,31.268,9.088
3914.714,38.868,32.019,9.052
3915.615,42.118,32.426,8.034
3916.517,40.479,31.817,6.491
3917.419,35.879,32.422,5.464
3918.321,39.773,33.025,5.005
3919.223,38.837,32.53,4.686
3920.126,34.566,32.046,4.408
3921.029,37.092,31.854,4.105
3921.931,38.079,32.648,3.795
3922.835,32.465,32.191,3.699
3923.738,35.779,32.131,3.76
3924.642,39.949,33.002,3.834
3925.545,36.507,32.641,3.776
3926.449,34.673,32.499,3.662
3927.354,32.44,32.241,3.448
3928.258,31.061,32.264,3.239
3929.163,29.86,31.924,3.06
3930.067,25.365,31.592,3.027
3930.972,20.285,32.406,3.079
3931.878,16.108,32.333,2.974
3932.783,8.464,31.719,2.772
3933.689,11.901,31.579,2.503
3934.595,9.105,31.809,2.348
3935.501,11.3,31.973,2.347
3936.407,15.472,31.981,2.424
3937.314,17.697,31.882,2.613
3938.22,21.475,31.802,2.97
3939.127,29.22,32.342,3.451
3940.034,34.875,32.428,3.933
3940.942,29.443,31.979,4.368
3941.849,32.863,32.395,4.742
3942.757,35.122,32.861,4.893
3943.665,32.882,33.195,4.755
3944.573,35.849,32.836,4.58
3945.481,39.137,32.545,4.517
3946.39,35.423,32.952,4.599
3947.299,32.631,33.38,4.732
3948.208,38.07,32.772,4.752
3949.117,40.492,33.452,4.547
3950.026,39.244,33.013,4.062
3950.936,35.841,32.668,3.783
3951.846,42.711,33.383,3.89
3952.756,37.955,33.559,4.046
3953.666,36.864,33.373,4.021
3954.577,37.677,33.836,3.947
3955.487,37.635,33.676,3.989
3956.398,38.832,33.578,4.03
3957.309,37.419,33.807,3.839
3958.221,35.287,33.42,3.354
3959.132,37.323,33.486,2.917
3960.044,37.807,33.547,2.986
3960.956,39.042,33.348,3.52
3961.868,37.46,33.185,3.983
3962.78,29.059,33.335,3.918
3963.693,28.458,33.511,3.482
3964.606,30.056,33.015,3.037
3965.519,24.355,33.21,2.801
3966.432,20.804,33.176,2.755
3967.345,14.581,32.796,2.65
3968.259,10.035,32.722,2.465
3969.173,6.802,33.192,2.385
3970.087,11.351,33.121,2.308
3971.001,12.799,33.371,2.415
3971.915,14.969,33.086,2.601
3972.83,21.03,32.677,2.828
3973.745,27.162,32.513,3.109
3974.66,30.147,32.153,3.459
3975.575,32.133,31.959,3.831
3976.491,35.522,32.74,4.172
3977.407,34.37,33.436,4.41
3978.323,37.396,33.547,4.564
3979.239,37.756,33.418,4.677
3980.155,36.92,33.69,4.684
3981.072,39.976,34.481,4.662
3981.988,40.462,35.254,4.552
3982.905,37.624,34.921,4.43
3983.823,36.675,34.309,4.408
3984.74,36.654,34.394,4.398
3985.658,36.696,34.9,4.316
3986.576,37.889,35.089,4.15
3987.494,40.251,35.655,4.096
3988.412,42.696,35.514,4.292
3989.33,39.619,35.239,4.582
3990.249,39.883,35.171,4.602
3991.168,41.244,35.079,4.274
3992.087,42.856,36.629,3.956
3993.006,45.244,36.256,4.056
3993.926,43.965,36.159,4.491
3994.846,41.298,36.398,4.589
3995.766,36.343,36.138,4.375
3996.686,38.971,35.784,4.233
3997.606,43.331,36.191,4.353
3998.527,39.317,36.806,4.659
3999.447,37.275,36.643,4.82
4000.369,43.458,36.351,4.834
4001.29,45.568,36.296,4.839
4002.211,41.534,36.107,4.822
4003.133,43.445,36.789,4.52
4004.055,42.207,36.874,4.218
4004.977,40.576,37.235,4.147
4005.899,40.38,36.997,4.513
4006.822,39.185,36.367,4.866
4007.744,41.485,37.101,4.933
4008.667,43.623,37.592,5.019
4009.59,42.611,36.833,5.293
4010.514,40.328,36.528,5.563
4011.437,42.963,36.917,5.569
4012.361,49.276,37.322,5.36
4013.285,46.849,37.412,5.33
4014.209,45.608,37.479,5.35
4015.134,43.334,37.084,5.264
4016.058,46.71,37.03,5.012
4016.983,45.108,37.337,4.727
4017.908,40.795,37.498,4.592
4018.833,42.07,37.422,4.679
4019.759,45.844,37.999,5.029
4020.685,43.795,38.164,5.458
4021.61,40.672,37.96,5.798
4022.537,47.036,37.873,5.918
4023.463,46.003,37.821,5.656
4024.389,45.833,37.525,5.687
4025.316,41.975,38.038,5.684
4026.243,43.889,37.968,5.394
4027.17,46.096,37.946,5.133
4028.098,49.21,38.021,5.005
4029.025,46.863,38.248,4.95
4029.953,40.897,38.79,4.927
4030.881,41.755,38.537,4.851
4031.809,41.409,38.437,4.936
4032.738,38.708,38.165,4.773
4033.667,41.513,37.874,4.693
4034.596,40.048,37.881,4.782
4035.525,40.512,38.07,4.765
4036.454,45.941,38.057,4.573
4037.383,45.672,38.557,4.729
4038.313,46.59,38.569,4.983
4039.243,48.504,38.275,5.079
4040.173,43.773,38.182,5.036
4041.104,43.953,38.344,5.181
4042.034,42.36,38.887,5.638
4042.965,46.581,38.868,5.943
4043.896,42.815,38.715,5.882
4044.827,41.355,39.108,6.316
4045.759,40.698,39.5,8.308
4046.691,35.173,38.809,11.222
4047.622,38.985,38.558,12.702
4048.555,45.635,39.071,11.698
4049.487,42.769,38.734,9.016
4050.419,45.247,38.082,6.842
4051.352,44.861,38.379,5.857
4052.285,46.624,39.325,5.623
4053.218,42.318,38.625,5.466
4054.152,46.103,38.631,5.493
4055.085,42.241,38.788,5.759
4056.019,40.872,38.873,5.717
4056.953,43.14,38.725,5.231
4057.887,44.023,38.097,4.769
4058.822,42.515,38.113,4.796
4059.757,41.349,39.372,5.1
4060.692,45.422,39.138,5.305
4061.627,40.033,38.308,5.452
4062.562,43.366,38.315,5.532
4063.498,41.838,38.616,5.512
4064.433,40.254,38.697,5.542
4065.369,39.571,38.823,5.698
4066.305,42.905,38.511,5.702
4067.242,43.824,38.489,5.706
4068.178,42.651,39.051,5.501
4069.115,46.305,39.323,5.718
4070.052,45.754,39.109,6.186
4070.99,39.927,38.662,6.008
4071.927,39.61,38.669,5.499
4072.865,41.419,39.077,5.208
4073.803,41.941,39.278,5.209
4074.741,42.379,38.857,5.32
4075.679,44.267,38.518,5.487
4076.618,43.886,38.73,5.837
4077.557,41.524,38.786,6.292
4078.496,40.525,38.787,6.399
4079.435,40.138,38.895,6.128
4080.374,42.298,38.856,5.952
4081.314,43.228,38.854,6.049
4082.254,42.203,38.989,6.065
4083.194,43.848,38.352,5.762
4084.134,42.291,38.03,5.33
4085.075,37.586,38.621,5.098
4086.015,39.919,38.861,5.25
4086.956,42.652,38.163,5.59
4087.898,41.081,38.48,5.772
4088.839,44.652,38.593,5.537
4089.781,45.476,38.672,5.067
4090.722,41.455,38.698,4.821
4091.664,45.812,38.131,4.977
4092.607,44.837,38.804,5.247
4093.549,39.137,38.664,5.35
4094.492,40.314,38.39,5.179
4095.435,41.128,38.299,5.01
4096.378,41.231,38.208,4.828
4097.321,41.45,38.042,4.531
4098.265,38.232,37.959,4.268
4099.208,34.352,38.176,4.298
4100.152,31.64,38.115,4.414
4101.097,28.752,37.866,4.405
4102.041,25.474,37.974,4.177
4102.986,24.102,38.476,3.986
4103.931,28.525,38.125,4.07
4104.876,30.506,38.05,4.289
4105.821,34.475,37.751,4.462
4106.766,40.342,37.952,4.559
4107.712,41.022,38.315,4.664
4108.658,37.885,37.691,4.787
4109.604,37.741,38.663,4.843
4110.551,39.544,39.078,4.743
4111.497,41.135,38.882,4.555
4112.444,40.906,38.726,4.503
4113.391,41.507,38.868,4.667
4114.338,41.845,38.969,4.815
4115.286,45.769,38.77,4.825
4116.233,46.959,39.107,4.859
4117.181,44.247,39.577,5.003
4118.13,44.074,39.735,5.014
4119.078,44.291,39.689,4.661
4120.026,42.701,39.861,4.206
4120.975,41.609,40.091,4.186
4121.924,45.817,40.64,4.557
4122.873,46.467,40.681,4.863
4123.823,43.872,40.222,4.808
4124.772,41.118,40.022,4.74
4125.722,45.17,40.53,5.004
4126.672,46.659,40.748,5.249
4127.623,41.615,41.434,5.137
4128.573,40.681,41.107,5.002
4129.524,42.41,40.543,5.125
4130.475,44.146,40.516,5.03
4131.426,41.064,40.426,4.953
4132.378,46.715,40.514,5.224
4133.329,44.575,40.415,5.285
4134.281,41.993,40.947,5.025
4135.233,44.816,41.065,4.874
4136.185,43.665,40.913,4.998
4137.138,42.762,41.978,5.071
4138.091,44.96,41.998,4.965
4139.044,44.864,41.806,5.003
4139.997,52.098,41.81,5.163
4140.95,50.878,41.705,5.254
4141.904,48.602,41.545,5.086
4142.858,42.453,41.728,4.599
4143.812,45.305,41.595,4.344
4144.766,40.753,41.938,4.477
4145.72,44.908,41.568,4.734
4146.675,48.721,41.642,4.839
4147.63,46.285,42.043,4.781
4148.585,46.712,41.12,4.667
4149.54,45.368,41.707,4.483
4150.496,47.069,41.828,4.229
4151.452,46.886,42.14,4.222
4152.408,46.696,42.195,4.424
4153.364,44.849,42.081,4.548
4154.321,42.092,42.744,4.539
4155.277,41.254,42.76,4.646
4156.234,43.866,42.704,4.889
4157.191,45.11,41.949,5.07
4158.149,44.321,41.953,5.095
4159.106,44.915,42.638,5.068
4160.064,44.457,42.077,5.087
4161.022,42.469,42.309,5.162
4161.98,43.024,42.093,5.191
4162.939,46.248,42.476,5.28
4163.897,48.0,43.005,5.588
4164.856,45.245,42.881,5.973
4165.815,47.182,42.547,6.091
4166.775,49.668,43.119,5.929
4167.734,49.06,42.436,5.763
4168.694,47.05,42.376,5.747
4169.654,43.81,42.646,5.668
4170.614,42.28,42.689,5.559
4171.574,46.025,42.913,5.573
4172.535,41.371,42.372,5.448
4173.496,40.295,43.106,5.25
4174.457,39.916,42.924,5.203
4175.418,43.529,42.528,5.229
4176.38,42.327,42.747,5.138
4177.342,43.5,42.535,5.022
4178.304,44.608,42.143,5.056
4179.266,42.908,43.058,5.122
4180.228,44.382,43.003,5.125
4181.191,44.824,42.572,5.226
4182.154,43.957,42.867,5.081
4183.117,44.89,43.143,4.657
4184.08,44.32,42.636,4.448
4185.044,44.225,44.027,4.649
4186.007,44.878,43.833,4.648
4186.971,40.515,43.353,4.433
4187.936,41.693,43.819,4.302
4188.9,44.672,43.453,4.34
4189.865,44.577,43.121,4.441
4190.83,45.397,43.519,4.519
4191.795,43.017,43.561,4.689
4192.76,45.472,43.797,4.889
4193.725,45.144,44.25,4.895
4194.691,49.689,43.938,4.772
4195.657,46.484,44.202,4.638
4196.623,43.691,43.941,4.376
4197.59,43.344,43.524,4.183
4198.556,40.786,43.813,4.009
4199.523,42.307,44.315,3.913
4200.49,46.108,43.914,4.059
4201.458,44.772,44.366,4.262
4202.425,45.285,44.185,4.129
4203.393,44.683,43.843,3.93
4204.361,48.758,43.965,4.146
4205.329,42.15,44.256,4.286
4206.298,44.895,44.063,4.402
4207.266,46.958,44.224,4.736
4208.235,46.017,44.422,5.063
4209.204,46.044,45.001,5.088
4210.174,45.227,45.07,4.814
4211.143,45.647,44.552,4.559
4212.113,50.86,44.655,4.524
4213.083,51.639,44.845,4.57
4214.053,43.285,44.814,4.628
4215.023,45.655,44.955,4.551
4215.994,43.27,44.964,4.388
4216.965,44.799,44.561,4.313
4217.936,47.043,44.171,4.436
4218.907,44.311,44.441,4.487
4219.879,44.773,44.61,4.432
4220.851,47.821,45.088,4.433
4221.823,48.352,45.101,4.519
4222.795,44.853,44.378,4.551
4223.767,42.696,44.16,4.46
4224.74,46.553,45.173,4.35
4225.713,41.253,45.469,4.222
4226.686,39.142,45.08,3.905
4227.659,41.498,44.523,3.664
4228.633,41.016,44.053,3.774
4229.607,46.526,43.766,4.159
4230.581,47.547,44.052,4.442
4231.555,48.304,44.563,4.455
4232.53,47.861,44.376,4.404
4233.504,44.356,44.344,4.388
4234.479,43.337,44.796,4.321
4235.454,42.488,44.8,4.224
4236.43,41.989,44.911,4.371
4237.405,45.706,44.492,4.528
4238.381,42.583,44.437,4.317
4239.357,45.088,44.37,4.109
4240.333,43.99,44.16,3.921
4241.31,45.802,44.503,4.158
4242.287,45.739,44.741,4.433
4243.263,45.605,45.204,4.305
4244.241,45.592,45.273,4.293
4245.218,44.803,45.089,4.696
4246.196,45.524,45.086,5.215
4247.173,44.026,45.2,5.437
4248.152,48.12,44.813,5.622
4249.13,42.682,44.491,5.411
4250.108,44.889,45.34,4.66
4251.087,44.127,45.687,4.462
4252.066,44.367,44.905,4.727
4253.045,46.884,44.836,4.92
4254.025,49.188,45.359,4.895
4255.004,46.829,45.522,4.813
4255.984,44.365,45.7,4.774
4256.964,47.708,45.629,4.774
4257.945,48.823,45.41,4.92
4258.925,47.565,45.66,5.022
4259.906,45.549,45.487,4.708
4260.887,48.647,44.794,4.328
4261.868,42.064,44.919,4.257
4262.849,47.205,45.381,4.421
4263.831,50.772,45.396,4.523
4264.813,46.59,45.286,4.562
4265.795,43.715,45.68,4.773
4266.778,44.791,45.736,5.105
4267.76,45.026,46.235,5.124
4268.743,44.621,46.081,4.86
4269.726,46.467,45.267,4.824
4270.709,45.637,45.544,4.824
4271.693,40.917,45.649,4.604
4272.676,43.478,44.88,4.4
4273.66,43.376,44.912,4.469

Tags: csv数据intestimportforasline
1条回答
网友
1楼 · 发布于 2024-10-05 10:34:47
  • 使用OP中的数据和问题中的代码,会产生与问题中所示相同的意外绘图
  • 给出了读取数据的原始方法
    • 有许多不必要的步骤
    • 绘图不起作用,因为数据从未从str转换为float
      • 使用x.append(float(point[0]))&y.append(float(point[1]))
  • 一旦数据位于正确的dtype中,API将更好地分隔轴刻度标签。当轴值为字符串时,每个值都显示为记号标签
  • 此问题是将string{}数据传递到绘图API的结果。
    • 通过将数据转换为正确的type(例如intfloatdatetime)来解决此问题
  • 另见What is plotted when string data is passed to the matplotlib API?

原来的实施是固定的

  • 可以使用^{}模块,但是下面修复了现有代码
data = 'spectrum.csv'

with open(data, "r") as f:
    rawData = [line.strip().split(',') for line in f]  # remove \n from each string and split into a list

wavelength = list()
bestfit = list()

for x in rawData[1:]:  # iterate through the raw data rows
    wavelength.append(float(x[0]))  # add data to list and convert to float
    bestfit.append(float(x[2]))  # add data to list and convert to float

# plot
p = plt.plot(wavelength, bestfit)  # generates plot of BestFit line as above

使用pandas

  • 使用^{}自动读取带有正确dtypefloat的数据,然后生成所需的绘图
import pandas as pd
import matplotlib.pyplot as plt

# created dataframe
df = pd.read_csv('spectrum.csv')

# display dataframe
   Wavelength    Flux  BestFit  SkyFlux
0    3809.781  29.277   26.563    4.139
1    3810.658  28.131   25.925    4.118
2    3811.536  30.299   26.545    3.995
3    3812.414  26.515   27.773    4.162
4    3813.291  26.534   26.918    4.089

# print(df.info())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3840 entries, 0 to 3839
Data columns (total 4 columns):
 #   Column      Non-Null Count  Dtype  
 -                      -  
 0   Wavelength  3840 non-null   float64
 1   Flux        3840 non-null   float64
 2   BestFit     3840 non-null   float64
 3   SkyFlux     3840 non-null   float64
dtypes: float64(4)
memory usage: 120.1 KB

# plot dataframe
ax = df.plot(x='Wavelength')
ax.set_yscale('log')
plt.show()

enter image description here

作为子地块

axes = df.plot(x='Wavelength', subplots=True, layout=(3, 1), sharey=True)
axes = axes.ravel()
for ax in axes:
    ax.set_yscale('log')
plt.show()

enter image description here

相关问题 更多 >

    热门问题