首页 > 解决方案 > 编辑绘图以使其平滑

问题描述

我寻求帮助来编辑我的情节。在图像中,您可以看到我在所有六个图中都有垂直线。在此处输入图像描述

但我想要一条平滑的曲线。这是我到目前为止尝试过的代码

import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
from math import *
from scipy.stats import skew
from scipy.interpolate import make_interp_spline
import scipy
import seaborn as sns
plt.rcParams["figure.figsize"] = (12,3)
df=pd.read_csv("plot_data00.txt")
df=df[df['DOY'].between(184,187)]
df["HR"] = df["HR"]/24
df["DOY"] = df["DOY"]+ df["HR"]
df.to_csv("final_plot_data.txt")
#define variables to plot

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["reconnection_rate"]
d = df["magnetopause_distance"]

#create a figure and edit size
fig=plt.figure(figsize=(20,17))

#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("DOY", "By", data=df)
plt1.set_ylabel("By",size=16)
plt1.set_title("3-6 July 2003",size=20)
plt1.get_yaxis().set_label_coords(-0.05,0.5)


plt2.plot("DOY", "Bz", data=df)
plt2.set_ylabel("Bz",size=16)
plt2.get_yaxis().set_label_coords(-0.05,0.5)

plt3.plot("DOY", "Vsw", data=df)
plt3.set_ylabel("Vsw",size=16)
plt3.get_yaxis().set_label_coords(-0.05,0.5)

plt4.plot("DOY", "Nsw", data=df)
plt4.set_ylabel("Nsw",size=16)
plt4.get_yaxis().set_label_coords(-0.05,0.5)

plt5.plot("DOY", "reconnection_rate", data=df)
plt5.set_ylabel("MRR",size=16)
plt5.get_yaxis().set_label_coords(-0.05,0.5)

plt6.plot("DOY", "magnetopause_distance", data=df)
plt6.set_ylabel("MD",size=16)
plt6.set_xlabel("Day of Year",size=16)
plt6.get_yaxis().set_label_coords(-0.05,0.5)
#plt.subplots_adjust(hspace =  ,wspace = 5)
#saving plot in .jpg format
plt.savefig('myplot03.jpg', format='jpeg',dpi=None, edgecolor='g', transparent=True, bbox_inches='tight')

我本可以添加我的数据,但似乎没有更好的选择来表示它,因为它很大(请提出建议)。感谢您的时间。

更新:

使用的样本数据为:

,Unnamed: 0,Unnamed: 0.1,index,YYYY,DOY,HR,MN,By,Bz,Vsw,Nsw,reconnection_rate,magnetopause_distance
0,225369,225369,263522,2003,184.0,0.0,2,4990000000.0,670000000.0,0.5928000000000002,1.49e-06,12573.256929898798,10.858269653698725
1,225370,225370,263523,2003,184.0,0.0,3,4080000000.0,390000000.0000001,0.5825,1.47e-06,11009.972803627901,10.946523155327649
2,225371,225371,263524,2003,184.0,0.0,4,4300000000.000002,-110000000.0,0.5675,1.4499999999999999e-06,13184.030740514894,11.067370358771116
1420,226789,226789,265056,2003,185.04340277777774,0.0017361111111111108,36,1100000000.0,4550000000.0,0.6971,4.589999999999999e-06,6.417619779966155,8.528263110991979
1421,226790,226790,265057,2003,185.04340277777774,0.0017361111111111108,37,530000000.0,6020000000.0,0.7007000000000001,4.17e-06,0.09620386453014222,8.650894534287474
1422,226791,226791,265058,2003,185.04340277777774,0.0017361111111111108,38,-3110000000.0,6230000000.000001,0.6958000000000002,4.779999999999998e-06,187.4614444320996,8.476077939976314
2765,228134,228134,266471,2003,186.04340277777774,0.0017361111111111108,11,3510000000.0000005,-2500000000.0,0.7323999999999999,1.7399999999999999e-06,2791.603594666996,9.860980027267642
2766,228135,228135,266472,2003,186.04340277777774,0.0017361111111111108,12,2000000000.0,-4340000000.000002,0.7396,1.42e-06,102.51414877271608,10.167496717459473
4048,229417,229417,267898,2003,187.0,0.0,58,5280000000.000001,1240000000.0,0.6935,1.22e-06,12837.913793204994,10.654153236043083
4049,229418,229418,267899,2003,187.0,0.0,59,3470000000.0,3690000000.0,0.6901,1.24e-06,1136.6725720724482,10.642739751911767

对不起,它看起来很笨拙,但它会帮助你理解我实际上想要做什么。

标签: pythonpandasmathplot

解决方案


使用scipy.interpolate模块:

绘制原始数据

sr = df.set_index('DOY')['Bz']
x = sr.index.values
y = sr.values
tck, u = interpolate.splprep([x, y])
u = np.linspace(0, 1, num=100, endpoint=True)
out = interpolate.splev(u, tck)
plt.plot(x, y, 'b', out[0], out[1], 'r')
plt.show()

绘制原始数据

使用相同 DOY 的平均值绘图

sr = df.set_index('DOY')['Bz'].groupby('DOY').mean()
x = sr.index.values
y = sr.values
tck, u = interpolate.splprep([x, y])
u = np.linspace(0, 1, num=100, endpoint=True)
out = interpolate.splev(u, tck)
plt.plot(x, y, 'b', out[0], out[1], 'r')
plt.show()

绘制平均数据

结合

sr = df.set_index('DOY')['Bz']
x = sr.index.values
y = sr.values

sr2 = sr.groupby(level=0).mean()
x2 = sr2.index.values
y2 = sr2.values

tck, u = interpolate.splprep([x2, y2])
u = np.linspace(0, 1, num=100, endpoint=True)
out = interpolate.splev(u, tck)
plt.plot(x, y, 'b', out[0], out[1], 'r')
plt.show()

绘制组合数据


推荐阅读