程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

如何绘制彼此相邻的两个半极坐标图

发布于2023-05-20 12:42     阅读(1188)     评论(0)     点赞(30)     收藏(2)


I am attempting to plot two half polar charts beside each other using matplotlib, pandas and numpy. The plots are calles c30 and c31. c31 plots well on its own but c30 does not, it appears as a line as you can see in the picture. Also they sit on top of each other instead of side by side. Can anyone advise on how to solve this problem?

What I currently achieve: 我目前取得的成就

import numpy as np
from scipy import interpolate
import pandas as pd
import matplotlib.pyplot as plt
c31df = pd.DataFrame({'measure':[0,7.52,7.91,8.57,8.9,8.94,9.36,9.21,7.97,6],      'angle':[0,52,60,75,90,110,120,135,150,180]})
c30df = pd.DataFrame({'measure':[0,5.1,7.26,7.58,8.1,8.46,8.51,8.9,8.94,7.87],  'angle':[0,52,60,75,90,110,120,135,150,180]})
c31x=c31df.measure[:-1] * np.cos(c31df.angle[:-1]/180*np.pi)
c31y=c31df.measure[:-1] * np.sin(c31df.angle[:-1]/180*np.pi)
c30x=c30df.measure[:-1] * np.sin(c30df.angle[:-1]/180*np.pi)
c30y=c30df.measure[:-1] * np.sin(c30df.angle[:-1]/180*np.pi)
c31x = np.r_[c31x, c31x[0]]
c31y = np.r_[c31y, c31y[0]]
c30x = np.r_[c30x, c30x[0]]
c30y = np.r_[c30y, c30y[0]]
# noinspection PyTupleAssignmentBalance
c31tck, c31u = interpolate.splprep([c31x, c31y], s=0, per=True)
c31xi, c31yi = interpolate.splev(np.linspace(0,1,1000), c31tck)
# noinspection PyTupleAssignmentBalance
c30tck, c30u = interpolate.splprep([c30x, c30y], s=0, per=True)
c30xi, c30yi = interpolate.splev(np.linspace(0,1,1000), c30tck)
def cart2pol(x,y):
    rho = np.sqrt(x**2+y**2)
    phi = np.arctan2(y,x)
    return(rho, phi)
fig = plt.figure(figsize=(12,8))
c31ax = fig.add_subplot(projection='polar')
c31ax.set_theta_zero_location('N')
c31ax.set_theta_direction(1)
c31ax.set_thetamax(180)
c31ax.plot(cart2pol(c31xi, c31yi)[1], cart2pol(c31xi, c31yi)[0], linewidth=1,      linestyle='solid', color='red', label='Chart')
c30ax = fig.add_subplot(projection='polar')
c30ax.set_theta_zero_location('N')
c30ax.set_theta_direction(-1)
c30ax.set_thetamax(180)
c30ax.plot(cart2pol(c30xi, c30yi)[1], cart2pol(c30xi, c30yi)[0], linewidth=1, linestyle='solid', color='green', label='Chart')
plt.tight_layout()
plt.show()

I have tried moving theta position around, and it changed the graph but still sat in the wrong position.


解决方案


更改此行:

c30x=c30df.measure[:-1] * np.sin(c30df.angle[:-1]/180*np.pi)

对此:

c30x=c30df.measure[:-1] * np.cos(c30df.angle[:-1]/180*np.pi)

当您使用 时add_subplot(),您需要提供预期的布局以及您的子图将适合该布局的位置。其中一种方法是将其称为add_subplot(nrows, nlins, pos).

在您的情况下,您需要一行和两列,因此:

c31ax = fig.add_subplot(1, 2, 1, projection='polar')
c30ax = fig.add_subplot(1, 2, 2, projection='polar')


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/1978745/23e1a2a51586a6e28863/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

30 0
收藏该文
已收藏

评论内容:(最多支持255个字符)