Mhmoud Khadija
Mhmoud Khadija

Reputation: 19

plot figure with different colors

I am trying to plot a figure that has many lines where each line represents a specifc temperature!

An example of what I want is here:

From Excel

However, I bulit the following code:

x=pd.DataFrame(df1, columns =[0]) 
J = set(x.iloc[:,0])
print ('Length Temperature',len(J))
O = len(J)
M = len(df1.index)
print('Indexxxxx: ',df1.iloc[0:12+0,5])
for i in range(0,M,O):
    figure3 = plt.Figure(figsize=(8, 6), dpi=80)
    ax1 = figure3.add_subplot(111)
    ax1.scatter(df1.iloc[i+1:M+i,5],df1.iloc[i+1:M+i,6], label = "Temperature " + str((df1.iloc[i, 0])))
    scatter1 = FigureCanvasTkAgg(figure3, GraphWindow)
    scatter1.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH)
    
    
    ax1.set_xlabel('Reduced Frequency [Hz]')
    ax1.set_ylabel('Complex Shear Modulus G*')
    ax1.set_yscale('log')
    ax1.set_xscale('log')
    ax1.set_title('MasterCurve ')
    ax1.set_facecolor('whitesmoke')

    figure3.patch.set_facecolor('whitesmoke')    
    ax1.spines['bottom'].set_color('black')
    ax1.spines['top'].set_color('black')
    ax1.spines['left'].set_color('black')
    ax1.spines['right'].set_color('black')
    toobar = NavigationToolbar2Tk(scatter1, GraphWindow)
    ax1.legend(['(Temperature)' +  str((df1.iloc[i, 0]))])
    hold(True)

Everything is fine in this code but I am obtaining the lines in blue and the legend is the same for all of them.. This is what I obtained:

From Python

My question is, how can I change the color of each line and add new legend in each iteration in the above for loop.

Thanks in advance!

Upvotes: 0

Views: 97

Answers (1)

gboffi
gboffi

Reputation: 25053

enter image description here

You want a single Figure, a single Axes and a single Canvas, and plot the different curves inside of them. In other words, you do too much inside the cycle…

import tkinter
from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import numpy as np

# frequencies and a poor man's dataframe
freq = np.logspace(-1.5, 1.5, 12)
G = {'ABCDE'[n-1]:np.logspace(n-4, n-2-n/4, 12) for n in range(1, 6)}

root = tkinter.Tk()
root.wm_title("Embedding in Tk")

fig = Figure(figsize=(6, 4), dpi=100, layout='constrained')
ax = fig.add_subplot()

# this is our loop on the different curves
lines = [ax.plot(freq, G[letter], '-o', label=letter)[0] for letter in G]
# titles, log axes, legend
ax.set_xlabel('Reduced Frequency [Hz]')
ax.set_ylabel('Complex Shear Modulus G*')
ax.set_yscale('log')
ax.set_xscale('log')
ax.set_title('MasterCurve ')
ax.set_facecolor('whitesmoke')
ax.legend()

connect the figure to a tkinter Canvas --- once
canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.
canvas.draw()

# boilerplate
toolbar = NavigationToolbar2Tk(canvas, root, pack_toolbar=False)
toolbar.update()
canvas.mpl_connect("key_press_event", key_press_handler)
button_quit = tkinter.Button(master=root, text="Quit", command=root.destroy)
button_quit.pack(side=tkinter.BOTTOM)
toolbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=True)

# let's show our curves
tkinter.mainloop()

Upvotes: 2

Related Questions