Plotly prend en charge les plages de réglage sur les axes X et Y. Voyons comment définir la plage de l'axe Y dans Plotly.
plotly.graph_objects est utilisé pour générer des figures. Il contient de nombreuses méthodes pour personnaliser des graphiques et restituer un graphique au format HTML.
Créez un module numpy et générez des plages aléatoires pour les axes X et Y.
Créez la méthode Figure() pour dessiner l'axe X et l'axe Y en mode ligne
Créez la méthode update_layout() et définissez la plage de l'axe Y.
Suivez les étapes indiquées pour définir la plage de l'axe Y dans Plotly.
Première étape : importer plotly
Importez le module plotly.graph_objs et alias-le vers 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>
Étape 2 - Importer numpy
Importez le module numpy et définissez son alias sur np et définissez une valeur seed aléatoire.
<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>
Étape 3 - Générer un nombre aléatoire sur l'axe X
Générons une liste de plages aléatoires de nombres sur l'axe 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>
Étape 4 - Générer des nombres aléatoires sur l'axe Y
Générez des nombres aléatoires sur l'axe Y comme suit -
<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>
Étape 5 - Générer le nuage de points
Générons le nuage de points avec les coordonnées suivantes −
<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>
Étape 6 - Définir la plage de l'axe Y
Utilisez la méthode update_layout() pour définir la plage de l'axe 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>
Étape 7 - Afficher l'image
Utilisez la méthode show() pour afficher le graphique.
<span class="pln">fig</span><span class="pun">.</span><span class="pln">show</span><span class="pun">()</span>
Le code complet pour définir la plage de l'axe Y dans Python Plotly est le suivant :
<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>
Il affichera la sortie suivante sur le navigateur −
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!