我正在使用保存在要绘制箱线图的文件中的一些数据。
每个文件(第 3 行)中有 27 个值,每个文件我希望成为沿 x 轴 5 ' 的单独箱线图
然后,我还希望将每个文件作为单个箱线图进行概述,因为我有 10 个“对象”(使用所有 5 个文件 27 个值(135 个值)作为单个箱线图)
我已经开始在代码上,但目前我只给出了一个箱线图,因为我认为它没有正确保存数据以便能够绘制多个箱线图,到目前为止我还没有开始组合整个图形的数据。|| |任何帮助将不胜感激。
` import numpy as np 从 numpy 导入 genfromtxt 将 matplotlib.pyplot 导入为 plt import pandas as pd
`
dataset = pd.read_excel("ObjectData.xlsx") #Where the names of the Objects are kept
data = []
#Iterations for objects and runs
for object in range(0, 10, 1):
for run in range(0, 5, 1):
#Access data from files
recordname1 = str(dataset.iloc[object, 1]) + " run:" + str(run) #Object and run name
path1 = recordname1 + "clonemin.csv"
my_plot_data = genfromtxt(path1, delimiter=',') #Get individual file (eg 1of10,1of5)
data = [data, np.append(my_plot_data,([3, :]))] #Get individual line from file R=3, C=all
#Save individual boxplots for plotting
fig = plt.figure(figsize=(10,7))
ax = fig.add_axes([0,0,1,1])
bp = ax.boxplot(data)
plt.show()
#Save object boxplots (1x5) iteratedx10
path2 = str(dataset.iloc[object, 1]) + "Boxplot.png"
plt.savefig(path2)
plt.clf()
path3 = "Final_Boxplot.png" #Save overall boxplots (1x10)
plt.savefig(path3)
plt.clf()
结果应该是:对于每个对象 (10),应该有一个由 5 个箱线图组成的图形,以及一个沿 x 轴显示 10 个对象箱线图的整体图形。总共 11 张图片。
Result should be: For each object (10) there should be a graphic made up of 5 boxplots, and one overall graphic showing the 10 objects boxplots along the x-axis. So 11 Images overall.
import numpy as np
from numpy import genfromtxt
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_excel("ObjectData.xlsx")
# Create a figure to store all object boxplots
fig_all_objects = plt.figure(figsize=(10, 7))
for object in range(0, 10, 1):
data_object = [] # Store data for all runs of current object
for run in range(0, 5, 1):
recordname1 = str(dataset.iloc[object, 1]) + " run:" + str(run)
path1 = recordname1 + "clonemin.csv"
my_plot_data = genfromtxt(path1, delimiter=',')
data_object.append(my_plot_data)
# Create a figure for each object
fig_object = plt.figure(figsize=(10, 7))
ax_object = fig_object.add_axes([0, 0, 1, 1])
# Boxplot for all runs of current object
bp = ax_object.boxplot(data_object)
ax_object.set_xticklabels([f"Run {i+1}" for i in range(5)])
ax_object.set_title(f"Object {dataset.iloc[object, 1]}")
plt.savefig(f"{dataset.iloc[object, 1]}_Boxplot.png")
plt.close(fig_object)
# Add data to the overall figure
ax_all_objects = fig_all_objects.add_axes([0, 0, 1, 1])
bp_all = ax_all_objects.boxplot(data_object, positions=[object * 5 + i for i in range(5)])
# Customize and save the overall boxplot
ax_all_objects.set_xticks([i * 5 + 2 for i in range(10)])
ax_all_objects.set_xticklabels([dataset.iloc[i, 1] for i in range(10)])
plt.savefig("Final_Boxplot.png")
plt.close(fig_all_objects)
Here's how this code works:
- Data Loading & Initialization:
- Reads object names from "ObjectData.xlsx."
-
Initializes lists to store data for plotting.
-
Iterating Through Objects and Runs:
- Loops through each object and its corresponding runs.
- Constructs file names for each run's data.
-
Reads data from each file using
genfromtxt
. -
Creating and Saving Individual Object Boxplots:
-
For each object, creates a new figure (
plt.figure
). -
Appends the data for all 5 runs of the object to
data_object
. -
Generates a boxplot (
ax.boxplot
) using the collected data. - Sets labels for the x-axis ticks (Run 1 to Run 5).
- Sets the title of the plot with the object's name.
- Saves the figure as a PNG file named "{Object Name}_Boxplot.png".
-
Clears the figure to prepare for the next object's plot.
-
Creating and Saving the Overall Boxplot:
-
Creates a new figure (
plt.figure
) for the overall boxplot. - Iterates through the data of each object and its runs.
-
For each object, adds its data to the overall boxplot (
ax.boxplot
) at specific positions along the x-axis to avoid overlap. - Sets custom x-axis ticks and labels to represent each object.
-
Saves the final overall boxplot as "Final_Boxplot.png".
-
Output:
-
The code generates 11 images in total:
- 10 images named "{Object Name}_Boxplot.png" each showing boxplots for the 5 runs of a specific object.
- 1 image named "Final_Boxplot.png" showing an overall boxplot combining the data of all 10 objects.