94703


pythonのmatplotlibを使ってグラフを作成しています。  
1枚目の全体図から２枚目の図にset_ylimを使って[-58,
-56]に範囲指定をして表示させています。その際に目盛りの間隔を調整して小数点以下の値をなくしたいのですが、調べてみたところ、set_ylimだとできそうにないので、代わりにset_yticsを使い、set_yticks(np.arange(-58,
-56,
1))とすると３枚目のように-56よりも大きい部分も表示されてしまい、上手くいきません。表示範囲を指定しつつ目盛りの間隔を調整するにはどうすれば良いでしょうか？set_yticks(np.linspace(-58,
-56, 1))も試しましたが３枚目と同様の表示になりました。  
set_ylimとset_yticsを同時に使ってみても３枚目と同様の表示になりました。  
用いたコードとデータも示しておきます。

[![全体図](https://i.stack.imgur.com/O7pdm.png)](https://i.stack.imgur.com/O7pdm.png)  
[![ylimで軸の範囲指定をした図](https://i.stack.imgur.com/XU6Y0.png)](https://i.stack.imgur.com/XU6Y0.png)  
[![軸の目盛りの間隔を調整しようとしておかしくなった図](https://i.stack.imgur.com/DcWqn.png)](https://i.stack.imgur.com/DcWqn.png)

[code]

    import numpy as np
    import matplotlib.pyplot as plt
    
    data_axis4, data_value4, data_value41, data_value42, data_value43 = np.loadtxt("modi_sub_spectrum.dat", comments = '!', unpack = True)
    
    fig, ax = plt.subplots(1,1, figsize = (4, 4), squeeze = False, tight_layout = True)
    
    ax[0][0].plot(data_axis4, data_value42, linestyle = '-', lw=2.5, color = "k", label = "Power")
    ax[0][0].set_xlabel("Frequency[GHz]", fontsize=35)
    ax[0][0].set_ylabel("Power[dB]", fontsize=35)
    #ax[0][0].set_ylim(-58, -56)
    ax[0][0].set_yticks(np.arange(-58, -56, 1))
    ax[0][0].legend(loc = "lower center", bbox_to_anchor = (.5, 1.0), frameon = False, fontsize=35)
    ax[0][0].tick_params(axis='x', which='major', labelsize=35)
    ax[0][0].tick_params(axis='y', which='major', labelsize=35)
    
    plt.show()
    
[/code]

[code]

    6.875000 0.0 -49.712481 -49.712481 0.0
    6.875029 nan -49.750332 -49.750332 -inf
    6.875059 nan -49.789488 -49.789488 -inf
    6.875088 nan -49.836692 -49.836692 -inf
    6.875117 nan -49.936209 -49.936209 -inf
    6.875146 nan -50.037595 -50.037595 -inf
    6.875176 nan -50.082141 -50.082141 -inf
    6.875205 nan -50.193007 -50.193007 -inf
    6.875234 nan -50.262921 -50.262921 -inf
    6.875264 nan -50.235701 -50.235701 -inf
    6.875293 nan -50.207080 -50.207080 -inf
    6.875322 nan -50.283403 -50.283403 -inf
    6.875352 nan -50.350929 -50.350929 -inf
    6.875381 nan -50.354985 -50.354985 -inf
    6.875410 nan -50.378959 -50.378959 -inf
    6.875439 nan -50.386481 -50.386481 -inf
    6.875469 nan -50.381175 -50.381175 -inf
    6.875498 nan -50.367296 -50.367296 -inf
    6.875527 nan -50.335636 -50.335636 -inf
    6.875557 nan -50.404234 -50.404234 -inf
    6.875586 nan -50.477661 -50.477661 -inf
    6.875615 nan -50.441110 -50.441110 -inf
    6.875645 nan -50.384208 -50.384208 -inf
    6.875674 nan -50.439203 -50.439203 -inf
    6.875703 nan -50.588825 -50.588825 -inf
    6.875732 nan -50.681688 -50.681688 -inf
    6.875762 nan -50.610474 -50.610474 -inf
    6.875791 nan -50.530977 -50.530977 -inf
    6.875820 nan -50.502358 -50.502358 -inf
    6.875850 nan -50.530413 -50.530413 -inf
    6.875879 nan -50.556472 -50.556472 -inf
    6.875908 nan -50.547726 -50.547726 -inf
    6.875938 nan -50.505140 -50.505140 -inf
    6.875967 nan -50.440467 -50.440467 -inf
    6.875996 nan -50.464695 -50.464695 -inf
    6.876025 nan -50.503300 -50.503300 -inf
    6.876055 nan -50.483720 -50.483720 -inf
    6.876084 nan -50.408642 -50.408642 -inf
    6.876113 nan -50.391526 -50.391526 -inf
    6.876143 nan -50.483123 -50.483123 -inf
    6.876172 nan -50.509681 -50.509681 -inf
    6.876201 nan -50.455169 -50.455169 -inf
    6.876230 nan -50.471186 -50.471186 -inf
    6.876260 nan -50.514105 -50.514105 -inf
    6.876289 nan -50.460034 -50.460034 -inf
    6.876318 nan -50.389080 -50.389080 -inf
    6.876348 nan -50.344029 -50.344029 -inf
    6.876377 nan -50.334080 -50.334080 -inf
    6.876406 nan -50.346895 -50.346895 -inf
    6.876436 nan -50.260121 -50.260121 -inf
    6.876465 nan -50.212231 -50.212231 -inf
    6.876494 nan -50.264467 -50.264467 -inf
    6.876523 nan -50.362046 -50.362046 -inf
    6.876553 nan -50.353696 -50.353696 -inf
    6.876582 nan -50.350442 -50.350442 -inf
    6.876611 nan -50.337984 -50.337984 -inf
    6.876641 nan -50.284363 -50.284363 -inf
    6.876670 nan -50.266644 -50.266644 -inf
    6.876699 nan -50.276656 -50.276656 -inf
    6.876729 nan -50.231064 -50.231064 -inf
    6.876758 nan -50.165537 -50.165537 -inf
    6.876787 nan -50.091102 -50.091102 -inf
    6.876816 nan -50.079811 -50.079811 -inf
    6.876846 nan -50.200739 -50.200739 -inf
    6.876875 nan -50.239681 -50.239681 -inf
    6.876904 nan -50.198859 -50.198859 -inf
    6.876934 nan -50.175637 -50.175637 -inf
    6.876963 nan -50.152723 -50.152723 -inf
    6.876992 nan -50.151388 -50.151388 -inf
    6.877021 nan -50.095805 -50.095805 -inf
    6.877051 nan -50.023432 -50.023432 -inf
    6.877080 nan -49.964115 -49.964115 -inf
    6.877109 nan -50.026361 -50.026361 -inf
    6.877139 nan -50.092288 -50.092288 -inf
    6.877168 nan -50.056214 -50.056214 -inf
    6.877197 nan -50.068118 -50.068118 -inf
    6.877227 nan -50.067595 -50.067595 -inf
    6.877256 nan -50.012719 -50.012719 -inf
    6.877285 nan -49.984007 -49.984007 -inf
    6.877314 nan -50.040228 -50.040228 -inf
    6.877344 nan -50.105635 -50.105635 -inf
    6.877373 nan -50.096553 -50.096553 -inf
    6.877402 nan -50.031739 -50.031739 -inf
    6.877432 nan -50.020086 -50.020086 -inf
    6.877461 nan -50.060188 -50.060188 -inf
    6.877490 nan -50.024616 -50.024616 -inf
    6.877520 nan -50.052410 -50.052410 -inf
    6.877549 nan -50.131570 -50.131570 -inf
    6.877578 nan -50.123401 -50.123401 -inf
    6.877607 nan -50.109487 -50.109487 -inf
    6.877637 nan -50.130126 -50.130126 -inf
    6.877666 nan -50.195506 -50.195506 -inf
    6.877695 nan -50.155794 -50.155794 -inf
    6.877725 nan -50.065169 -50.065169 -inf
    6.877754 nan -50.072044 -50.072044 -inf
    6.877783 nan -50.146648 -50.146648 -inf
    6.877813 nan -50.159008 -50.159008 -inf
    6.877842 nan -50.177692 -50.177692 -inf
    6.877871 nan -50.156946 -50.156946 -inf
    6.877900 nan -50.114335 -50.114335 -inf
    6.877930 nan -50.088416 -50.088416 -inf
    6.877959 nan -50.076318 -50.076318 -inf
    6.877988 nan -50.162140 -50.162140 -inf
    6.878018 nan -50.145060 -50.145060 -inf
    6.878047 nan -50.074781 -50.074781 -inf
    6.878076 nan -50.149664 -50.149664 -inf
    6.878105 nan -50.249763 -50.249763 -inf
    6.878135 nan -50.307675 -50.307675 -inf
    6.878164 nan -50.259312 -50.259312 -inf
    6.878193 nan -50.140247 -50.140247 -inf
    6.878223 nan -50.095891 -50.095891 -inf
    6.878252 nan -50.153015 -50.153015 -inf
    6.878281 nan -50.195483 -50.195483 -inf
    6.878311 nan -50.203952 -50.203952 -inf
    6.878340 nan -50.175352 -50.175352 -inf
    6.878369 nan -50.093387 -50.093387 -inf
    6.878398 nan -50.022837 -50.022837 -inf
    6.878428 nan -50.009407 -50.009407 -inf
    6.878457 nan -50.098201 -50.098201 -inf
    6.878486 nan -50.123868 -50.123868 -inf
    6.878516 nan -50.158858 -50.158858 -inf
    6.878545 nan -50.154339 -50.154339 -inf
    6.878574 nan -50.140235 -50.140235 -inf
    6.878604 nan -50.173217 -50.173217 -inf
    6.878633 nan -50.180851 -50.180851 -inf
    6.878662 nan -50.164846 -50.164846 -inf
    6.878691 nan -50.076173 -50.076173 -inf
    6.878721 nan -50.045093 -50.045093 -inf
    6.878750 nan -50.084571 -50.084571 -inf
    6.878779 nan -50.085710 -50.085710 -inf
    6.878809 nan -50.115596 -50.115596 -inf
    6.878838 nan -50.093670 -50.093670 -inf
    6.878867 nan -50.012266 -50.012266 -inf
    6.878896 nan -50.000124 -50.000124 -inf
    6.878926 nan -50.091896 -50.091896 -inf
    6.878955 nan -50.112535 -50.112535 -inf
    6.878984 nan -50.077113 -50.077113 -inf
    6.879014 nan -50.045095 -50.045095 -inf
    6.879043 nan -50.181747 -50.181747 -inf
    6.879072 nan -50.308497 -50.308497 -inf
    6.879102 nan -50.263720 -50.263720 -inf
    6.879131 nan -50.172506 -50.172506 -inf
    6.879160 nan -50.176121 -50.176121 -inf
    6.879189 nan -50.206312 -50.206312 -inf
    6.879219 nan -50.179665 -50.179665 -inf
    6.879248 nan -50.162737 -50.162737 -inf
    6.879277 nan -50.213652 -50.213652 -inf
    6.879307 nan -50.252922 -50.252922 -inf
    6.879336 nan -50.216573 -50.216573 -inf
    6.879365 nan -50.233266 -50.233266 -inf
    6.879395 nan -50.244995 -50.244995 -inf
    6.879424 nan -50.212705 -50.212705 -inf
    6.879453 nan -50.181096 -50.181096 -inf
    6.879482 nan -50.195058 -50.195058 -inf
    6.879512 nan -50.216554 -50.216554 -inf
    6.879541 nan -50.268270 -50.268270 -inf
    6.879570 nan -50.309167 -50.309167 -inf
    6.879600 nan -50.288590 -50.288590 -inf
    6.879629 nan -50.266178 -50.266178 -inf
    6.879658 nan -50.264961 -50.264961 -inf
    6.879688 nan -50.308623 -50.308623 -inf
    6.879717 nan -50.391527 -50.391527 -inf
    6.879746 nan -50.492379 -50.492379 -inf
    6.879775 nan -50.517403 -50.517403 -inf
    6.879805 nan -50.370884 -50.370884 -inf
    6.879834 nan -50.241431 -50.241431 -inf
    6.879863 nan -50.252909 -50.252909 -inf
    6.879893 nan -50.351399 -50.351399 -inf
    6.879922 nan -50.416911 -50.416911 -inf
    6.879951 nan -50.339409 -50.339409 -inf
    6.879980 nan -50.490017 -50.490017 -inf
    6.880010 nan -50.613564 -50.613564 -inf
    6.880039 nan -50.471063 -50.471063 -inf
    6.880068 nan -50.479829 -50.479829 -inf
    6.880098 nan -50.468977 -50.468977 -inf
    6.880127 nan -50.423129 -50.423129 -inf
    6.880156 nan -50.453628 -50.453628 -inf
    6.880186 nan -50.574730 -50.574730 -inf
    6.880215 nan -50.638829 -50.638829 -inf
    6.880244 nan -50.666469 -50.666469 -inf
    6.880273 nan -51.470514 -51.470514 -inf
    6.880303 nan -54.145429 -54.145429 -inf
    6.880332 -56.798426 -56.798426 -56.798426 -56.798426
    6.880361 -57.277159 -57.277159 -57.277159 -57.277159
    6.880391 -57.254014 -57.254014 -57.254014 -57.254014
    6.880420 -57.290470 -57.290470 -57.290470 -57.290470
    6.880449 -57.291084 -57.291084 -57.291084 -57.291084
    6.880479 -57.254405 -57.254405 -57.254405 -57.254405
    6.880508 -57.297125 -57.297125 -57.297125 -57.297125
    6.880537 -57.323129 -57.323129 -57.323129 -57.323129
    6.880566 -57.330465 -57.330465 -57.330465 -57.330465
    6.880596 -57.334400 -57.334400 -57.334400 -57.334400
    6.880625 -57.271129 -57.271129 -57.271129 -57.271129
    6.880654 -57.220006 -57.220006 -57.220006 -57.220006
    6.880684 -57.277797 -57.277797 -57.277797 -57.277797
    6.880713 -57.323719 -57.323719 -57.323719 -57.323719
    6.880742 -57.303045 -57.303045 -57.303045 -57.303045
    6.880771 -57.267439 -57.267439 -57.267439 -57.267439
    6.880801 -57.256123 -57.256123 -57.256123 -57.256123
    6.880830 -57.298360 -57.298360 -57.298360 -57.298360
    6.880859 -57.306241 -57.306241 -57.306241 -57.306241
    6.880889 -57.293854 -57.293854 -57.293854 -57.293854
    6.880918 -57.286062 -57.286062 -57.286062 -57.286062
    6.880947 -57.288687 -57.288687 -57.288687 -57.288687
    6.880977 -57.321115 -57.321115 -57.321115 -57.321115
    6.881006 -57.287595 -57.287595 -57.287595 -57.287595
    6.881035 -57.256606 -57.256606 -57.256606 -57.256606
    6.881064 -57.264220 -57.264220 -57.264220 -57.264220
    6.881094 -57.294620 -57.294620 -57.294620 -57.294620
    6.881123 -57.364265 -57.364265 -57.364265 -57.364265
    6.881152 -57.343847 -57.343847 -57.343847 -57.343847
    6.881182 -57.237118 -57.237118 -57.237118 -57.237118
    6.881211 -57.196744 -57.196744 -57.196744 -57.196744
    6.881240 -57.237982 -57.237982 -57.237982 -57.237982
    6.881270 -57.270205 -57.270205 -57.270205 -57.270205
    6.881299 -57.281397 -57.281397 -57.281397 -57.281397
    6.881328 -57.278935 -57.278935 -57.278935 -57.278935
    6.881357 -57.261485 -57.261485 -57.261485 -57.261485
    6.881387 -57.228955 -57.228955 -57.228955 -57.228955
    6.881416 -57.117637 -57.117637 -57.117637 -57.117637
    6.881445 nan -inf -inf -inf
    6.881475 nan -inf -inf -inf
    6.881504 nan -inf -inf -inf
    6.881533 nan -inf -inf -inf
    6.881563 nan -inf -inf -inf
    6.881592 -57.142454 -57.142454 -57.142454 -57.142454
    6.881621 -57.182646 -57.182646 -57.182646 -57.182646
    6.881650 -57.153363 -57.153363 -57.153363 -57.153363
    6.881680 -57.144751 -57.144751 -57.144751 -57.144751
    6.881709 -57.147614 -57.147614 -57.147614 -57.147614
    6.881738 -57.171187 -57.171187 -57.171187 -57.171187
    6.881768 -57.266886 -57.266886 -57.266886 -57.266886
    6.881797 -57.264420 -57.264420 -57.264420 -57.264420
    6.881826 -57.177223 -57.177223 -57.177223 -57.177223
    6.881855 -57.139956 -57.139956 -57.139956 -57.139956
    6.881885 -57.141170 -57.141170 -57.141170 -57.141170
    6.881914 -57.172848 -57.172848 -57.172848 -57.172848
    6.881943 -57.180062 -57.180062 -57.180062 -57.180062
    6.881973 -57.142554 -57.142554 -57.142554 -57.142554
    6.882002 -57.120000 -57.120000 -57.120000 -57.120000
    6.882031 -57.126649 -57.126649 -57.126649 -57.126649
    6.882061 -57.121884 -57.121884 -57.121884 -57.121884
    6.882090 -57.102750 -57.102750 -57.102750 -57.102750
    6.882119 -57.134014 -57.134014 -57.134014 -57.134014
    6.882148 -57.164235 -57.164235 -57.164235 -57.164235
    6.882178 -57.167273 -57.167273 -57.167273 -57.167273
    6.882207 -57.178318 -57.178318 -57.178318 -57.178318
    6.882236 -57.220910 -57.220910 -57.220910 -57.220910
    6.882266 -57.195967 -57.195967 -57.195967 -57.195967
                             ・
    　　　　　　　　　　　　　　　・
    　　　　　　　　　　　　　　　・
    
[/code]


CC BY-SA 4.0
2023-05-01T06:54:58.563
94702
2023-05-01T07:13:10.103
52666
question
python3 matplotlib
matplotlibで軸の目盛りの間隔を調整したい


set_ylimとset_yticksを両方とも指定してあげるとよいかと思います。

[code]

    ax[0][0].set_ylim(-58, -56)
    ax[0][0].set_yticks(np.arange(-58, -56, 1))
    
[/code]

なお、結果画像ではフォントサイズは大きすぎたようなのでラベルなどは描画していません。  
[![画像の説明をここに入力](https://i.stack.imgur.com/MrTFF.png)](https://i.stack.imgur.com/MrTFF.png)


CC BY-SA 4.0
2023-05-01T07:13:10.103
94703
2023-05-01T07:13:10.103
25830
94702
answer