Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Bagaimana untuk menetapkan julat paksi-Y dalam Python Plotly?

Bagaimana untuk menetapkan julat paksi-Y dalam Python Plotly?

PHPz
PHPzke hadapan
2023-08-26 11:13:012341semak imbas

Plotly menyokong julat tetapan pada paksi-X dan paksi-Y. Mari kita fahami cara menetapkan julat paksi-Y dalam Plotly.

  • plotly.graph_objects digunakan untuk menjana angka Ia mengandungi banyak kaedah untuk menyesuaikan carta dan menghasilkan carta dalam format HTML.

  • Buat modul numpy dan jana julat rawak untuk paksi X dan Y.

  • Buat kaedah Rajah() untuk melukis paksi-X dan paksi-Y dalam mod garisan

  • Buat kaedah update_layout() dan tetapkan julat paksi-Y.

Ikuti langkah yang diberikan untuk menetapkan julat paksi-Y dalam Plotly.

Langkah pertama - import plotly

Import modul plotly.graph_objs dan alias kepada go

<span class="kwd">import</span><span class="pln"> plotly</span><span class="pun">.</span><span class="pln">graphs_objs </span><span class="kwd">as</span><span class="pln"> go</span>

Langkah 2 − Import numpy

Import modul numpy dan tetapkan aliasnya kepada np dan tetapkan nilai seed rawak.

<span class="kwd">import</span><span class="pln"> numpy </span><span class="kwd">as</span><span class="pln"> np
np</span><span class="pun">.</span><span class="pln">random</span><span class="pun">.</span><span class="pln">seed</span><span class="pun">(</span><span class="lit">3</span><span class="pun">)</span>

Langkah 3 − Jana nombor rawak pada paksi X

Mari kami menjana senarai julat nombor rawak pada paksi-X.

<span class="pln">x </span><span class="pun">=</span><span class="pln"> list</span><span class="pun">(</span><span class="pln">range</span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="lit">20</span><span class="pun">,</span><span class="lit">2</span><span class="pun">))</span>

Langkah 4 - Hasilkan nombor rawak pada paksi Y

Hasilkan nombor rawak pada paksi Y seperti berikut -

<span class="pln">y </span><span class="pun">=</span><span class="pln"> np</span><span class="pun">.</span><span class="pln">random</span><span class="pun">.</span><span class="pln">randn</span><span class="pun">(</span><span class="lit">10</span><span class="pun">)</span>

Langkah 5 − Hasilkan plot taburan

Mari kita jana plot serakan dengan koordinat berikut −

<span class="pln">fig </span><span class="pun">=</span><span class="pln"> go</span><span class="pun">.</span><span class="typ">Figure</span><span class="pun">(</span><span class="pln">data</span><span class="pun">=</span><span class="pln">go</span><span class="pun">.</span><span class="typ">Scatter</span><span class="pun">(</span><span class="pln">x</span><span class="pun">=</span><span class="pln">x</span><span class="pun">,</span><span class="pln"> y</span><span class="pun">=</span><span class="pln">y</span><span class="pun">,</span><span class="pln"> mode</span><span class="pun">=</span><span class="str">'lines'</span><span class="pun">))</span>

Langkah 6 − Tetapkan julat paksi Y

Gunakan kaedah update_layout() untuk menetapkan julat paksi-Y.

<span class="pln">fig</span><span class="pun">.</span><span class="pln">update_layout</span><span class="pun">(</span><span class="pln">yaxis_range</span><span class="pun">=[-</span><span class="lit">3</span><span class="pun">,</span><span class="lit">3</span><span class="pun">])</span>

Langkah 7 - Paparkan Imej

Gunakan kaedah show() untuk memaparkan carta.

<span class="pln">fig</span><span class="pun">.</span><span class="pln">show</span><span class="pun">()</span>
Terjemahan bahasa Cina bagi

Contoh

ialah:

Contoh

Kod lengkap untuk menetapkan julat paksi-Y dalam Python Plotly adalah seperti berikut:

<span class="com"># Importing Libraries</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> plotly</span><span class="pun">.</span><span class="pln">graph_objs </span><span class="kwd">as</span><span class="pln"> go
</span><span class="kwd">import</span><span class="pln"> numpy </span><span class="kwd">as</span><span class="pln"> np

np</span><span class="pun">.</span><span class="pln">random</span><span class="pun">.</span><span class="pln">seed</span><span class="pun">(</span><span class="lit">3</span><span class="pun">)</span><span class="pln">

</span><span class="com"># generating numbers ranging from 0 to 18 on X-axis</span><span class="pln">
x </span><span class="pun">=</span><span class="pln"> list</span><span class="pun">(</span><span class="pln">range</span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="lit">20</span><span class="pun">,</span><span class="lit">2</span><span class="pun">))</span><span class="pln">

</span><span class="com"># generating random numbers on y-axis</span><span class="pln">
y </span><span class="pun">=</span><span class="pln"> np</span><span class="pun">.</span><span class="pln">random</span><span class="pun">.</span><span class="pln">randn</span><span class="pun">(</span><span class="lit">10</span><span class="pun">)</span><span class="pln">

</span><span class="com"># plotting scatter plot on x and y data with</span><span class="pln">
</span><span class="com"># 'lines' as mode</span><span class="pln">
fig </span><span class="pun">=</span><span class="pln"> go</span><span class="pun">.</span><span class="typ">Figure</span><span class="pun">(</span><span class="pln">data</span><span class="pun">=</span><span class="pln">go</span><span class="pun">.</span><span class="typ">Scatter</span><span class="pun">(</span><span class="pln">x</span><span class="pun">=</span><span class="pln">x</span><span class="pun">,</span><span class="pln"> y</span><span class="pun">=</span><span class="pln">y</span><span class="pun">,</span><span class="pln"> mode</span><span class="pun">=</span><span class="str">'lines'</span><span class="pun">))</span><span class="pln">

</span><span class="com"># setting the y-axis range from -3 to 3</span><span class="pln">
fig</span><span class="pun">.</span><span class="pln">update_layout</span><span class="pun">(</span><span class="pln">yaxis_range</span><span class="pun">=[-</span><span class="lit">3</span><span class="pun">,</span><span class="lit">3</span><span class="pun">])</span><span class="pln">

</span><span class="com"># to display the figure in the output screen</span><span class="pln">
fig</span><span class="pun">.</span><span class="pln">show</span><span class="pun">()</span>

Output

Ia akan memaparkan output berikut pada pelayar −

如何在Python Plotly中设置Y轴的范围?

Atas ialah kandungan terperinci Bagaimana untuk menetapkan julat paksi-Y dalam Python Plotly?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:tutorialspoint.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam