【问题标题】:Making small polar plots inside a scatterplot- Python在散点图中制作小极坐标图 - Python
【发布时间】:2020-06-18 13:16:34
【问题描述】:

我已编辑以使我的问题更具体。我有一个包含 4 个变量的数据集:“x”、“y”、“长度”和“方向”。变量“方向”是极坐标。我被要求制作一个散点图,然后将每个点更改为反映其长度和方向的小极坐标图。我尝试使用 quiver 制作矢量,但结果并不令人满意。我附上了我的散点图代码、示例数据帧和显示我想要的输出的 png。

bc = pd.DataFrame({'x':[2,4,6], 'y':[1,3,5], 'Length':[10,25,23], 'Direction':[-86,-85,-80]})   
plt.figure(figsize=(14, 7))
ax = sns.scatterplot(x="x", y="y", data=bc)
plt.title('Scatter Plot of x and y')

【问题讨论】:

  • 您是否已经查看过quiver
  • @JohanC。我做到了。我已经编辑了这个问题,所以读者知道我到底想要什么

标签: python data-visualization data-science


【解决方案1】:

这是一种可能的方法。使用给定的 x 和 y 创建一个三角形,并考虑角度和长度。如果默认方向(指向右侧的零角度)不是所需的方向,则可以将减号添加到正弦和/或余弦。此外,正弦和余弦的作用可以互换,以对角镜像。

为了防止角度看起来变形,可以使用set_aspect('equal')

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

bc = pd.DataFrame({'x': [2, 4, 6], 'y': [1, 3, 5], 'Length': [10, 25, 23], 'Direction': [-86, -85, -80]})
plt.figure(figsize=(14, 7))

delta = 15  # half of the aperture angle (in degrees)
length_factor = 1 / bc['Length'].max()  # this makes the longest 1 unit long
for x, y, length, dir in zip(bc['x'], bc['y'], bc['Length'], bc['Direction']):
    for is_backgr in (True, False):
        if is_backgr:
            arc_angles = np.linspace(dir + delta, dir + 360 - delta, 32)
        else:
            arc_angles = np.linspace(dir - delta, dir + delta, 10)
        c_arr = np.cos(np.radians(arc_angles))
        s_arr = np.sin(np.radians(arc_angles))
        r = length * length_factor
        x_arr = x + np.pad(c_arr * r, (1, 1))
        y_arr = y + np.pad(s_arr * r, (1, 1))
        plt.fill(x_arr, y_arr, c='grey' if is_backgr else 'crimson', alpha=0.2)
        plt.plot(x_arr, y_arr, c='black' if is_backgr else 'crimson', lw=0.5 if is_backgr else 2)
ax = sns.scatterplot(x="x", y="y", data=bc, s=100)
ax.set_title('Scatter Plot of x and y')
ax.set_aspect('equal')
plt.show()

【讨论】:

  • 有没有什么办法可以围绕每个点画一个圆圈,让它看起来像极坐标图?
  • 更新的答案是您要问的吗?如果没有,您究竟需要在哪里使用这些圆圈?
  • 我被要求制作一个以每个点为中心的极坐标图。你所做的很棒,我们可以制作一个半径与你制作的圆锥完全一样的黑色圆圈。红色部分将像上面一样确定长度和方向。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多