python3.7 matplotlib增加坐标说明_python matplotlib:如何在极坐标图中的轴和刻度标签之间插入更多空格?...

@dabillox已经提到使用压裂kwarg来ax.set_thetagrids.

但是,正如您已经注意到的那样,您真正想要改变的是勾选标签的对齐方式,而不是刻度标签的整体径向位移.

另一方面,标签板没有效果的原因是它控制轴标签(例如plt.xlabel,plt.ylabel)和轴之间的填充,而不是刻度标签.

首先,您可以更清楚地编写示例代码.这里或多或少是我接近你正在做的事情(请注意,这仍然会与刻度标签定位有相同的问题):

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.ticker as tkr

def main():

data = [ 10.49531611, 22.49511583, 10.90891806, 18.99525417,

21.57165972, 6.687755 , 6.52137028, 15.86534639,

18.53823556, 6.32563583, 12.99365833, 11.06817056,

17.29261306, 15.31288556, 19.16236667, 10.38483333,

14.51442222, 17.01413611, 6.96102278, 15.98508611,

16.5287 , 15.26533889, 20.83520278, 17.21952056,

7.3225775 , 16.42534361, 14.38649722, 21.63573111, 16.19249444]

data = np.array(data)*60*60

plot_clock(data)

plt.show()

def plot_clock(data):

def hour_formatAM(x, p):

hour = x * 6 / np.pi

return '{:0.0f}:00'.format(hour) if x > 0 else '12:00'

def hour_formatPM(x, p):

hour = x * 6 / np.pi

return '{:0.0f}:00'.format(hour + 12) if x > 0 else '24:00'

def plot(ax, theta, counts, formatter):

colors = plt.cm.jet(theta / 12.0)

ax.bar(theta, counts, width=np.pi/6, color=colors, alpha=0.5)

ax.xaxis.set_major_formatter(tkr.FuncFormatter(formatter))

plt.rcParams['font.size'] = 8

bins = np.r_[0, 0.5:12, 12, 12.5:24, 23.99999]

data = np.array(data) / (60*60)

counts = np.histogram(data,bins)[0]

counts[13] += counts[0]

counts[-1] += counts[13]

fig, axes = plt.subplots(ncols=2, figsize=(5.5, 3), dpi=200,

subplot_kw=dict(projection='polar'))

fig.subplots_adjust(wspace=0.4)

for ax in axes:

ax.set(theta_offset=np.pi/2, theta_direction=-1,

xticks=np.arange(0, np.pi*2, np.pi/6),

yticks=np.arange(1, counts.max()))

plot(axes[0], bins[1:13] * np.pi / 6, counts[1:13], hour_formatAM)

plot(axes[1], bins[14:26] * np.pi / 6, counts[14:26], hour_formatPM)

main()

如果我们想避免错误对齐的刻度标签,我们可以根据它们的位置设置水平对齐:

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.ticker as tkr

def main():

data = [ 10.49531611, 22.49511583, 10.90891806, 18.99525417,

21.57165972, 6.687755 , 6.52137028, 15.86534639,

18.53823556, 6.32563583, 12.99365833, 11.06817056,

17.29261306, 15.31288556, 19.16236667, 10.38483333,

14.51442222, 17.01413611, 6.96102278, 15.98508611,

16.5287 , 15.26533889, 20.83520278, 17.21952056,

7.3225775 , 16.42534361, 14.38649722, 21.63573111, 16.19249444]

data = np.array(data)*60*60

axes = plot_clock(data)

for ax in axes:

realign_polar_xticks(ax)

plt.show()

def realign_polar_xticks(ax):

for x, label in zip(ax.get_xticks(), ax.get_xticklabels()):

if np.sin(x) > 0.1:

label.set_horizontalalignment('left')

if np.sin(x) < -0.1:

label.set_horizontalalignment('right')

def plot_clock(data):

def hour_formatAM(x, p):

hour = x * 6 / np.pi

return '{:0.0f}:00'.format(hour) if x > 0 else '12:00'

def hour_formatPM(x, p):

hour = x * 6 / np.pi

return '{:0.0f}:00'.format(hour + 12) if x > 0 else '24:00'

def plot(ax, theta, counts, formatter):

colors = plt.cm.jet(theta / 12.0)

ax.bar(theta, counts, width=np.pi/6, color=colors, alpha=0.5)

ax.xaxis.set_major_formatter(tkr.FuncFormatter(formatter))

plt.rcParams['font.size'] = 8

bins = np.r_[0, 0.5:12, 12, 12.5:24, 23.99999]

data = np.array(data) / (60*60)

counts = np.histogram(data,bins)[0]

counts[13] += counts[0]

counts[-1] += counts[13]

fig, axes = plt.subplots(ncols=2, figsize=(5.5, 3), dpi=200,

subplot_kw=dict(projection='polar'))

fig.subplots_adjust(wspace=0.5)

for ax in axes:

ax.set(theta_offset=np.pi/2, theta_direction=-1,

xticks=np.arange(0, np.pi*2, np.pi/6),

yticks=np.arange(1, counts.max()))

plot(axes[0], bins[1:13] * np.pi / 6, counts[1:13], hour_formatAM)

plot(axes[1], bins[14:26] * np.pi / 6, counts[14:26], hour_formatPM)

return axes

main()

最后,如果您想“正确”执行此操作,无论θ方向和偏移量如何,请执行以下操作:

def realign_polar_xticks(ax):

for theta, label in zip(ax.get_xticks(), ax.get_xticklabels()):

theta = theta * ax.get_theta_direction() + ax.get_theta_offset()

theta = np.pi/2 - theta

y, x = np.cos(theta), np.sin(theta)

if x >= 0.1:

label.set_horizontalalignment('left')

if x <= -0.1:

label.set_horizontalalignment('right')

if y >= 0.5:

label.set_verticalalignment('bottom')

if y <= -0.5:

label.set_verticalalignment('top')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值