Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按像素值移动matplotlib Xticklabel_Python_Python 3.x_Matplotlib - Fatal编程技术网

Python 按像素值移动matplotlib Xticklabel

Python 按像素值移动matplotlib Xticklabel,python,python-3.x,matplotlib,Python,Python 3.x,Matplotlib,如何将记号标签移动几个像素 在我的例子中,我想将X轴上的This is class#N标签向右移动几个像素 我是horizontallign/havaluesright,center和left;但我想“改善”右侧对齐的外观 以下面的示例为例,该示例将生成如下所示的绘图: import pandas as pd import numpy as np categories = ['This is class #{}'.format(n) for n in range(10)] data = {

如何将记号标签移动几个像素

在我的例子中,我想将X轴上的This is class#N标签向右移动几个像素

我是
horizontallign
/
ha
values
right
center
left
;但我想“改善”右侧对齐的外观

以下面的示例为例,该示例将生成如下所示的绘图:

import pandas as pd
import numpy as np

categories = ['This is class #{}'.format(n) for n in range(10)]
data = {
    'Value': [categories[np.random.randint(10)] for _ in range(100)], 
    'id': [1000+i for i in range(100)]
}

df = pd.DataFrame(data)

ax = df.Value.value_counts().sort_index().plot(kind='bar', rot=45)
plt.xticks(ha='right')
结果:

我认为,主观上,如果标签被翻译到右边,这样勾号就被放置在“#”上,那么情节看起来会更好。换句话说,
右侧
中间
对齐选项之间的“中间地带”

旁注

我使用的是熊猫,但我认为这与问题无关,因为它使用matplotlib进行绘图

使用
plt.xticks()
方法是为了简单起见,我也可以使用
ax.setxticklabels()
,但我不需要重写标签文本,而且如果不使用
ax.setxticklabels(标签,**更多选项)
将现有标签复制到中,就没有设置水平对齐的快捷方式,as
ha
不是有效的ax.xaxis.set_tick_params()-方法


我知道pandas的
Series.hist()
-方法,但我认为
Series.value\u counts().plot(kind='bar')
在我有几个类别并且希望条数与类别数相同时看起来更漂亮。

为了将标签移动几个像素,可以在其转换链中添加一个平移。例如,要向右移动20个像素,请使用

import matplotlib.transforms as mtrans
# ...
trans = mtrans.Affine2D().translate(20, 0)
for t in ax.get_xticklabels():
    t.set_transform(t.get_transform()+trans)
当然,如果为了使#-符号位于记号下方而需要移动的像素数不是先验的清晰,那么需要通过试错来确定。或者你可能有一个不同的提示,你想在什么其他单位转移多少

这是一个完整的例子

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib.transforms as mtrans

categories = ['This is class #{}'.format(n) for n in range(10)]
data = {
    'Value': [categories[np.random.randint(10)] for _ in range(100)], 
    'id': [1000+i for i in range(100)]
}

df = pd.DataFrame(data)

ax = df.Value.value_counts().sort_index().plot(kind='bar', rot=45)
plt.xticks(ha='right')

trans = mtrans.Affine2D().translate(20, 0)
for t in ax.get_xticklabels():
    t.set_transform(t.get_transform()+trans)

plt.tight_layout()
plt.show()
生产


为了将标签移动几个像素,您可以向标签的变换链中添加平移。例如,要向右移动20个像素,请使用

import matplotlib.transforms as mtrans
# ...
trans = mtrans.Affine2D().translate(20, 0)
for t in ax.get_xticklabels():
    t.set_transform(t.get_transform()+trans)
当然,如果为了使#-符号位于记号下方而需要移动的像素数不是先验的清晰,那么需要通过试错来确定。或者你可能有一个不同的提示,你想在什么其他单位转移多少

这是一个完整的例子

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib.transforms as mtrans

categories = ['This is class #{}'.format(n) for n in range(10)]
data = {
    'Value': [categories[np.random.randint(10)] for _ in range(100)], 
    'id': [1000+i for i in range(100)]
}

df = pd.DataFrame(data)

ax = df.Value.value_counts().sort_index().plot(kind='bar', rot=45)
plt.xticks(ha='right')

trans = mtrans.Affine2D().translate(20, 0)
for t in ax.get_xticklabels():
    t.set_transform(t.get_transform()+trans)

plt.tight_layout()
plt.show()
生产


如前所述,此操作非常有效。完整的示例和图像回答得很好。出于某种原因,我在线获得了预期的结果,但如果我使用“savefig”,则保存的图像没有预期的翻译。这完全符合描述。答案很好,有完整的示例和图像。出于某种原因,我在线获得了预期的结果,但是如果我使用“savefig”,保存的图像没有预期的翻译。