Os Snehith Ab
Os Snehith Ab

Reputation: 87

how to represent data in a graph using matplotlib plt.plot(df) by smoothening the curves?

enter image description here this is my question,can someone help enter image description here

the second picture is my question and the first picture is what i was able to day with the following code. how can i plot graph using matplotlib as in the question?

enter code here

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

maxi = [39, 41, 43, 47, 49, 51, 45, 38, 37, 29, 27, 25]
mini = [21, 23, 27, 28, 32, 35, 31, 28, 21, 19, 17, 18]
index=[i for i in range(0,len(maxi))]
list=[]
for i in range(0,len(maxi)):
   l=[]
   l.append(maxi[i])
   l.append(mini[i])
   l.append(index[i])
   list.append(l)

df=pd.DataFrame(list,columns=['maxi','mini','index'])
data=[tuple(df['maxi']),tuple(df['mini'])]


l=[]
for i in range(0,len(df.columns)-1):
   l.append(df.columns[i])


color=['r','b']
j=0
for y in l:
   plt.scatter(data=df,x='index', y=y, color=color[j])
   plt.plot(df[l[j]],color=color[j])
   j=j+1

Upvotes: 2

Views: 193

Answers (1)

vestland
vestland

Reputation: 61074

If you're willing to use plotly, you can easily set up a figure using go.Figure() and go.Scatter(), and then set line = dict(shape='spline', smoothing= <factor>) where <factor> is a number between 0 and 1.3. From the docs you can also see that you'll have to set shape='spline' for this to take effect:

smoothing:

Parent: data[type=scatter].line Type: number between or equal to 0 and 1.3 Default: 1 Has an effect only if shape is set to "spline" Sets the amount of smoothing. "0" corresponds to no smoothing (equivalent to a "linear" shape).

Here's a very basic example:

enter image description here

Complete code:

# imports
import pandas as pd
import plotly.graph_objs as go

# data
df = pd.DataFrame({'value1':[10,40,20,5],
                   'value2':[20,30,10,10]})

# plotly setup
fig = go.Figure()

# value 1 with smoothing = 1.3
fig.add_trace(go.Scatter(x=df.index, y = df['value1'],
                          line = dict(shape='spline', smoothing= 1.3)
                          )
               )

# value 2 with smoothing = 0.8
fig.add_trace(go.Scatter(x=df.index, y = df['value2'],
                          line = dict(shape='spline', smoothing= 0.8)
                          )
               )

fig.show()

Upvotes: 1

Related Questions