簡體   English   中英

如何將標簽移近 x 軸

[英]How do I move labels closer to x axis

我正在嘗試合並這張圖表,以便我可以將它(和其他圖表)放入 powerpoint 幻燈片中,並且它仍然清晰可辨。 如果我可以將 x 軸的標簽移動到更靠近刻度的位置,那將是一個巨大的幫助,但我不知道該怎么做。

我已經添加了一些虛擬數據,但在真實文件中這些將是日期 - 不確定這是否重要,但我想我會提到它。

import matplotlib.pyplot as plt
from matplotlib import rcParams
import matplotlib.dates as dates
import math
from matplotlib.ticker import MaxNLocator
from matplotlib.legend_handler import HandlerLine2D

tva_blue = '#003A70'
lt_grey = '#BFBFBF'
green = '#00B050'
median = '#7F7F7F'
black = '#000000'

x = [10, 20, 30,40, 50, 60, 70, 80]
y = [8, 5, 3, 6, 8, 6, 2, 9]
z = [11, 10, 10, 9, 9, 9, 8, 7]

dates.ConciseDateConverter(x)

labels = x

tfont = {'font': 'arial', 'size': 6.5, 'fontweight': 'bold'}
lfont = {'font': 'arial', 'size': 6, 'fontweight': 'bold'}

#Set Chart Size
rcParams['figure.figsize'] = (2, 1) 
fig, ax = plt.subplots()
fig.set_dpi(400)

# Find Min / Max, use to set y axis dimensions
mx = math.ceil(12)
ymin = round(8)

# Generate Bar Chart with Line interse
plt.bar(x, y, color = tva_blue, width = 20, align = 'center', label = 'Retail Rate (¢/kWh)')
plt.plot(x, z, color = green, lw = 1.5, label = 'Top Quartile')

# Place Date labels onto x axis
plt.xticks(x, labels, fontsize = 5, rotation='vertical')
plt.yticks(y, fontsize = 5)


# Set y axis parameters
plt.ylim(8, (mx))
plt.ylabel("Cents / kWh", fontdict = lfont)
ax.yaxis.set_major_locator(MaxNLocator(integer=True))

#Add title to chart
plt.title('TVA Effective Retail Rate \n 12-Month Rolling Average Trends \nTVA Rate Gap to Top Quartile Utility Co. Peers'
, fontdict=(tfont), fontname = 'Arial', y = 1.32, color = 'black')

# Hide top, right spines
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)

# Center Ticks, Pad margins
ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=16))
ax.tick_params(axis='x', which='minor', tick1On=True, tick2On=False, 
               length = 3)
ax.tick_params(axis='x', which='major', tick1On=False, tick2On=False)
for label in ax.get_xticklabels(minor=True):
    label.set_horizontalalignment('center')
ax.margins(x=0.013)

# Format left and bottom axes
for axis in ['left', 'bottom']:
    ax.spines[axis].set_linewidth(.7)
    ax.spines[axis].set_color(black)

# Format Legend Line Width
def update(handle, orig):
    handle.update_from(orig)
    handle.set_linewidth(2.5)

# Format Legend Location
plt.legend(handler_map={plt.Line2D : HandlerLine2D(update_func=update)}, 
           ncol = 2,  bbox_to_anchor = (0.5, 1.40), loc = 'upper center', 
           frameon = False, fontsize = 5)

# Generate chart    
plt.show()

您可以將主 x 軸的pad設置為 0(甚至負值):

ax.tick_params(axis='x', which='major', tick1On=False, tick2On=False, pad=-3)

這會給你(小圖片因為 figsize (2, 1)):

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM