绘制半极性图,将角轴设置为-90°至90°

时间:2018-11-21 09:49:02

标签: python python-3.x plotly

我试图用python绘制极坐标图,并绘制出二维图像中纤维的方向(请参见下面的代码)。 如果您能告诉我如何更改当前在0°至360°范围内的角轴以匹配我的-90°至90°范围内的数据,那就太好了。 下面是一个极坐标图的示例图像,我的数据格式和我的代码。

import plotly.graph_objs as go
from plotly import plotly
import plotly.offline as offline
import numpy as np

# data
x= np.genfromtxt(see data image) 
y= np.genfromtxt(see data image)

trace = [
    go.Scatterpolar(
        r = [y], #radial coordinates
        theta = [x], #angular coordinates
        mode = 'markers',
        marker = dict(
            color = 'peru'
        )
    )
]

layout = go.Layout(
    showlegend = True,
    polar = dict(
        domain = dict( # set chart size and position 
        x = [0, 0.8],
        y = [0.3, 0.8]),
        sector = [0, 180],   # set chart shape (half or full)
        angularaxis = dict(
            thetaunit = "degrees",
            dtick = 10),
        radialaxis = dict(
            range = [1, 8000])      
))


fig = go.Figure(data=trace, layout=layout)
offline.plot(fig)

data image
x-y数据

half polar chart for fiber orientation
关于纤维取向的半极性图

2 个答案:

答案 0 :(得分:0)

好,所以我只能通过以下更改获得输出:

  • go.Layout sector=[0, 180]更改为sector=[-90, 90]

  • go.Scatterplot r=[y]更改为r=[float(a) for a in y]

  • go.Scatterplot theta=[x]更改为theta=[float(b) for b in x]

第一个更改创建了您需要的范围(-90至90)

第二和第三次更改将np.genfromtext()的结果从字符串转换为数字(尽管如果您的数据在xy中已经是数字,则这些更改可能不是必需的)。 / p>

我的代码现在看起来像这样:

import plotly.graph_objs as go
import plotly.offline as offline
import numpy as np

# data
x = np.genfromtxt([str(i) for i in range(-90, 91)])
y = np.genfromtxt([str(i * 25 + np.random.randint(1000, 2500)) for i in range(0, 181)])

trace = [
    go.Scatterpolar(
        r=[float(a) for a in y],  # radial coordinates
        theta=[float(b) for b in x],  # angular coordinates
        thetaunit="degrees",
        mode='markers',
        marker=dict(
            color='peru'
        )
    )
]

layout = go.Layout(
    showlegend=True,
    polar=dict(
        domain=dict(  # set chart size and position
            x=[0, 0.8],
            y=[0.3, 0.8]),
        sector=[0, 1800],  # set chart shape (half or full)
        angularaxis=dict(
            thetaunit="degrees",
            dtick=10,
            rotation=90,
            direction='clockwise'),
        radialaxis=dict(
            range=[1, 8000])
    )
)

fig = go.Figure(data=trace, layout=layout)
offline.plot(fig)

这些更改足以产生以下内容:

Plotly Radial Plot

您正在寻找什么吗?

答案 1 :(得分:0)

非常感谢DatHydroGuy!我用您所做的更改完全重写了代码,它现在可以正常工作。下面是代码和输出图像。注意!再次感谢HydroGuy,谢谢您的帮助!

如果有人知道如何旋转(逆时针旋转90°)并镜像(-90左,0顶部,右90),请告诉我。我尝试了rotation=direction=命令,但是它们不起作用并且与sector=命令冲突,以将角轴范围设置为-90到90。

干杯,罗恩

import plotly.graph_objs as go
from plotly import plotly
import plotly.offline as offline
import numpy as np
import plotly


# data
x= np.genfromtxt("data.csv", delimiter=",", usecols=(0), skip_header=1, encoding = 'unicode_escape')
y= np.genfromtxt("data.csv", delimiter=",", usecols=(1), skip_header=1, encoding = 'unicode_escape')

trace = [
    go.Scatterpolar(
        r = [float(a) for a in y], #radial coordinates
        theta = [float(b) for b in x], #angular coordinates
        mode = 'lines',
        marker = dict(
            color = 'peru'
        )
    )
]

layout = go.Layout(
    showlegend = True,
    legend=dict(
        x=1),
    polar = dict(
        domain = dict( # set chart size and position 
        x = [0, 1],
        y = [0, 1]),
        sector = [-90,90],   # set chart shape (half or full)
        angularaxis = dict(
            thetaunit = "degrees",
            dtick = 10,
            #rotation = -90, #does not work
            #direction = "clockwise" # does not work
            ),
        radialaxis = dict(
            range = [1, 6500])

))


fig = go.Figure(data=trace, layout=layout)
offline.plot(fig)

plotly half polar plot