Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0bc86ea

Browse files
committed
Added Polynomial Fit, and linear fit examples for python
1 parent b6e2210 commit 0bc86ea

File tree

5 files changed

+181
-0
lines changed

5 files changed

+181
-0
lines changed

_includes/documentation_eg.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
{% assign financial_analysis = true %}
1515
{% elsif page.display_as == "multiple_axes" %}
1616
{% assign multiple_axes = true %}
17+
{% elsif page.display_as == "fitting_tools" %}
18+
{% assign fitting_tools = true %}
1719
{% elsif page.display_as == "scientific" %}
1820
{% assign scientific = true %}
1921
{% elsif page.display_as == "statistical" %}
@@ -217,6 +219,27 @@ <h5 class="centered"> {{page.name}} </h5>
217219
</div>
218220
{% endif %}
219221

222+
{% if fitting_tools %}
223+
<hr>
224+
<h4 class="example-section-title" id="fitting-tools"><a class="no_underline plot-blue" href="#fitting-tools">Fitting Tools</a></h4>
225+
<div class="row">
226+
{% for page in languagelistimg %}
227+
{% if page.display_as == "fitting_tools" %}
228+
<a href="/{{page.permalink}}">
229+
<div class="alltutcol">
230+
<img class="thumbnail" alt="{{page.name}}" src="{{site.imgurl}}{{page.thumbnail}}" >
231+
<div class="titlecenter">
232+
<h5 class="centered"> {{page.name}} </h5>
233+
</div>
234+
</div>
235+
</a>
236+
<div class="{% cycle 'group2-clear': '', '', 'three-column-clear' %}"></div>
237+
<div class="{% cycle 'group 2': '', '', 'three-column-clear', '', 'five-column-clear' %}"></div>
238+
{% endif %}
239+
{% endfor %}
240+
</div>
241+
{% endif %}
242+
220243
{% if page.language == "matlab" or page.language == "python" %}
221244
<hr>
222245
<h4 class="example-section-title" id="streaming"><a class="no_underline plot-blue" href="#streaming">Streaming</a></h4>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: Linear fit in Python | Examples | Plotly
3+
name: Linear Fit
4+
permalink: python/linear-fits/
5+
description: Create a polynomial fit / regression in Python and add a line of best fit to your chart
6+
layout: base
7+
thumbnail: thumbnail/linear_fit.jpg
8+
language: python
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: fitting_tools
12+
---
13+
{% assign examples = site.posts | where:"language","python" | where:"suite","linear_fit" | sort: "order" %}
14+
{% include auto_examples.html examples=examples %}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
name: Linear fit
3+
plot_url: https://plot.ly/~tarzzz/870
4+
language: python
5+
suite: linear_fit
6+
order: 0
7+
sitemap: false
8+
---
9+
# Learn about API authentication here: https://plot.ly/python/getting-started
10+
# Find your api_key here: https://plot.ly/settings/api
11+
12+
import plotly.plotly as py
13+
import plotly.graph_objs as go
14+
15+
# Scientific libraries
16+
from numpy import arange,array,ones
17+
from scipy import stats
18+
19+
20+
xi = arange(0,9)
21+
A = array([ xi, ones(9)])
22+
23+
# (Almost) linear sequence
24+
y = [19, 20, 20.5, 21.5, 22, 23, 23, 25.5, 24]
25+
26+
# Generated linear fit
27+
slope, intercept, r_value, p_value, std_err = stats.linregress(xi,y)
28+
line = slope*xi+intercept
29+
30+
# Creating the dataset, and generating the plot
31+
trace1 = go.Scatter(
32+
x=xi,
33+
y=y,
34+
mode='markers',
35+
marker=go.Marker(color='rgb(255, 127, 14)')
36+
)
37+
38+
trace2 = go.Scatter(
39+
x=xi,
40+
y=line,
41+
mode='lines',
42+
marker=go.Marker(color='rgb(31, 119, 180)')
43+
)
44+
45+
annotation = go.Annotation(
46+
x=3.5,
47+
y=23.5,
48+
text='$R^2 = 0.9551,\\Y = 0.716X + 19.18$',
49+
showarrow=False
50+
# font=go.Font(size=18)
51+
)
52+
layout = go.Layout(
53+
title='Linear Fit in Python',
54+
plot_bgcolor='rgb(229, 229, 229)',
55+
xaxis=go.XAxis(zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'),
56+
yaxis=go.YAxis(zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'),
57+
annotations=[annotation]
58+
)
59+
60+
data = [trace1, trace2]
61+
fig = go.Figure(data=data, layout=layout)
62+
63+
py.plot(fig, filename='Linear-Fit-in-python')
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: Polynomial fit in Python | Examples | Plotly
3+
name: Polynomial Fit
4+
permalink: python/polynomial-fits/
5+
description: Create a polynomial fit / regression in Python and add a line of best fit to your chart
6+
layout: base
7+
thumbnail: thumbnail/polynomial_fit.jpg
8+
language: python
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: fitting_tools
12+
---
13+
{% assign examples = site.posts | where:"language","python" | where:"suite","polynomial_fit" | sort: "order" %}
14+
{% include auto_examples.html examples=examples %}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
name: Polynomial fit
3+
plot_url: https://plot.ly/~tarzzz/895
4+
language: python
5+
suite: polynomial_fit
6+
order: 0
7+
description: Inspired by <a href="http://stackoverflow.com/a/19165440">Stack Overflow</a>.
8+
sitemap: false
9+
---
10+
# Learn about API authentication here: https://plot.ly/python/getting-started
11+
# Find your api_key here: https://plot.ly/settings/api
12+
13+
import plotly.plotly as py
14+
import plotly.graph_objs as go
15+
16+
# Scientific libraries
17+
import numpy as np
18+
19+
points = np.array([(1, 1), (2, 4), (3, 1), (9, 3)])
20+
21+
# get x and y vectors
22+
x = points[:,0]
23+
y = points[:,1]
24+
25+
# calculate polynomial
26+
z = np.polyfit(x, y, 3)
27+
f = np.poly1d(z)
28+
print f
29+
30+
# calculate new x's and y's
31+
x_new = np.linspace(x[0], x[-1], 50)
32+
y_new = f(x_new)
33+
34+
# Creating the dataset, and generating the plot
35+
trace1 = go.Scatter(
36+
x=x,
37+
y=y,
38+
mode='markers',
39+
marker=go.Marker(color='rgb(255, 127, 14)')
40+
)
41+
42+
trace2 = go.Scatter(
43+
x=x_new,
44+
y=y_new,
45+
mode='lines',
46+
marker=go.Marker(color='rgb(31, 119, 180)')
47+
)
48+
49+
annotation = go.Annotation(
50+
x=6,
51+
y=-4.5,
52+
text='$\textbf{Fit}: 0.43X^3 - 0.56X^2 + 16.78X + 10.61$',
53+
showarrow=False
54+
# font=go.Font(size=18)
55+
)
56+
layout = go.Layout(
57+
title='Polynomial Fit in Python',
58+
plot_bgcolor='rgb(229, 229, 229)',
59+
xaxis=go.XAxis(zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'),
60+
yaxis=go.YAxis(zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'),
61+
annotations=[annotation]
62+
)
63+
64+
data = [trace1, trace2]
65+
fig = go.Figure(data=data, layout=layout)
66+
67+
py.plot(fig, filename='Polynomial-Fit-in-python')

0 commit comments

Comments
 (0)