- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何获取 xtick 主要标签的位置?我从 label.get_position() 获得的值没有意义。
import numpy as np
import matplotlib.pyplot as plt
def f(t):
return np.exp(-t) * np.cos(2*np.pi*t)
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
# fig, ax = plt.figure(1)
fig, ax = plt.subplots()
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')
# plt.show()
print(fig)
print(ax.get_position())
# ------------------------------------------------
# positions of the tick labels, incorrect (0,0) returned
# ------------------------------------------------
print([text.get_position() for text in ax.get_xticklabels()])
# correct tick label values
print(ax.get_xticks())
以上代码的输出是:
Figure(640x480)
Bbox('array([[ 0.125, 0.1 ],\n [ 0.9 , 0.9 ]])')
[(0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0)] <-- incorrect positions
[ 0. 1. 2. 3. 4. 5.]
如何获取 xtick 主要标签的位置?我从 label.get_position() 获得的值没有意义。有没有我不知道的转换?最终,我希望文本框的位置以 (x,y) 图像像素为单位。
最佳答案
如果你需要像素坐标,你需要图形坐标,并转换它们。
如果您需要有关转换的更多信息,请查看有关 matplotlib 转换的教程:ref
编辑:为了完整起见,我添加了指定 dpi 的选项,这将影响您的图形尺寸
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
def f(t):
return np.exp(-t) * np.cos(2*np.pi*t)
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
# set the dpi you want in your final figure
dpi = 300
mpl.rcParams['figure.dpi'] = dpi
fig, ax = plt.subplots()
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')
# saving the figure: don't forget the dpi option!
fig.savefig('./out.png', format='png', dpi=dpi)
xtickslocs = ax.get_xticks()
ymin, _ = ax.get_ylim()
print('xticks pixel coordinates')
print(ax.transData.transform([(xtick, ymin) for xtick in xtickslocs]))
print('label bounding boxes')
print([l.get_window_extent() for l in ax.get_xticklabels()])
输出:
xticks pixel coordinates
array([[ 60. , 40. ],
[ 134.4, 40. ],
[ 208.8, 40. ],
[ 283.2, 40. ],
[ 357.6, 40. ],
[ 432. , 40. ]])
label bounding boxes
[Bbox([[56.4375, 25.5555555556], [63.5625, 35.5555555556]]),
Bbox([[130.8375, 25.5555555556], [137.9625, 35.5555555556]]),
Bbox([[205.2375, 25.5555555556], [212.3625, 35.5555555556]]),
Bbox([[279.6375, 25.5555555556], [286.7625, 35.5555555556]]),
Bbox([[354.0375, 25.5555555556], [361.1625, 35.5555555556]]),
Bbox([[428.4375, 25.5555555556], [435.5625, 35.5555555556]])]
关于Python matplotlib,获取xtick标签的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44012436/
环境:Windows 7 64 位,Matlab 2014a 目标: 绘制两个待优化参数的误差热图。 放置适当的刻度线和刻度值 在正确的地方画出网格线 问题:排列 X 和 Y 刻度位置和值。当 x 轴
我用 Excel 文件中的数据创建了一个图表。我需要保留所有 xticks 但 xticklabels 只出现在每 6 个刻度上。我试过这样做: tickStep=6; Sheet=2; filena
我正在使用 Matlab 绘制数据并尝试处理刻度/刻度标签。 我打算做什么 绘制带有动态刻度标签的图表,这样我就可以找出图表上特定点的时间戳。因此时间戳 (xTickLabel) 应该只来自数据源。
我无法使用以下代码在 xticks 之间获得更好的空间: import random import matplotlib.pyplot as plt coverages = [random.randi
在 x 轴上,我想显示数字(不旋转),并在每个数字下方显示旋转名称。我有以下内容,但想分别轮换名称“一”、“二”和“三”。 plt.xticks([1,2,3], ['1\n one', '2\n t
如何从字典中的键设置 xticks?在我的原始代码中,字典是空的,并根据数据文件进行填充,因此我不能为 xticks 设置任何静态内容。根据用户输入的内容(1-10 之间的数字),该图从该数量的最高值
我试图在对数刻度之间绘图,但存在问题; from pylab import * import matplotlib.pyplot as pyplot Ds = pow(10,5) D = np.li
我无法让我的 x 刻度标签旋转,目前它们总体 我已经尝试过(如代码所示,为了使用旋转参数,我尝试使用 plt.setp 并尝试在第一个绘图参数内旋转。如果我只有一个轴,那么它会完美绘制,它是辅助轴由于
我正在绘制两个 pandas 系列。索引是一个日期(1-1到12-31) s1.plot() s2.plot() pd.plot() 解释日期并将它们分配给轴值,如下所示: 我想将主要刻度修改为每个月
使用 Matplotlib,我有两个子图,我希望它们具有相同的自定义字符串 xticks。 这是我到目前为止尝试过的一个最小示例: import matplotlib.pyplot as plt f,
如果我绘制单个 imshow 图我可以使用 fig, ax = plt.subplots() ax.imshow(data) plt.xticks( [4, 14, 24], [5, 15, 25]
class parser(object): """docstring for parser""" def __init__(self, path): self.enco
我无法操纵所有子图上的 xticks。我正在使用的 xticks 方法(根据文档)仅更改最顶层子图的 xticks。 如何更改下部子图的 xticks? 下面是我的代码: fig = make_sub
来自 plotly doc : layout > xaxis > tickvals: Sets the values at which ticks on this axis appear. Only
我有下一个DF: A B C 1 2 'name 1' 2 3 'name 2' 3 5 'name 3' 绘制A列并将C列用作xticks的正确顺序是什么? df['A'].plot(xticks
在下图中,如何使 xTicks 适合所有 x 轴?我不明白问题出在哪里,作为新的 MATLAB 用户,我将不胜感激任何建议。 这是我绘制此图的代码部分: f=figure(); plo
我想在刻度之间放置标签,否则某些标签会相互重叠。 set(gca,'XTick',[66 98 134 215 266 330 334 388 414 443 ]); set(gca,'XTickLa
我正在尝试按对象绘制组,但 x 轴标签未对齐。有什么帮助吗?谢谢。 我的代码: gdp_by_region.plot(kind='bar', align= 'center') plt.xticks(f
这个问题已经有答案了: Changing the tick frequency on the x or y axis (14 个回答) 已关闭 4 年前。 我画了一个计数图,如下: ax, fig =
这个问题已经有答案了: matplotlib strings as labels on x axis (4 个回答) 已关闭 5 年前。 def plot_freq_error(diff,file,p
我是一名优秀的程序员,十分优秀!