pyplot没有在图表上显示x轴:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('sitka_weather_2014.csv')
df['AKST'] = pd.to_datetime(df.AKST)
df['Dates'] = df['AKST'].dt.strftime('%b %d, %Y')
df.set_index("Dates", inplace= True)
# Plot Data
fig = plt.figure(dpi=256, figsize=(14, 7))
plt.title("Daily high and low temperature - 2014")
df['Max TemperatureF'].plot(linewidth=1, c='blue', label="Max Temperature °F")
df['Min TemperatureF'].plot(linewidth=1, c='red', label="Min Temperature °F")
plt.grid(True)
plt.rc('grid', linestyle=":", linewidth=1, color='gray')
plt.legend(loc='upper left')
plt.xlabel('', fontsize=10)
plt.ylabel("Temperature (°F)", fontsize=10)
plt.tick_params(axis='both', which='major', labelsize=10)
fig.autofmt_xdate(rotation=45)
plt.show()
X轴应该是包含日期的Pandas Dataframe (df)的索引。
发布于 2018-03-14 03:44:46
您的代码实际上是很好的。我试着用必要的sitka_weather_2014.csv
文件运行它,它起作用了。
问题是你看不到x轴,因为图形太大了,所以x轴的描述消失了。尝试缩放你的图形,例如,通过使dpi
更小:
fig = plt.figure(dpi=100, figsize=(14, 7)) #dpi=100 instead of dpi=256
或者使labelsize
更小:
plt.tick_params(axis='both', which='major', labelsize=5) #labelsize=5 instead of labelsize=10
最适合你的。但是代码是正确的,并且显示了x轴的描述。
发布于 2020-10-28 12:10:49
您已将xlabel
值设置为null
plt.xlabel('', fontsize=10)
https://stackoverflow.com/questions/49220796
复制相似问题