mport matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os
from matplotlib.ticker import FuncFormatter
# Assuming dataset is already defined
dataset2 = #redacted
# Set up the figure
plt.figure(figsize=(8, 6))
# Plot the histogram with weights for relative frequencies
weights2 = np.ones_like(dataset2['Angle']) / len(dataset2['Area'])
plt.hist(dataset2['Angle'], bins=15, weights=weights2, color='Black', linewidth=2, label='Virtual')
# Set labels and title with larger font size
plt.xlabel("In-plane ($x_1$-$x_2$) angle range (°)", fontsize=14)
plt.ylabel("Probability", fontsize=14)
# Set x-axis limit
plt.xlim(-90, 90)
# Increase the size of tick labels
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
# Convert y-axis ticks to percentages for the second plot
plt.gca().yaxis.set_major_formatter(FuncFormatter(lambda y, _: '{:.0%}'.format(y)))
# Add text box in the upper-right corner
a_11_value = 0.58
text_box_content = f'$a_{{11}} = {a_11_value:.2f}$'
plt.text(0.95, 0.95, text_box_content, transform=plt.gca().transAxes,
verticalalignment='top', horizontalalignment='right', fontsize=16,
bbox=dict(facecolor='white', alpha=0.8, edgecolor='black'))
# Specify the folder to save the plots
save_folder = r'savelink
# Save the combined plot as an SVG and PNG file
plt.savefig(os.path.join(save_folder, 'angledistribution_plots.svg'))
plt.savefig(os.path.join(save_folder, 'angledistribution_plots.png'), dpi=00)
# Show the plot
plt.show()
当我尝试将图表保存为SVG格式时,直方图上出现了不必要的白色线条,而在PNG格式中则没有这个问题。我的本意是希望SVG和PNG两种格式的文件看起来一样,即直方图的各个区间不应该有类似渐变的线条出现,但实际上却并非如此。