我已经用matplotlib绘制了一组4个箱形图,并且我使用了不同的方法来使轴的字体大小和形状相似,但是其中一个子图具有不同的形状。我不知道该如何解决。我把结果放在这里了。我rcParams
还在font-size
每个子图中都使用过,但是没有一个是解决这个问题的方法。如何使所有的轴字体大小在形状上相似?我知道定义一个可重现的问题会更好,但是我不确定在哪一部分中犯了错误,这就是为什么我在这里编写代码的原因。代码如下:
import os
import numpy as np
import matplotlib.pyplot as plt
import pylab
import matplotlib as mpl
import pandas as pd
from matplotlib import cm
from matplotlib import rcParams
fig, axs = plt.subplots(2, 2,sharex=True,sharey=True)
plt.rcParams.update({'font.size': 20})
root = r'C:\Users\Master Candidate\Desktop\New folder\Desktop\Out\NEW SCENARIO\Intersection\Beta 10\intersection'
xx=[]
percentage=[]
labels = []
gg=[]
my_list = os.listdir(root)
my_list = [file for file in sorted(my_list) if os.path.isdir(os.path.join(root, file))]
my_list= sorted(my_list)
percetanges = []
for directory in my_list:
CASES = [file for file in os.listdir(os.path.join(root, directory)) if file.startswith('config')]
if len(CASES)==0:
continue
CASES=sorted(CASES)
percentage=[]
for filename in CASES:
with open(os.path.join(root, directory,filename), "r") as file:
lines = file.readlines()
x = [float(line.split()[0]) for line in lines]
y = [float(line.split()[1]) for line in lines]
g = np.linspace(min(y),max(y),100)
h = min(y)+6
t = max(y)-6
xx=[]
for i in range(1,len(x)):
if (y[i] < h or y[i] > t):
xx.append(x[i])
percent = len(xx)/len(y)
percentage.append(percent)
labels.append(directory)
labels=sorted(labels)
percetanges.append(percentage)
for i, x in enumerate(percetanges):
axs[0, 0].boxplot(x,positions=[i],whis=0.001,widths = 0.6)
plt.xticks(np.arange(len(labels)),labels)
plt.grid()
plt.ylim((0,1))
...
the same strategy for the rest of 3 subplots
在代码的最后,我完成了保存过程。我的意思是您对每个子图重复了上面看到的工作,而我没有做任何其他事情
有两种方法可以设置多个图的x轴字体大小
plt.setp(ax.get_xticklabels(), fontsize=14)
ax.tick_params(axis='x', labelsize=14)
码:
import matplotlib.pyplot as plt
def example_plot(ax):
ax.plot([1, 2])
plt.setp(ax.get_xticklabels(), fontsize=14)
# ax.tick_params(axis='x', labelsize=16)
fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True)
for ax in axs.flat:
example_plot(ax)
fig.suptitle('sub title', fontsize=16)
fig.text(0.5, 0.04, '$Cr$', ha='center', va='center', fontsize=16)
fig.text(0.06, 0.5, '$a$', ha='center', va='center', rotation='vertical', fontsize=16)
plt.show()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句