1
Good afternoon. I’m trying to create a graph in Python, in a Jupyter Notebook in Anaconda where I’d like to put the trend lines for each of the lines and the linear regression equation. I was able to put the dots on the chart from an Excel file, but I’m not getting the lines that connect all the dots together. How could I remove those lines? I was also able to put a trend line in red, but I don’t know how to put the trend line equation on the side. I would like Python to be able to calculate the slope and the ordinate at the origin of the trend line. How could you put the trend line label next to the line?
The Excel file is as follows:
The Python code follows below:
import matplotlib.pyplot as plt
from numpy import *
import numpy as np
import pandas as pd
df = pd.read_excel('ampliacao grafico 8min 9min 10min.xlsx', header=None, usecols=
[0,1,2,3,4,5,6], names=['A', 'B', 'C', 'D','E','F','G'])
plt.style.use('classic')
plt.rcParams["figure.figsize"] = [10.5,10]
plt.rc('axes', titlesize=16)
plt.rc('xtick', labelsize=12) # fontsize of the tick labels
plt.rc('ytick', labelsize=12)
plt.rc('legend', fontsize=14)
plt.rcParams["figure.facecolor"] = "w"
plt.plot(df['A'], df['B'], label='Temperatura da zona fria (8 ºC/min)', marker='o',
color='DodgerBlue', markersize=4)
plt.plot(df['A'], df['C'], label='Temperatura do termopar (8 ºC/min)', marker='o',
color='RoyalBlue', markersize=4)
plt.plot(df['A'], df['D'], label='Temperatura da zona fria (9 ºC/min)', marker='o',
color='PaleVioletRed', markersize=4)
plt.plot(df['A'], df['E'], label='Temperatura do termopar (9 ºC/min)', marker='o',
color='DeepPink', markersize=4)
plt.plot(df['A'], df['F'], label='Temperatura da zona fria (10 ºC/min)', marker='o',
color='MediumAquamarine', markersize=4)
plt.plot(df['A'], df['G'], label='Temperatura do termopar (10 ºC/min)', marker='o',
color='OliveDrab', markersize=4)
plt.scatter(df['A'], df['B'])
z = np.polyfit(df['A'], df['B'], 1)
p = np.poly1d(z)
plt.plot(df['A'],p(df['A']),"r--")
#plt.grid(b=True, color='black', alpha=0.3, linestyle='-.', linewidth=1) #caso quisesse a
grid completa e nao apenas a horizontal
axes = plt.gca()
axes.yaxis.grid(b=True, color='black', alpha=0.3, linestyle='-.', linewidth=1)
plt.xlabel('Tempo (minuto)', fontsize=15)
plt.ylabel('Temperatura (ºC)', fontsize=15)
plt.title('Perfil de temperatura do forno')
plt.legend(loc='lower right', prop={'size': 12})
#plt.show()
plt.savefig('output.png', dpi=500, bbox_inches='tight')
The result I got:
Second attempt:
import matplotlib.pyplot as plt
from numpy import *
import numpy as np
import pandas as pd
df = pd.read_excel('ampliacao grafico 8min 9min 10min.xlsx',
header=None, usecols=[0,1,2,3,4,5,6], names=['A', 'B', 'C',
'D','E','F','G'])
plt.style.use('classic')
plt.rcParams["figure.figsize"] = [10.5,10]
plt.rc('axes', titlesize=16)
plt.rc('xtick', labelsize=12) # fontsize of the tick labels
plt.rc('ytick', labelsize=12)
plt.rc('legend', fontsize=14)
plt.rcParams["figure.facecolor"] = "w"
plt.plot(df['A'], df['B'],'o', label='Temperatura da zona fria (8
ºC/min)', color='DodgerBlue', markersize=4)
plt.plot(df['A'], df['C'],'o', label='Temperatura do termopar (8
ºC/min)', color='RoyalBlue', markersize=4)
plt.plot(df['A'], df['D'], 'o', label='Temperatura da zona fria (9
ºC/min)', color='PaleVioletRed', markersize=4)
plt.plot(df['A'], df['E'], 'o', label='Temperatura do termopar (9
ºC/min)', color='DeepPink', markersize=4)
plt.plot(df['A'], df['F'], 'o', label='Temperatura da zona fria (10
ºC/min)', color='MediumAquamarine', markersize=4)
plt.plot(df['A'], df['G'],'o', label='Temperatura do termopar (10
ºC/min)', color='OliveDrab', markersize=4)
plt.scatter(df['A'], df['B'])
z = np.polyfit(df['A'], df['B'], 1)
p = np.poly1d(z)
plt.plot(df['A'],p(df['A']),'--', color='DodgerBlue')
plt.text(8,1,"y=%.3fx+%.3f"%(z[0],z[1]), ha='left')
z1 = np.polyfit(df['A'], df['C'], 1)
p1 = np.poly1d(z1)
plt.plot(df['A'],p1(df['A']),'--', color='RoyalBlue')
z2 = np.polyfit(df['A'], df['D'], 1)
p2 = np.poly1d(z2)
plt.plot(df['A'],p2(df['A']),'--', color='PaleVioletRed')
z3 = np.polyfit(df['A'], df['E'], 1)
p3 = np.poly1d(z3)
plt.plot(df['A'],p3(df['A']),'--', color='DeepPink')
z4 = np.polyfit(df['A'], df['F'], 1)
p4 = np.poly1d(z4)
plt.plot(df['A'],p4(df['A']),'--', color='MediumAquamarine')
z5 = np.polyfit(df['A'], df['G'], 1)
p5 = np.poly1d(z5)
plt.plot(df['A'],p5(df['A']),'--', color='OliveDrab')
#plt.grid(b=True, color='black', alpha=0.3, linestyle='-.',
linewidth=1) #caso quisesse a grid completa e nao apenas a horizontal
axes = plt.gca()
axes.yaxis.grid(b=True, color='black', alpha=0.3, linestyle='-.',
linewidth=1)
plt.xlabel('Tempo (minuto)', fontsize=15)
plt.xlim(0, 30)
plt.ylabel('Temperatura (ºC)', fontsize=15)
plt.ylim(0, 350)
plt.title('Perfil de temperatura do forno')
plt.legend(loc='lower right', prop={'size': 12})
#plt.show()
plt.savefig('output.png', dpi=500, bbox_inches='tight')
New chart:
To remove the lines that are joining the chart posts just change the chart type
plot
for scatter and to write the trend line equation just use the attribute text– Solkarped
@Solkarped I tried a second time and I’ve already managed to get the straights out and put the trend lines, but I’m not getting the trend line equations down to the right, in the position I need. How can I change to the equation stand right next to each straight? I will put my second try in the post
– Carmen González
On the link
text
that I made available in the first comment, you will be able to implement and position your equation. To better understand the formation of the equations visit this link. Now, if you intend to insertTODAS
equations, your graph will be well polluted.– Solkarped
@Solkarped Thank you for your help. I think I just need one thing. How can I change the '.' (dot) separator to a ',' comma on the trend line label? They usually put point because it’s the American system, but I wanted to put the numbers in decimal.
– Carmen González
Already tried using the function
replace
? It would look something like.replace(".", ",")
.– Solkarped
@Solkarped Thank you for the suggestion. Could you answer this complete change in the code please? I never used the function
replace
so I don’t really know how to apply it.– Carmen González
Carmen, good morning! Has the graphic question been solved? Hug!
– lmonferrari
@lmonferrari Yes I have! Thank you all very much!
– Carmen González