Home > database >  Matplotlib not shown x tick labels
Matplotlib not shown x tick labels

Time:11-23

I have a dataframe as follows (reproducible data):

import pandas as pd
import numpy as np
from datetime import datetime

np.random.seed(365)
rows = 2
start_date=datetime.strptime('2020-01-01 00:00:00', '%Y-%m-%d %H:%M:%S')
data = np.random.uniform(2.1, 6.5, size=(rows, cols))
index = pd.bdate_range(start_date, freq='H', periods=rows)

df = pd.DataFrame(data=data, index=index, columns=['Ta']) #Ta: Room temperature
                           Ta
2020-01-01 00:00:00  6.242405
2020-01-01 01:00:00  4.923052
2020-01-01 02:00:00  5.112286
2020-01-01 03:00:00  4.689673
2020-01-01 04:00:00  4.493104
2020-01-01 05:00:00  3.719512
2020-01-01 06:00:00  5.473153
2020-01-01 07:00:00  3.442055
2020-01-01 08:00:00  4.045178
2020-01-01 09:00:00  2.585951
2020-01-01 10:00:00  4.028845
2020-01-01 11:00:00  5.411510
2020-01-01 12:00:00  3.383155
2020-01-01 13:00:00  5.997180
2020-01-01 14:00:00  6.485442
2020-01-01 15:00:00  4.240901
2020-01-01 16:00:00  3.637405
2020-01-01 17:00:00  2.766216
2020-01-01 18:00:00  6.024569
2020-01-01 19:00:00  5.503587
2020-01-01 20:00:00  5.532941
2020-01-01 21:00:00  4.251602
2020-01-01 22:00:00  4.444596
2020-01-01 23:00:00  2.935362

I'm trying to plot temperature along the entire day, but I can’t see the ticks marks with the specific date. Only the first one appears and I want to see every tick.

Here's the code:

df['Ta'].plot(figsize=(20,12),legend=True,subplots=True,ylim=(0,12),
              xticks=list(df.index.values),fontsize=10,grid=True,
              rot=0, xlim=(pd.Timestamp('2020-01-01 00:00:00'),pd.Timestamp('2020-01-01 23:00:00')))

Plot

Plot of the data

I have tried everything that is on my hands, but I can't figure it out.

CodePudding user response:

use matplotlib annotation to attach labels to the chart

data="""Date,Ta
2020-01-01 00:00:00,6.242405
2020-01-01 01:00:00,4.923052
2020-01-01 02:00:00,5.112286
2020-01-01 03:00:00,4.689673
2020-01-01 04:00:00,4.493104
2020-01-01 05:00:00,3.719512
2020-01-01 06:00:00,5.473153
2020-01-01 07:00:00,3.442055
2020-01-01 08:00:00,4.045178
2020-01-01 09:00:00,2.585951
2020-01-01 10:00:00,4.028845
2020-01-01 11:00:00,5.411510
2020-01-01 12:00:00,3.383155
2020-01-01 13:00:00,5.997180
2020-01-01 14:00:00,6.485442
2020-01-01 15:00:00,4.240901
2020-01-01 16:00:00,3.637405
2020-01-01 17:00:00,2.766216
2020-01-01 18:00:00,6.024569
2020-01-01 19:00:00,5.503587
2020-01-01 20:00:00,5.532941
2020-01-01 21:00:00,4.251602
2020-01-01 22:00:00,4.444596
2020-01-01 23:00:00,2.935362
"""

def plot_df(df, x, y, title="", xlabel='Date', ylabel='Value', dpi=100):
    plt.figure(figsize=(20,12), dpi=dpi)
    plt.plot(x, y, color='tab:red')
    plt.xlim([pd.Timestamp('2020-01-01 00:00:00'),pd.Timestamp('2020-01-01 23:00:00')])        
    plt.ylim(0,12)
    items=range(0,len(df))
    for index in items:
        y2=y[index]
        x2=x[index]
        plt.annotate(xy=[x2,y2],s=str(y2))
    plt.gca().set(title=title, xlabel=xlabel, ylabel=ylabel)
    plt.show()

df = pd.read_csv(StringIO(data), sep=',', index_col=0,parse_dates=['Date'])
plot_df(df, x=df.index, y=df.Ta, title='Temparature')

CodePudding user response:

Filter your dataframe before the plot:

df.loc[df.index.normalize() == '2020-01-01'] \
  .plot(figsize=(20,12), legend=True, subplots=True,
        ylim=(0,12), fontsize=10, grid=True)

enter image description here

  • Related