Matplotlib绘图时x轴标签重叠的解决办法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Matplotlib绘图时x轴标签重叠的解决办法相关的知识,希望对你有一定的参考价值。
参考技术A 在使用 Matplotlib 画图时,我遇到了一个尴尬的情况,那就是当x轴的标签名字很长的时候,在绘制图形时,发生了x轴标签互相重叠的情况。本文主要通过一个简单的示例,探索了以上描述问题的4种解决方法。
绘图结果:
在使用上述数据进行绘图的时候,就出现了本文一开始描述的问题,我们可以从柱状图看到,除了第1个x轴标签之外,后面4个都发生了重叠。
既然x轴标签是由于横向空间不足,导致发生了重叠,那么,我们只需要将图形的横向空间拉长即可,也就是设置一个更大的画布。
绘图结果:
但是该方法存在一个很大的问题,那就是当x轴标签数量很多时,那么就无法通过这样的方法进行解决了。
方法二是方法一的逆向思路,既然可以调大画布,那么反过来,我们也可以调小x轴标签字体。
绘图结果:
只要我们将纵向柱状图改成横向柱状图,那么就不会存在这种问题。
绘图结果:
我们只需要将x轴的标签旋转一定的角度,就可以让其不再发生重叠。
绘图结果:
以上4种方法都是本人目前所能想到的,较为简单的解决办法,如果有更好的办法,也欢迎与本人进行交流。
Python,x轴标题与matplotlib中的刻度标签重叠
【中文标题】Python,x轴标题与matplotlib中的刻度标签重叠【英文标题】:Python, x-axis title is overlapping the tick labels in matplotlib 【发布时间】:2021-01-31 17:28:18 【问题描述】:我正在绘制图表,但 x 轴标签在图表中不可见。
我试图通过添加来解决它
ax.xaxis.labelpad = -10 # Adjust x-axis label position
x-label 会与股票标签重叠
如何调整以在绘图图中同时显示 x 轴标签和 x-ticker 标签?
复制图的完整代码:
#################################
### Modules imported used ###
#################################
import pandas as pd
import numpy as np
from datetime import datetime
from datetime import date
import time
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.dates as mdates
# file_path_setup = 'G:/Stocks/PowerPivotApps/Price download/'
# Performance_History = pd.read_csv(file_path_setup + 'Performance.txt', dtype=str, sep=',')
# Portfolio = Performance_History.loc[Performance_History['ExecutionType'] == 'All Portfolios']
# Portfolio = Performance_History.loc[Performance_History['ExecutionType'] == 'Selected Portfolios'] # remove "# set minimum level for performance time"
#Portfolios_Nr_of_Stocks = Portfolio['NrOfStocks']
#Portfolio_Performance_Time = Portfolio['PerformanceTime']
#Portfolio_Date = Portfolio['Date']
Portfolio_Date = ['2020-08-31','2020-09-01','2020-09-02','2020-09-02','2020-09-03','2020-09-04','2020-09-07','2020-09-08','2020-09-09','2020-09-09','2020-09-10','2020-09-11','2020-09-14','2020-09-15','2020-09-16','2020-09-17','2020-09-18','2020-09-21','2020-09-22','2020-09-22','2020-09-23','2020-09-24','2020-09-25','2020-09-28','2020-09-29','2020-09-30','2020-10-01','2020-10-02','2020-10-05','2020-10-06','2020-10-07','2020-10-08','2020-10-08','2020-10-09','2020-10-12','2020-10-13','2020-10-14','2020-10-15','2020-10-16']
Portfolio_Performance_Time =['00:11:11','00:11:07','00:11:16','00:10:42','00:10:54','00:10:46','00:10:27','00:11:23','00:11:35','00:10:23','00:10:51','00:41:22','00:11:05','00:11:15','00:10:50','00:10:41','00:19:47','00:10:43','00:10:48','00:11:12','00:11:05','00:10:45','00:11:02','00:10:57','00:11:01','00:15:17','00:14:33','00:18:49','00:14:28','00:20:45','00:14:29','00:14:45','00:17:52','00:14:37','00:14:08','00:15:05','00:14:46','00:14:39','00:14:40']
Portfolios_Nr_of_Stocks = ['621','619','617','619','622','622','622','621','622','622','622','613','622','621','621','607','621','622','621','622','620','620','622','620','620','680','679','680','681','488','681','681','680','678','678','676','678','676','676']
# Convert To integer
numberofstocks = [int(stock) for stock in Portfolios_Nr_of_Stocks]
# Convert to time
def get_sec(time_str):
"""Get Seconds from time."""
h, m, s = time_str.split(':')
return int(h) * 3600 + int(m) * 60 + int(s)
PerformanceTime = [get_sec(t) for t in Portfolio_Performance_Time]
# print(type(numberofstocks)) # print type
# convert to date series
date_portfolio = [datetime.strptime(d, '%Y-%m-%d') for d in Portfolio_Date]
# https://matplotlib.org/gallery/api/two_scales.html
# https://cmdlinetips.com/2019/10/how-to-make-a-plot-with-two-different-y-axis-in-python-with-matplotlib/
# create figure and axis objects with subplots()
fig,ax = plt.subplots(figsize=(12, 8)) # figsize -> size of the plot window
# make a plot
ax.plot(date_portfolio, PerformanceTime, color="red", marker="x")
# set x-axis label
ax.set_xlabel("Date", fontsize=14)
# set y-axis label
ax.set_ylabel("Performance Time",color="red",fontsize=14)
# set title
ax.set_title("Execution History",fontsize=20, loc="center", pad=10)
# format y-axis label to hh:mm:ss
formatter_yx1 = matplotlib.ticker.FuncFormatter(lambda s, x: time.strftime('%H:%M:%S', time.gmtime(s)))
ax.yaxis.set_major_formatter(formatter_yx1)
# rotate x-axis lables and adjust size
plt.xticks(rotation=90, ha='right')
# plt.xticks(rotation=90, ha='right', fontsize='x-small') # Small font text
# set minimum level for performance time, y-axis 1
ax.set_ylim([min(PerformanceTime)-100,25*60]) # -100 -> set minimum. 25*60 -> Set maximum
# twin object for two different y-axis on the sample plot
ax2=ax.twinx()
# make a plot with different y-axis using second axis object
ax2.plot(date_portfolio, numberofstocks,color="blue",marker="o")
# ax2.set_ylim([620, 680])
ax2.set_ylabel("Nr Of Stocks",color="blue",fontsize=14)
# set minimum level for performance time, y-axis 2
ax2.set_ylim([600, max(numberofstocks)+10]) # -100 -> set minimum. 25*60 -> Set maximum
# set date interval
ax.xaxis.set_major_locator(mdates.DayLocator(interval=7)) # max interval
ax.xaxis.set_minor_locator(mdates.DayLocator(interval=1)) # minimum interval
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) # set date format
ax.xaxis.labelpad = -10 # Adjust x-axis label position
# Plot graph
plt.show()
【问题讨论】:
【参考方案1】:你可以使用matplotlib中的“Tight Layout”函数来解决这个问题。
在绘制图表之前添加线,h_pad
将调整高度,w_pad
将调整宽度。
# Adjust x-axis margins
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=5.0)
并删除这部分:
ax.xaxis.labelpad = -10 # Adjust x-axis label position
结果:
【讨论】:
以上是关于Matplotlib绘图时x轴标签重叠的解决办法的主要内容,如果未能解决你的问题,请参考以下文章