简体   繁体   English

如何使用 Dataframe 和 Python 中的 mathplotlib 设置轴标题和主标题?

[英]How to set axis titles and main title using Dataframe and mathplotlib in Python?

I am trying to set axis labels and a graph title using Dataframe and mathplotlib.我正在尝试使用 Dataframe 和 mathplotlib 设置轴标签和图表标题。

Below is my code:下面是我的代码:

# Plot line chart showing average, minimum, and maximum temperature
title = "Daily minimum, average, and maximum temperatures - 2022 San Diego, CA"
fig, ax = plt.subplots()
ax.set_title(title, fontsize=20)
ax.set_xlabel('Dates', fontsize=16)
fig.autofmt_xdate()
ax.set_ylabel("Temperature (C)", fontsize=16)
data.plot(y=['tavg', 'tmin', 'tmax'])
plt.show()

I was expecting all to be on one graph.我期待所有的都在一张图表上。

However, when I run the code, it makes and shows two separate graphs.但是,当我运行代码时,它会制作并显示两个单独的图表。 One graph only showing the data (the average, minimum, and maximum temperatures) and axis numbers.一张图表仅显示数据(平均、最低和最高温度)和轴号。 Another graph showing only the axis labels and graph title.另一个仅显示轴标签和图表标题的图表。 I am trying to put all on one graph.我想把所有的都放在一张图上。

When plotting with the pandas utility you need to specify the matplotlib axis object you want to place the plot in if you don't want a new figure ie:使用 pandas 实用程序绘图时,您需要指定 matplotlib 轴 object 如果您不想要新图形,则要放置 plot 即:

# Plot line chart showing average, minimum, and maximum temperature
title = "Daily minimum, average, and maximum temperatures - 2022 San Diego, CA"
fig, ax = plt.subplots()
ax.set_title(title, fontsize=20)
ax.set_xlabel('Dates', fontsize=16)
fig.autofmt_xdate()
ax.set_ylabel("Temperature (C)", fontsize=16)
data.plot(y=['tavg', 'tmin', 'tmax'], ax=ax) #set the ax argument here 
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM