如何通过平滑曲线使用 matplotlib plt.plot(df) 在图中表示数据? [英] how to represent data in a graph using matplotlib plt.plot(df) by smoothening the curves?
问题描述
这是我的问题,有人可以帮忙吗
第二张图片是我的问题,第一张图片是我使用以下代码能够解决的问题.如何在问题中使用matplotlib绘制图形?
在此处输入代码导入matplotlib.pyplot作为plt%matplotlib 内联将numpy导入为np将熊猫作为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对于范围(0,len(maxi))中的i]列表=[]对于范围(0,len(maxi))中的i:l = []l.append(maxi[i])l.append(mini [i])l.append(index[i])list.append(l)df = pd.DataFrame(list,columns = ['maxi','mini','index'])数据=[元组(df['maxi']),元组(df['mini'])]l = []对于范围内的 i (0,len(df.columns)-1):l.append(df.columns[i])color = ['r','b']j=0对于 y 在 l:plt.scatter(data=df,x='index', y=y, color=color[j])plt.plot(df[l[j]],color=color[j])j=j+1
如果您愿意使用图,可以使用 go.Figure()
和轻松地设置图形.go.Scatter()
,然后设置 line = dict(shape ='spline',smoothing =< factor>)
,其中< factor>
是0 和 1.3
之间的数字.从
完整代码:
#个导入将熊猫作为pd导入导入plotly.graph_objs# 数据df = pd.DataFrame({'value1':[10,40,20,5],'value2':[20,30,10,10]})#绘图设置无花果= go.Figure()# 平滑的值 1 = 1.3fig.add_trace(go.Scatter(x=df.index, y = df['value1'],line = dict(shape='spline', 平滑 = 1.3)))#平滑= 0.8的值2fig.add_trace(go.Scatter(x=df.index, y = df['value2'],line = dict(shape='spline', 平滑 = 0.8)))图.show()
this is my question,can someone help
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
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:
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()
这篇关于如何通过平滑曲线使用 matplotlib plt.plot(df) 在图中表示数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!