简体   繁体   中英

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.

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:

# 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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