1

I have been trying to plot a smooth graph, and here is my code


import matplotlib.pyplot as plt

#fig,axes= plt.subplots(nrows=6, ncols=1, squeeze=False)

x = df["DOY"]
y = df["By"]
z = df["Bz"]
a = df["Vsw"]
b = df["Nsw"]
c = df["magnetopause_distance"]
d = df["reconnection_rate"]

And after that, I used the following logic to plot the same

#create a figure
fig=plt.figure()

#define subplots and define their position
plt1=fig.add_subplot(611)
plt2=fig.add_subplot(612)
plt3=fig.add_subplot(613)
plt4=fig.add_subplot(614)
plt5=fig.add_subplot(615)
plt6=fig.add_subplot(616)

plt1.plot(x,y,'black',linewidth=0.5,marker=None)
plt1.set_ylabel("By")
plt1.set_title("3-6 July 2003")


plt2.plot(x,z,'black',linewidth=0.5)
plt2.set_ylabel("Bz")

plt3.plot(x,a,'black',linewidth=0.5)
plt3.set_ylabel("Vsw")

plt4.plot(x,b,'black',linewidth=0.5)
plt4.set_ylabel("Nsw")

plt5.plot(x,c,'black',linewidth=0.5)
plt5.set_ylabel("MD")

plt6.plot(x,d,'black',linewidth=0.5)
plt6.set_ylabel("MRR")


plt.subplots_adjust(hspace = 2,wspace = 2)



#saving plot in .jpg format
plt.savefig('myplot01.jpg', format='jpeg',dpi=500, bbox_inches='tight')

Finally, I am getting a plot like this: enter image description here

What I want is something like this: enter image description here

Sorry for the typos. Thanks for your time :)

Girish Hegde
  • 1,420
  • 6
  • 16
Prater
  • 107
  • 1
  • 8

1 Answers1

0

Use:

from scipy.interpolate import UnivariateSpline
import numpy as np

list_x_new = np.linspace(min(x), max(x), 1000)
list_y_smooth = UnivariateSpline(x, y, list_x_new)

plt.plot(list_x_new, list_y_smooth)
plt.show() 

This is for one of the graphs, you can substitute the values in list_y_smooth in place of y according to the values you want to plot.

Ravish Jha
  • 481
  • 3
  • 25
  • getting an error:cannot import name 'spline' from 'scipy.interpolate' (C:\Users\User\anaconda3\lib\site-packages\scipy\interpolate\__init__.py) Thanks for your time – Prater Aug 20 '20 at 08:06
  • 1
    I just saw that `spline` is now deprecated, I have edited my answer accordingly – Ravish Jha Aug 20 '20 at 08:35
  • Can you please elaborate the np.linspace() and its parameters? – Prater Aug 20 '20 at 08:57
  • I am getting some errors maybe due to data set limits. Although the code is working. – Prater Aug 20 '20 at 08:58
  • 1
    Because your data was too packed according to you, so I tried to space out the x-axis values so that the graph smoothens out. You can change the parameters according to your specification, here's the documentation for `linspace()` https://numpy.org/doc/stable/reference/generated/numpy.linspace.html – Ravish Jha Aug 20 '20 at 09:14
  • Do consider upvoting and verifying the answer if you found it helpful. – Ravish Jha Aug 20 '20 at 10:03