即使垂直旋转也停止 matplotlib xticks 重叠

stop matplotlib xticks overlap even with vertical rotation

我有一个图表,其中列出了用户随时间变化的余额。该图的比例甚至比这更大。有什么方法可以让文本随着 x 值的增加而变小,或者最好让图表随着 x 值的增加而变宽。

plt.rcParams.update({'figure.autolayout': True})

plt.clf()
for i in range(len(line_colors)-1):
  plt.plot([i, i+1], [balance[i], balance[i+1]], color=line_colors[i])
max = int(math.ceil((max * Decimal("1.05")) / Decimal("100"))) * 100
if min != 0:
  min = int(math.floor((min - max * Decimal("0.05")) / Decimal("100"))) * 100

plt.ylim([min, max])
llc = len(line_colors)
plt.plot([llc-1, llc], [balance[llc-1], balance[llc]], color=line_colors[llc-1], zorder=1)

plt.xticks(range(len(balances)), labels, rotation='vertical')
plt.xlabel(xlabel)

for ticklabel, tickcolor in zip(plt.gca().get_xticklabels(), label_colors):
  ticklabel.set_color(tickcolor)

plt.tight_layout()

buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
im = Image.open(buf)
return im

我做了一些尝试,发现这是在添加更多点时缩放图形的好方法(您还可以设置最小图形长度)

x_length = x_count / 6.5 + 0.8
if x_length < 8:
  x_length = 8

with mpl.rc_context({"figure.figsize": (x_length, 8), 'figure.autolayout': True}):
  plt.plot(y)

  plt.xticks(range(len(labels)), labels, rotation='vertical')
  plt.margins(x=1/((x_length-0.8)*6))
  plt.savefig('graph.png')