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')
python3.7 matplotlib增加坐标说明_python matplotlib:如何在极坐标图中的轴和刻度标签之间插入更多空格?...相关推荐
- python用matplotlib画人口图_Python+Matplotlib画contour图
- 全文阅读3分钟 - 在本文中,你将学习到以下内容: 理解画contour图的数据结构 contour图的参数调节 如何添加colorbar 我们先假设x, y的取值范围如下: import mat ...
- python matplotlib设置字体大小_python – Matplotlib表的字体大小
在Python中使用Matplotlib(2.7.9).我必须在子图中绘制一个表(在这种情况下,子图名称是选项卡)但我似乎无法找到更改表的字体大小的方法(http://imgur.com/0Ttvze ...
- python爬取疫情信息html.xpath p标签_python xpath 如何过滤div中的script和style标签
爬取一个页面中的div,想获取div中的文字,我是这么写的: selector.xpath('//div[@class="text-con"]').xpath('string(.) ...
- python坐标轴刻度设置_Python Matplotlib 设置x/y坐标轴刻度
刻度设置 参考文档: xticks 命令 yticks 命令 以xticks为例: matplotlib.pyplot.xticks(args, *kwargs) 获取或者设置当前刻度位置和文本的 x ...
- python做动态折线图_python matplotlib折线图样式实现过程
python matplotlib 同时画箱线图和折线图的问题 python3 matplotlib画两个折线图,X轴相同,Y轴生...你的memo和cpui里面是字符串,不是数字 将memo.app ...
- python绘制多条不同x轴曲线_Python matplotlib 绘制双Y轴曲线图的示例代码
Matplotlib简介 Matplotlib是非常强大的python画图工具 Matplotlib可以画图线图.散点图.等高线图.条形图.柱形图.3D图形.图形动画等. Matplotlib安装 p ...
- python绘制时间序列图_python matplotlib 画dataframe的时间序列图实例
python matplotlib 画dataframe的时间序列图实例 在python中经常会用到pandas来处理数据,最常用的数据类型是dataframe,但是有时候在dataframe有时间字 ...
- python matplotlib画数据分布图_Python数据可视化之matplotlib
数据可视化能让人们更直观的传递数据所要表达的信息.Python 中有两个专用于可视化的库,matplotlib 和 seaborn ,本文将介绍matplotlib. Matplotlib:基于Pyt ...
- python使用matplotlib可视化线图(line plot)、设置X轴坐标的下限和上限数值(setting the lower and upper bound of the x axis)
python使用matplotlib可视化线图(line plot).设置X轴坐标的下限和上限数值(setting the lower and upper bound of the x axis) 目 ...
最新文章
- Office365从销售说起——企业办公考虑重点
- paradox 修改字段长度_400字的作文就只能写400字?刘强东:这不是笑话
- .net mysql 备份_windows mysql 自动备份的几种方法
- 操作系统--用JavaScript实现银行家算法
- 微信朋友圈也可以发语音你们造吗?
- SQL记录-PLSQL数组
- drools规则中调用其它规则_Makefile文件中包含哪些规则?
- python---windows中的文件路径书写的错误
- vue实现播放rtmp直播视频流
- 错误: 找不到符号 符号: 类 ActivityMainBindingImpl
- 基于Scala开发的spark临界点均值法填充缺失值的job
- wxPython的基础教程
- 如何快速找出电脑里的所有视频\照片\文件
- 程序员都秃顶?Python 创始人笑了,养生还得学这门语言
- 智能家居创意DIY-智能触摸面板开关
- Java时区处理之夏令时,冬令时 - 美国的6个时区
- 【SSM】第四课 超市订单管理平台--订单管理功能
- C++技术开发的发展方向有哪些?
- B2C电商项目(第十四天、秒杀前端、秒杀商品存入缓存、秒首页实现、时间菜单、倒计时、加载商品列表、抢购按钮 )
- 计算机如何查看网络延时,电脑网络延时如何查看 网络延时查看方法【步骤教程】...