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中文网其他相关文章!