Home > Mobile >  Plotting time series data with with 30sec break point and color
Plotting time series data with with 30sec break point and color

Time:10-05

I am new in python programming. I can simply plot the input data shown in the figure with my code but how can I plot the time series data as mention in the figure. Any code and suggestions will be thankful.
enter image description here

My code is:

import matplotlib.pyplot as plt
import numpy as np
y_values = [5, 5, 1, 1, 5, 5, 1, 1, 5, 1, 1] 
x_values = np.arange(30, 331, 30)
plt.figure()
plt.plot(x_values,y_values,"-x") 
plt.show()

CodePudding user response:

Although there is a way to draw a series of rectangular shapes, we used a general method and used horizontal bar charts. We added a list for the values in the bar chart and stacked the values. Class label names and class titles are now supported as annotations. You can try various other parameters.

import matplotlib.pyplot as plt
import numpy as np

y = [5]*11
y_values = [5, 5, 1, 1, 5, 5, 1, 1, 5, 1, 1] 
x_values = np.arange(30, 331, 30)

fig, ax = plt.subplots(figsize=(12,1))
ax.barh(y=0, height=1.0, edgecolor='k', width=y[0], label='Time Interval')
for i in range(len(y)):
    if y_values[i] == 5:
        color = 'y'
    else:
        color = 'm'
    ax.barh(y=0, left=sum(y[:i]), height=1.0, width=y[i], color=color, edgecolor='k', label='Time Interval')

for s in ['top','bottom','left','right']:
    ax.spines[s].set_visible(False)

for i,(p,t) in enumerate(zip(y, y_values)):
    ax.text(y=0.6, x=2.5 p*i, s=str(t))

ax.text(-0.08, 1, 'Class', transform=ax.transAxes)
ax.set_xticks([])
ax.set_yticks([])
ax.set_ylabel('Time Interval', rotation=0, labelpad=40, loc='center')
    
plt.show()

enter image description here

CodePudding user response:

Try:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

y_values = ['class', 5, 5, 1, 1, 5, 5, 1, 1, 5, 1, 1]
x_values = np.arange(30, 331, 30)
x_values = np.concatenate((['Time'],x_values))

df = pd.DataFrame(data={'class': y_values, 'Time': x_values})
colors = {5: 'gold', 1: 'darkviolet'}
df['colors'] = df['class'].map(colors)
df['colors'].fillna('white', inplace=True)
df['Time'].iloc[1:] = ''
print(df)

fig, ax =plt.subplots(1,1)
ax.axis('tight')
ax.axis('off')
data = df.T.values
colors = [data[2].tolist()]
table = ax.table(cellText=[data[1].tolist()], colLabels=data[0].tolist(),loc="center", cellColours=colors)
table.set_fontsize(14)

for i in range(len(data[0])):
    table[0, i].visible_edges = ''

table[1, 0].visible_edges = ''
table.scale(1.5, 1.5)
plt.show()

Plot

  • Related