保存绘图并创建可复用函数
在这最后一步,我们将创建一个可复用的函数来生成带有货币格式的绘图,并将可视化结果保存到文件中。这种方法能让你在未来轻松地将相同的格式应用到不同的金融数据集上。
在你的 Notebook 的新单元格中,添加并运行以下代码:
def create_currency_plot(x_data, y_data, title='Financial Data',
xlabel='X-Axis', ylabel='Amount ($)',
filename=None, show_stats=True):
"""
Create a plot with currency formatting on the y-axis.
Parameters:
-----------
x_data : array-like
Data for the x-axis
y_data : array-like
Data for the y-axis (currency values)
title : str
Title of the plot
xlabel : str
Label for the x-axis
ylabel : str
Label for the y-axis
filename : str, optional
If provided, save the plot to this filename
show_stats : bool
Whether to show statistics (average, min, max)
Returns:
--------
fig, ax : tuple
The figure and axes objects
"""
## Import the necessary module for formatting
import matplotlib.ticker as ticker
## Create a figure and axes
fig, ax = plt.subplots(figsize=(12, 7))
## Plot the data
ax.plot(x_data, y_data, marker='o', linestyle='-', color='blue',
linewidth=2, markersize=6, label='Data')
if show_stats:
## Calculate statistics
avg_value = np.mean(y_data)
max_value = np.max(y_data)
min_value = np.min(y_data)
max_x = x_data[np.argmax(y_data)]
min_x = x_data[np.argmin(y_data)]
## Add a horizontal line for average value
ax.axhline(y=avg_value, color='r', linestyle='--', alpha=0.7,
label=f'Average: ${avg_value:.2f}')
## Add annotations for max and min values
ax.annotate(f'Max: ${max_value:.2f}', xy=(max_x, max_value),
xytext=(max_x+1, max_value+200),
arrowprops=dict(facecolor='black', shrink=0.05, width=1.5))
ax.annotate(f'Min: ${min_value:.2f}', xy=(min_x, min_value),
xytext=(min_x+1, min_value-200),
arrowprops=dict(facecolor='black', shrink=0.05, width=1.5))
## Format y-axis with dollar signs
formatter = ticker.StrMethodFormatter('${x:,.2f}')
ax.yaxis.set_major_formatter(formatter)
## Customize tick parameters
ax.tick_params(axis='both', which='major', labelsize=10)
## Add labels and title
ax.set_xlabel(xlabel, fontsize=12)
ax.set_ylabel(ylabel, fontsize=12)
ax.set_title(title, fontsize=14, fontweight='bold')
## Add grid for better readability
ax.grid(True, linestyle='--', alpha=0.7)
## Add legend
if show_stats:
ax.legend(loc='best', fontsize=10)
## Adjust layout
plt.tight_layout()
## Save the plot if filename is provided
if filename:
plt.savefig(filename, dpi=300, bbox_inches='tight')
print(f"Plot saved as '{filename}'")
return fig, ax
## Use our function to create and save a plot
fig, ax = create_currency_plot(
days,
daily_revenue,
title='Monthly Revenue Report',
xlabel='Day of Month',
ylabel='Daily Revenue ($)',
filename='revenue_plot.png'
)
## Display the plot
plt.show()
print("Function created and plot saved successfully!")
运行此代码后,你将看到:
- 一个与我们上一步创建的绘图类似的图表,但这次是使用我们自定义的函数生成的
- 一条确认消息,表明绘图已保存到名为
revenue_plot.png
的文件中
我们创建的函数:
- 接受 x 轴和 y 轴的数据
- 允许自定义标签和标题
- 有一个选项可以将绘图保存到文件中
- 可以显示或隐藏诸如平均值、最小值和最大值等统计信息
- 返回图形和坐标轴对象,以便在需要时进行进一步的自定义
这个可复用的函数让你在未来能够轻松创建格式一致的金融绘图。你只需使用不同的数据集调用这个函数,它就会自动处理所有的货币格式设置和统计注释。
为了验证我们的绘图是否正确保存,让我们检查文件是否存在:
import os
if os.path.exists('revenue_plot.png'):
print("Plot file exists! Size:", os.path.getsize('revenue_plot.png'), "bytes")
else:
print("Plot file was not saved correctly.")
你应该会看到一条消息,确认文件存在并显示其大小。
恭喜你!你已经成功学会了如何使用美元符号格式化绘图,并使用 Matplotlib 创建专业的金融可视化图表。