如何在Python Plotly中設定Y軸的範圍?

PHPz
發布: 2023-08-26 11:13:01
轉載
2339 人瀏覽過

Plotly支援在X軸和Y軸上設定範圍。讓我們了解如何在Plotly中設定Y軸的範圍。

  • plotly.graph_objects is used to generate figures. It contains a lot of methods to customize charts and render a chart in HTML format.

  • #Create a numpy module and generate random ranges for both X and Y axis.

  • #建立Figure()方法,以線條模式繪製X軸和Y軸

  • Create update_layout() method and set the Y-axis range.

依照給定的步驟來設定Plotly中Y軸的範圍。

第一步 - 導入plotly

匯入 plotly.graph_objs 模組,並將其別名為 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>
登入後複製

Step 2 − Import numpy

匯入 numpy 模組並將其別名設為 np,並設定隨機 種子 值。

<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>
登入後複製

Step 3 − Generate random number on X-axis

#Let us generate a list of random range of numbers on X-axis.

<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>
登入後複製

第四步 - 在Y軸上產生隨機數字

在Y軸上產生隨機數如下 -

<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>
登入後複製

Step 5 − Generate the scatter plot

#Let us generate the scatter plot with the following coordinates −

<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>
登入後複製

Step 6 − Set the Y-axis range

使用update_layout()方法設定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>
登入後複製

步驟 7 - 顯示圖片

使用 show() 方法顯示圖表。

<span class="pln">fig</span><span class="pun">.</span><span class="pln">show</span><span class="pun">()</span>
登入後複製

Example

的中文翻譯為:

範例

在Python Plotly中設定Y軸範圍的完整程式碼如下:

<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

#它將在瀏覽器上顯示以下輸出 −

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

##

以上是如何在Python Plotly中設定Y軸的範圍?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!