本文介绍了将长xticks分成2行matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 matplotlib

我想将 x-ticks 分成 2 行而不是 1 行,因为有时它们很长,这就是为什么它们会出现另一行然后无法读取 x-ticks.

保持思想X标记不是硬编码的,并且正在发生变化.因此,并非总是相同的x-ticks.

因此,对于以下示例,如果我可以拥有石勒苏益格-荷尔斯泰因州,而不是我有

到石勒苏益格-荷斯坦

如何将x刻度的字符串放在-之后的换行符中?或者只是说出10个字母后我想换行

顺便说一句,如果我能像上面的例子一样将所有文本居中也很好

所以跟随也是可以的,但不是最好的.

到石勒苏益格-荷斯坦

PS:这是我使用的代码:

# 创建图形无花果= plt.figure()#x轴(站点)i = np.array(i)i_pos = np.arange(len(i))# y 轴(值)u = urbs_valueso = oemof_valuesplt.bar(i_pos-0.15, list(u.values()), label='urbs', align='center', alpha=0.75, width=0.2)plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))plt.bar(i_pos+0.15, list(o.values()), label='oemof', align='center', alpha=0.75, width=0.2)plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))#刻度名称plt.xticks(i_pos,list(map(('至').__ add__,list(u.keys()))))# 绘图规格plt.xlabel('行')plt.ylabel('容量[MW]')plt.title(site +''+ name)plt.grid(真)plt.legend()plt.ticklabel_format(style='sci',axis='y')#plt.show()#保存情节fig.savefig(os.path.join(result_dir, 'comp_'+name+'_'+site+'.png'), dpi=300)plt.close(图)
解决方案

您可以按照

替代

  xlabels_new = [xlabels中label的label.replace('-','-\ n')]

I have the following matplotlib

I would like to divide x-ticks into 2 lines instead of 1 because sometimes they are so long that is why they come over another and then it is impossible to read x-ticks.

KEEP IN MIND X-ticks are not hard coded and they are changing. So not always same x-ticks.

So for following example it would be good if I have instead of to Schleswig-Holstein I could have:

to Schleswig-
  Holstein

How would I put the string after - in newline for the x ticks? or simply after lets say 10 letters I wanna go to a new line

Btw it would be also good if I could center all the text like the example above

So following is also okay but not the best.

to Schleswig-
Holstein

PS: Here is the code I use:

# create figure
fig = plt.figure()

# x-Axis (sites)
i = np.array(i)
i_pos = np.arange(len(i))

# y-Axis (values)
u = urbs_values
o = oemof_values

plt.bar(i_pos-0.15, list(u.values()), label='urbs', align='center', alpha=0.75, width=0.2)
plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))
plt.bar(i_pos+0.15, list(o.values()), label='oemof', align='center', alpha=0.75, width=0.2)
plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))

# tick names
plt.xticks(i_pos, list(map((' to ').__add__, list(u.keys()))))

# plot specs
plt.xlabel('Lines')
plt.ylabel('Capacity [MW]')
plt.title(site+' '+name)
plt.grid(True)
plt.legend()
plt.ticklabel_format(style='sci', axis='y')
# plt.show()

# save plot
fig.savefig(os.path.join(result_dir, 'comp_'+name+'_'+site+'.png'), dpi=300)
plt.close(fig)
解决方案

You can use re as suggested on this answer and create a list of new labels with a new line character after every 10th character.

import re
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

xlabels = ["to Schleswig-Holstein", "to Mecklenburg-Vorpommern", r"to Lower Saxony"]

xlabels_new = [re.sub("(.{10})", "\\1\n", label, 0, re.DOTALL) for label in xlabels]

plt.plot(range(3))
plt.xticks(range(3), xlabels_new)
plt.show()

Alternative

xlabels_new = [label.replace('-', '-\n') for label in xlabels]

这篇关于将长xticks分成2行matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 17:46