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>
在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>
#它將在瀏覽器上顯示以下輸出 −
##以上是如何在Python Plotly中設定Y軸的範圍?的詳細內容。更多資訊請關注PHP中文網其他相關文章!