<bdo id='DiM9G'></bdo><ul id='DiM9G'></ul>

    <small id='DiM9G'></small><noframes id='DiM9G'>

    <tfoot id='DiM9G'></tfoot>
  • <legend id='DiM9G'><style id='DiM9G'><dir id='DiM9G'><q id='DiM9G'></q></dir></style></legend>
      <i id='DiM9G'><tr id='DiM9G'><dt id='DiM9G'><q id='DiM9G'><span id='DiM9G'><b id='DiM9G'><form id='DiM9G'><ins id='DiM9G'></ins><ul id='DiM9G'></ul><sub id='DiM9G'></sub></form><legend id='DiM9G'></legend><bdo id='DiM9G'><pre id='DiM9G'><center id='DiM9G'></center></pre></bdo></b><th id='DiM9G'></th></span></q></dt></tr></i><div id='DiM9G'><tfoot id='DiM9G'></tfoot><dl id='DiM9G'><fieldset id='DiM9G'></fieldset></dl></div>

      1. 如何在极坐标matplotlib图上用文本(即标签)绘制水平线?(Python)

        时间:2024-08-10
        • <legend id='jKaeu'><style id='jKaeu'><dir id='jKaeu'><q id='jKaeu'></q></dir></style></legend>
            <bdo id='jKaeu'></bdo><ul id='jKaeu'></ul>
          • <tfoot id='jKaeu'></tfoot>

            <small id='jKaeu'></small><noframes id='jKaeu'>

              <tbody id='jKaeu'></tbody>

            <i id='jKaeu'><tr id='jKaeu'><dt id='jKaeu'><q id='jKaeu'><span id='jKaeu'><b id='jKaeu'><form id='jKaeu'><ins id='jKaeu'></ins><ul id='jKaeu'></ul><sub id='jKaeu'></sub></form><legend id='jKaeu'></legend><bdo id='jKaeu'><pre id='jKaeu'><center id='jKaeu'></center></pre></bdo></b><th id='jKaeu'></th></span></q></dt></tr></i><div id='jKaeu'><tfoot id='jKaeu'></tfoot><dl id='jKaeu'><fieldset id='jKaeu'></fieldset></dl></div>

                  本文介绍了如何在极坐标matplotlib图上用文本(即标签)绘制水平线?(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试标记极坐标图中的节点。有3个&q;轴&q;被拆分,我已经知道如何使用象限来选择要标记的节点。但是,我不知道如何将它们与绘图边缘对齐(即axis_maximum)。我花了几个小时试图弄清楚这件事。我最好的选择是在左边或右边填上.,但这是一个固定的数字,当点太多时就会变得乱七八糟。而且,当有很多点的时候,这种方法超出了情节的圆圈性质。I did some trigonometry计算出所有内容的长度,但这很难使用.这样的文本单位来实现。

                  如果有人能帮忙,我们将不胜感激。我展示了下面的情节,然后用红色添加了我正在尝试实现的内容。模拟图中的label对应于for循环中的name_node。理想情况下,我希望避免使用像.这样的字符,而宁愿使用实际的matplotlibLine对象,这样我就可以指定linestyleLike:-

                  总之,我想做以下工作:

                  1. 添加从我的";轴&q;延伸到绘图外边缘的水平线(根据象限的不同是向右还是向左)
                  2. 在(1)中的行尾,我要添加name_node文本。

                  编辑:

                  • 我添加了一个覆盖笛卡尔坐标轴的尝试,然后在此轴上绘制线条。没有成功。

                  import numpy as np
                  from numpy import array # I don't like this but it's for loading in the pd.DataFrame
                  import pandas as pd 
                  import matplotlib.pyplot as plt
                  df = pd.DataFrame({'node_positions_normalized': {'iris_100': 200.0, 'iris_101': 600.0, 'iris_102': 1000.0, 'iris_0': 200.0, 'iris_1': 600.0, 'iris_2': 1000.0, 'iris_50': 200.0, 'iris_51': 600.0, 'iris_52': 1000.0}, 'theta': {'iris_100': array([5.42070629, 6.09846678]), 'iris_101': array([5.42070629, 6.09846678]), 'iris_102': array([5.42070629, 6.09846678]), 'iris_0': array([1.23191608, 1.90967657]), 'iris_1': array([1.23191608, 1.90967657]), 'iris_2': array([1.23191608, 1.90967657]), 'iris_50': array([3.32631118, 4.00407168]), 'iris_51': array([3.32631118, 4.00407168]), 'iris_52': array([3.32631118, 4.00407168])}})
                  axis_maximum = df["node_positions_normalized"].max()
                  thetas = np.unique(np.stack(df["theta"].values).ravel())
                  
                  
                  def pol2cart(rho, phi):
                      x = rho * np.cos(phi)
                      y = rho * np.sin(phi)
                      return(x, y)
                  
                  def _get_quadrant_info(theta_representative):
                      # 0/360
                      if theta_representative == np.deg2rad(0):
                          quadrant = 0
                      # 90
                      if theta_representative == np.deg2rad(90):
                          quadrant = 90
                      # 180
                      if theta_representative == np.deg2rad(180):
                          quadrant = 180
                      # 270
                      if theta_representative == np.deg2rad(270):
                          quadrant = 270
                  
                      # Quadrant 1
                      if np.deg2rad(0) < theta_representative < np.deg2rad(90):
                          quadrant = 1
                      # Quadrant 2
                      if np.deg2rad(90) < theta_representative < np.deg2rad(180):
                          quadrant = 2
                      # Quadrant 3
                      if np.deg2rad(180) < theta_representative < np.deg2rad(270):
                          quadrant = 3
                      # Quadrant 4
                      if np.deg2rad(270) < theta_representative < np.deg2rad(360):
                          quadrant = 4
                      return quadrant
                      
                      
                  with plt.style.context("seaborn-white"):
                      fig = plt.figure(figsize=(8,8))
                      ax = plt.subplot(111, polar=True)
                      ax_cartesian = fig.add_axes(ax.get_position(), frameon=False, polar=False)
                      ax_cartesian.set_xlim(-axis_maximum, axis_maximum)
                      ax_cartesian.set_ylim(-axis_maximum, axis_maximum)
                  
                      # Draw axes
                      for theta in thetas:
                          ax.plot([theta,theta], [0,axis_maximum], color="black")
                          
                      # Draw nodes
                      for name_node, data in df.iterrows():
                          r = data["node_positions_normalized"]
                          for theta in data["theta"]:
                              ax.scatter(theta, r, color="teal", s=150, edgecolor="black", linewidth=1, alpha=0.618)
                          # Draw node labels
                          quadrant = _get_quadrant_info(np.mean(data["theta"]))
                   
                          # pad on the right and push label to left
                          if quadrant in {1,4}:
                              theta_anchor_padding = min(data["theta"])
                          # pad on left and push label to the right
                          if quadrant in {2,3}:
                              theta_anchor_padding = max(data["theta"])
                              
                          # Plot
                          ax.text(
                              s=name_node,
                              x=theta_anchor_padding,
                              y=r,
                              horizontalalignment="center",
                              verticalalignment="center",
                          )
                      
                      ax.set_rlim((0,axis_maximum))
                      
                      # Convert polar to cartesian and plot on cartesian overlay?
                      xf, yf = pol2cart(theta_anchor_padding, r) #fig.transFigure.inverted().transform(ax.transData.transform((theta_anchor_padding, r)))
                      ax_cartesian.plot([xf, axis_maximum], [yf, yf])
                  

                  推荐答案

                  可以使用annotate而不是text,这样可以独立于点坐标指定文本坐标和文本坐标系。我们将文本放置在图形坐标中(01,详情请参见here)。在设置r限制之后,请务必将数据坐标转换为图形坐标

                  with plt.style.context("seaborn-white"):
                      fig = plt.figure(figsize=(8,8))
                      ax = plt.subplot(111, polar=True)
                      ax.set_rlim((0,axis_maximum))
                      ann_transf = ax.transData + fig.transFigure.inverted() 
                  
                      # Draw axes
                      for theta in thetas:
                          ax.plot([theta,theta], [0,axis_maximum], color="black")
                      
                      
                      # Draw nodes
                      for name_node, data in df.iterrows():
                          r = data["node_positions_normalized"]
                          for theta in data["theta"]:
                              ax.scatter(theta, r, color="teal", s=150, edgecolor="black", linewidth=1, alpha=0.618)
                          # Draw node labels
                          quadrant = _get_quadrant_info(np.mean(data["theta"]))
                   
                          # pad on the right and push label to left
                          if quadrant in {1,4}:
                              theta_anchor_padding = min(data["theta"])
                          # pad on left and push label to the right
                          if quadrant in {2,3}:
                              theta_anchor_padding = max(data["theta"])
                              
                          # Plot
                          _,y = ann_transf.transform((theta_anchor_padding, r))
                          ax.annotate(name_node, 
                                      (theta_anchor_padding,r), 
                                      (0.91 if quadrant in {1,4} else 0.01, y),
                                      textcoords='figure fraction',
                                      arrowprops=dict(arrowstyle='-', color='r'),
                                      color='r',
                                      verticalalignment='center'
                          )
                  

                  这篇关于如何在极坐标matplotlib图上用文本(即标签)绘制水平线?(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                1. <legend id='mDfBs'><style id='mDfBs'><dir id='mDfBs'><q id='mDfBs'></q></dir></style></legend>
                  <tfoot id='mDfBs'></tfoot>
                    <bdo id='mDfBs'></bdo><ul id='mDfBs'></ul>

                  • <small id='mDfBs'></small><noframes id='mDfBs'>

                          <i id='mDfBs'><tr id='mDfBs'><dt id='mDfBs'><q id='mDfBs'><span id='mDfBs'><b id='mDfBs'><form id='mDfBs'><ins id='mDfBs'></ins><ul id='mDfBs'></ul><sub id='mDfBs'></sub></form><legend id='mDfBs'></legend><bdo id='mDfBs'><pre id='mDfBs'><center id='mDfBs'></center></pre></bdo></b><th id='mDfBs'></th></span></q></dt></tr></i><div id='mDfBs'><tfoot id='mDfBs'></tfoot><dl id='mDfBs'><fieldset id='mDfBs'></fieldset></dl></div>
                            <tbody id='mDfBs'></tbody>