我创建了一个极地图,本质上是风玫瑰。它使用方向度作为theta值,使用风速作为高度。 Power BI中使用了这种可视化,所以我有一些限制。在Power BI页面中,页面上大约有六种风玫瑰,它们都很小,因此,当y轴的标签在其中时,看起来就很拥挤。我希望能够将笛卡尔坐标轴添加到上方的极坐标图中以及要在圆外面标记它们的侧面。
基本上是这样的 graph
当我尝试使用无花果添加它时,它永远不会显示出来,而且我不确定自己做错了什么。
这是我关于风玫瑰的代码(那里有一些Power BI的东西)。这确实是我第一次使用matplotlib,因此,如果您发现其他非最佳操作,我很想听听。
# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:
# dataset = pandas.DataFrame(WindSpeed, WindDir)
# dataset = dataset.drop_duplicates()
import numpy as np
import matplotlib.pyplot as plt
# Data
theta = dataset.WindDir
heights = dataset.WindSpeed
plt.rcParams.update({
"savefig.facecolor": (0.0, 0.0, 0, 0), # black with alpha = 0%
})
# Get an axes handle/object
ax1 = plt.subplot(111, polar=True)
# Plot
bars = ax1.bar(theta, heights,
color='#9DD9D2',
width=0.5,
bottom=0.0,
edgecolor='#666666',
alpha=0.5)
## Main tweaks
# Radius limits
#ax1.set_ylim(0, 1.5)
# Radius tick position in degrees
#ax1.set_yticklabels([])
# Angle ticks
ax1.set_xticks(np.linspace(0, 2.0*np.pi, 17)[:-1])
# Set N to the top of the graph
ax1.set_theta_zero_location("N")
# Set Clockwise
ax1.set_theta_direction(-1)
# Set cardinal directions instead of degrees
ax1.set_xticklabels(['N', 'NNW', 'NW', 'WNW', 'W', 'WSW', 'SW', 'SSW', 'S', 'SSE', 'SE', 'ESE','E', 'ENE', 'NE', 'NNE'])
# Set Axis Text
ax1.tick_params(colors='#666666', labelsize=11, pad=10)
# Additional Tweaks
plt.grid(True)
ax1.set_axisbelow(True)
plt.tight_layout()
# Generate Graph
plt.show()
答案 0 :(得分:0)
感谢JohanC为我提供了一个我自己做出的很好解释的链接。仍在进行一些调整,但这通过极坐标图和笛卡尔线偏移量获得了我想要的基础。
这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
# Data
theta = dataset.WindDir
heights = dataset.WindSpeed
CIRCLE_RES = 36 # resolution of circle inside
def offset_radial_axis(ax):
x_circle = np.linspace(0, 2*np.pi, CIRCLE_RES)
y_circle = np.zeros_like(x_circle)
ax.fill(x_circle, y_circle, fc='white', ec='black', zorder=2) # circle
# or set the ticks manually (simple)
# or define a custom TickLocator (very flexible)
# or leave out this line if the ticks are fully behind the circle
X_OFFSET = .05 # to control how far the scale is from the plot (axes coordinates)
def add_scale(ax):
# add extra axes for the scale
rect = ax.get_position()
rect = (rect.xmin-X_OFFSET, rect.ymin+rect.height/2, # x, y
rect.width, rect.height/2) # width, height
scale_ax = ax.figure.add_axes(rect)
# hide most elements of the new axes
for loc in ['right', 'top', 'bottom']:
scale_ax.spines[loc].set_visible(False)
scale_ax.tick_params(bottom=False, labelbottom=False)
scale_ax.patch.set_visible(False) # hide white background
# adjust the scale
scale_ax.spines['left'].set_bounds(*ax.get_ylim())
scale_ax.set_yticks(ax.get_yticks())
scale_ax.set_ylim(ax.get_rorigin(), ax.get_rmax())
def add_scale2(ax):
# add extra axes for the scale
rect = ax.get_position()
rect = (rect.xmin-X_OFFSET, rect.ymin, # x, y
rect.width, rect.height/2) # width, height
scale_ax = ax.figure.add_axes(rect)
# hide most elements of the new axes
for loc in ['right', 'top', 'bottom']:
scale_ax.spines[loc].set_visible(False)
scale_ax.tick_params(bottom=False, labelbottom=False)
scale_ax.patch.set_visible(False) # hide white background
# adjust the scale
scale_ax.spines['left'].set_bounds(*ax.get_ylim())
scale_ax.set_yticks(ax.get_yticks())
scale_ax.set_ylim(ax.get_rmax(), ax.get_rorigin())
# Plot
ax = plt.subplot(111, projection='polar')
ax.bar(theta, heights,
color='#00828C',
width=0.5,
bottom=0.0,
edgecolor='#666666',
alpha=0.5)
# Radius tick position in degrees
ax.set_yticklabels([])
# Angle ticks
ax.set_xticks(np.linspace(0, 2.0*np.pi, 17)[:-1])
# Set N to the top of the graph
ax.set_theta_zero_location("N")
# Set Clockwise
ax.set_theta_direction(-1)
# Set cardinal directions instead of degrees
ax.set_xticklabels(['N', 'NNW', 'NW', 'WNW', 'W', 'WSW', 'SW', 'SSW', 'S', 'SSE', 'SE', 'ESE','E', 'ENE', 'NE', 'NNE'])
# Set Axis Text
ax.tick_params(colors='#666666', labelsize=11, pad=10)
# Additional Tweaks
ax.grid(True)
add_scale(ax)
add_scale2(ax)
plt.show()
结果图像: