简体   繁体   中英

Plotting daily data for a year with X-axis having month name

I have a series with daily data for a given year. eg I have data for the maximum temperature of each day for a year. Each day is represented in format 'Month_Day'. While plotting the series in matplotlib, the x-axis gets darken and nothing appears as shown in the attached figure 1 . I want to plot each day data bu x-axis should have the month name instead. How to set the x-axis with month name?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import MonthLocator, DateFormatter

# Sample data
dates = pd.date_range('2015-01-01', '2015-12-31')
values = np.random.rand(len(dates))
df = pd.DataFrame({"Date": dates,
                   "Value": values})
>>>
    Date    Value
0   2015-01-01  0.150016
1   2015-01-02  0.565111
2   2015-01-03  0.229569
3   2015-01-04  0.952355
4   2015-01-05  0.509594
fig, ax = plt.subplots()

ax.plot(df.Date, df.Value)
ax.xaxis.set_major_locator(MonthLocator())  # Tick locator
ax.xaxis.set_major_formatter(DateFormatter('%B'))  # Date format

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