社区,我做了一些复制粘贴并提出了下面的代码。然而,y 轴数字的科学记数法以及方差分析测试不起作用。有什么想法吗?我认为这可能是一些冗余或冲突,但我自己找不到。我正在 jupyter 笔记本中运行下面的代码,但假设它不会影响代码的工作。
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
from scipy.stats import f_oneway
import string
import matplotlib.ticker as mticker
# Create the new dataframe
data = {
'a': [533330.45, 488550.63, 433338.27, 63335.47, 613338.78, 658029.33, 423276.31, 486775.20, 465138.94],
'b': [103336.32, 93337.35, 93336.19, 233305.77, 283336.51, 152925.39, 331697.83, 299425.14, 274197.59],
'c': [252919.78, 193330.26, 205333.11, 134333.34, 123332.30, 123336.85, 153332.33, 195452.55, 167399.20],
'd': [44133.07, 43963.73, 35705.67, 135454.11, 137261.84, 152548.42, 233336.48, 213787.00, 257127.76],
'e': [38697.59, 43337.02, 33331.53, 34333.28, 63332.51, 43338.29, 34333.43, 42793.33, 63334.84]
}
df = pd.DataFrame(data)
color_palette = {
'a': 'green',
'b': 'lightgreen',
'c': 'yellow',
'd': 'lightyellow',
'e': 'red'
}
# Create a figure with desired dimensions
plt.figure(figsize=(14, 10))
# Create the box plot using Seaborn with color code
ax = sns.boxplot(data=df, palette=color_palette)
# Add stripplot to display all data points
sns.stripplot(data=df, color="black", ax=ax, jitter=True, dodge=True, alpha=0.5)
# Add faint horizontal grid lines
ax.yaxis.grid(True, linestyle='dotted', linewidth=0.5, color='lightgray')
# Increase font size of y- and x-axis labels
ax.set_ylabel('values', fontsize=18)
ax.yaxis.set_major_formatter(mticker.ScalarFormatter(useMathText=True))
ax.yaxis.get_offset_text().set_fontsize(12) # Set font size for the offset (e.g., '1e6')
# Get the p-value from ANOVA test
_, p_value = f_oneway(df['a'], df['b'], df['c'], df['d'], df['e'])
# Add ANOVA test result above the plot
significance = "*" if p_value < 0.05 else "ns"
y_position = df.values.max() + 50000 # Adjust y position
ax.annotate(f'ANOVA p-value: {p_value:.4f} {significance}', xy=(0.5, y_position), fontsize=12, ha='center')
# Set the x-axis tick labels
ax.set_xticklabels(['a', 'b', 'c', 'd', 'e'], fontsize=20)
# plt.tight_layout()
# Save the figure
# plt.savefig('box_plot_with_anova.png', dpi=360)
plt.show()
添加科学记数法可以通过使用 Python String Format Cookbook 示例获取刻度信息并将格式调整为您想要的格式来完成。我已经调整了名为“#增加 y 轴和 x 轴标签的字体大小”的代码部分,以添加使用指数表示法制作新刻度。使用该新代码,我认为不需要
ax.yaxis.set_major_formatter(mticker.ScalarFormatter(useMathText=True))
行或其后面的行。我也将 y 轴本身的标签调整得更大。
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
from scipy.stats import f_oneway
import string
import matplotlib.ticker as mticker
# Create the new dataframe
data = {
'a': [533330.45, 488550.63, 433338.27, 63335.47, 613338.78, 658029.33, 423276.31, 486775.20, 465138.94],
'b': [103336.32, 93337.35, 93336.19, 233305.77, 283336.51, 152925.39, 331697.83, 299425.14, 274197.59],
'c': [252919.78, 193330.26, 205333.11, 134333.34, 123332.30, 123336.85, 153332.33, 195452.55, 167399.20],
'd': [44133.07, 43963.73, 35705.67, 135454.11, 137261.84, 152548.42, 233336.48, 213787.00, 257127.76],
'e': [38697.59, 43337.02, 33331.53, 34333.28, 63332.51, 43338.29, 34333.43, 42793.33, 63334.84]
}
df = pd.DataFrame(data)
color_palette = {
'a': 'green',
'b': 'lightgreen',
'c': 'yellow',
'd': 'lightyellow',
'e': 'red'
}
# Create a figure with desired dimensions
plt.figure(figsize=(14, 10))
# Create the box plot using Seaborn with color code
ax = sns.boxplot(data=df, palette=color_palette)
# Add stripplot to display all data points
sns.stripplot(data=df, color="black", ax=ax, jitter=True, dodge=True, alpha=0.5)
# Add faint horizontal grid lines
ax.yaxis.grid(True, linestyle='dotted', linewidth=0.5, color='lightgray')
# Increase font size of y- and x-axis labels
ax.set_ylabel('values', fontsize=22)
#ax.yaxis.set_major_formatter(mticker.ScalarFormatter(useMathText=True))
#ax.yaxis.get_offset_text().set_fontsize(12) # Set font size for the offset (e.g., '1e6')
grab_major_locations = ax.yaxis.get_majorticklocs()
ax.yaxis.set_major_locator(mticker.FixedLocator(grab_major_locations)) # added this based on https://stackoverflow.com/a/63755285/8508004 or else got `UserWarning: FixedFormatter should only be used together with FixedLocator`
ax.set_yticklabels([f"{x:.1E}" for x in grab_major_locations],fontsize=14)
# Get the p-value from ANOVA test
_, p_value = f_oneway(df['a'], df['b'], df['c'], df['d'], df['e'])
# Add ANOVA test result above the plot
significance = "*" if p_value < 0.05 else "ns"
y_position = df.values.max() + 50000 # Adjust y position
ax.annotate(f'ANOVA p-value: {p_value:.4f} {significance}', xy=(0.5, y_position), fontsize=12, ha='center')
# Set the x-axis tick labels
ax.set_xticklabels(['a', 'b', 'c', 'd', 'e'], fontsize=20)
# plt.tight_layout()
# Save the figure
# plt.savefig('box_plot_with_anova.png', dpi=360)
plt.show()