'How to solve Matplotlib and Dataframe confusion?

import pandas as pd
import tkinter as tk
from tkinter import *        
from PIL import ImageTk, Image

app_root = Tk()

#Setting it up
img = ImageTk.PhotoImage(Image.open("predictsodapng.png"))

#Displaying it
imglabel = Label(app_root, image=img).grid(row=0, column=0)   

app_root.title("Predict Soda")
app_root.geometry("800x600")

app_root.mainloop()


def show_entry_fields():
    print("Bit Diameter as cms: %s\nWell Name: %s" % (float(e1.get()), e2.get()))

master = tk.Tk()
tk.Label(master, 
         text="Bit Diameter as cms").grid(row=0)
tk.Label(master, 
         text="Well Name").grid(row=1)

e1 = tk.Entry(master)
e2 = tk.Entry(master)


e1.grid(row=0, column=1)
e2.grid(row=1, column=1)

e1_float = 0.0          # Storage for results on exit
e2_string = ""          # Storage for results on exit


                
def get_float( entry ):
    """ Converts the characters in an Entry to a float, 
        returns 0.0 if not a valid float"""
    try: 
        return float( entry.get() )
    except ValueError:
        return 0.0
    
def save_and_quit():
    global e1_float, e2_string
    e1_float = get_float( e1 )
    e2_string = e2.get()
    print( type( 'e1'),  'e1', type(e1), e1 )   # Prints to see what happens.
    print( type( e1.get()), e1.get(), type(e1_float), e1_float )
    master.destroy()

tk.Button(master, 
          text='Save & Quit', 
          command=save_and_quit).grid(row=3, 
                                      column=0, 
                                      sticky=tk.W, 
                                      pady=4)
tk.Button(master, 
          text='Show', command=show_entry_fields).grid(row=3, 
                                                       column=1, 
                                                       sticky=tk.W, 
                                                       pady=4)
    

tk.mainloop()


from tkinter import filedialog 

app_root_r = Tk()

file = filedialog.askopenfilename()

if file:
        try:
            filename = r"{}".format(file)
            df = pd.read_excel(file)
        except ValueError:
            label.config(text="File could not be opened")
        except FileNotFoundError:
            label.config(text="File Not Found")
                         


data = df.loc[df['WELL'] == e2_string ][['DEPTH_MD', 'CALIPER', 'GR', 'LITHOLOGY', 'SHALLOW', 'DEEP']]



e1_float = e1_float * 30/100 + e1_float

df.loc[(df.GR < 30.0000) & (df.CALIPER > e1_float) & (df.SHALLOW > 2.60000)  , "LITHOLOGY"] = '1' 
df.loc[(df.GR > 100.0000) , "LITHOLOGY"] = '3' 
df.LITHOLOGY.fillna('2', inplace=True)



df.to_excel(file)


df = pd.read_excel(file)

app_root_r.mainloop()


lithology_numbers = {1: {'lith':'Trona', 'lith_num':1, 'hatch': '+', 'color':'aqua'},
                 2: {'lith':'Shale, Claystone', 'lith_num':2, 'hatch':'-.', 'color':'black'},
                 3: {'lith':'Tuff', 'lith_num':3, 'hatch':'o', 'color':'orange'}}
df_lith = pd.DataFrame.from_dict(lithology_numbers, orient='index')
df_lith.index.name = 'LITHOLOGY'
df_lith

y = [0, 1]
x = [1, 1]

from matplotlib import pyplot as plt

fig, axes = plt.subplots(ncols=4,nrows=3, sharex=True, sharey=True,
                         figsize=(10,5), subplot_kw={'xticks': [], 'yticks': []})

for ax, key in zip(axes.flat, lithology_numbers.keys()):
    ax.plot(x, y)
    ax.fill_betweenx(y, 0, 1, facecolor=lithology_numbers[key]['color'], hatch=lithology_numbers[key]['hatch'])
    ax.set_xlim(0, 0.1)
    ax.set_ylim(0, 1)
    ax.set_title(str(lithology_numbers[key]['lith']))

plt.tight_layout()
plt.show()

def makeplot(well, top_depth, bottom_depth):
    fig, ax = plt.subplots(figsize=(15,10))

    ax1 = plt.subplot2grid((1,5), (0,0), rowspan=1, colspan = 1)
    ax5 = ax1.twiny()
    ax2 = plt.subplot2grid((1,5), (0,1), rowspan=1, colspan = 1, sharey = ax1)
    ax3 = ax2.twiny() #Twins the y-axis for the density track with the neutron track
    ax4 = plt.subplot2grid((1,5), (0,3), rowspan=1, colspan = 1, sharey = ax1)
    ax6 = plt.subplot2grid((1,5), (0,2), rowspan=1, colspan = 1, sharey = ax1)
    ax7 = ax6.twiny()

    ax10 = ax1.twiny()
    ax10.xaxis.set_visible(False)
    ax11 = ax2.twiny()
    ax11.xaxis.set_visible(False)
    ax13 = ax4.twiny()
    ax13.xaxis.set_visible(False)
    ax14 = ax6.twiny()
    ax14.xaxis.set_visible(False)

    # Gamma Ray track
    ax1.plot(well["GR"], well['DEPTH_MD'], color = "green", linewidth = 0.5)
    ax1.set_xlabel("Gamma")
    ax1.xaxis.label.set_color("green")
    ax1.set_xlim(0, 200)
    ax1.set_ylabel("Depth (m)")
    ax1.tick_params(axis='x', colors="green")
    ax1.spines["top"].set_edgecolor("green")
    ax1.title.set_color('green')
    ax1.set_xticks([0, 50, 100, 150, 200])

    # Caliper track
    ax2.plot(well["CALIPER"], well['DEPTH_MD'], color = "red", linewidth = 0.5)
    ax2.set_xlabel("Caliper")
    ax2.set_xlim(0, 50)
    ax2.xaxis.label.set_color("red")
    ax2.tick_params(axis='x', colors="red")
    ax2.spines["top"].set_edgecolor("red")
    ax2.set_xticks([0, 10, 20, 30, 40, 50])

  # Resistivity track placed ontop of density track(SHALLOW)
    ax6.plot(well["SHALLOW"], well['DEPTH_MD'], color = "blue", linewidth = 0.5)
    ax6.set_xlabel('Resistivity')
    ax6.xaxis.label.set_color("blue")
    ax6.set_xlim(0, 15)
    ax6.tick_params(axis='x', colors="blue")
    ax6.spines["top"].set_position(("axes", 1.08))
    ax6.spines["top"].set_visible(True)
    ax6.spines["top"].set_edgecolor("blue")
    ax6.set_xticks([0, 1, 2, 15])
    
  # Resistivity (deep)
    ax7.plot(well["DEEP"], well['DEPTH_MD'], color = "blue", linewidth = 0.5)
    ax7.xaxis.label.set_color("blue")
    ax7.tick_params(axis='x', colors="blue")
    ax7.spines["top"].set_position(("axes", 1.08))
    ax7.spines["top"].set_visible(True)
    ax7.spines["top"].set_edgecolor("blue")
    ax7.set_xticks([1, 2, 15])
    

  

    # Lithology track
    ax4.plot(well["LITHOLOGY"], well['DEPTH_MD'], color = "black", linewidth = 0.5)
    ax4.set_xlabel("Lithology")
    ax4.xaxis.label.set_color("black")
    ax4.set_xlim(0,1)
    ax4.tick_params(axis='x', colors="black")
    ax4.spines["top"].set_edgecolor("black")
    
    
    for key in lithology_numbers.keys():
        color = lithology_numbers[key]['color']
        hatch = lithology_numbers[key]['hatch']
        ax4.fill_betweenx(well['DEPTH_MD'], 0, well['LITHOLOGY'], where=(well['LITHOLOGY']==key),
                         facecolor=color, hatch=hatch)
        

    ax4.set_xticks([0, 1])


    for ax in [ax1, ax2, ax4, ax6]:
        ax.set_ylim(bottom_depth, top_depth)
        ax.grid(which='major', color='lightgrey', linestyle='-')
        ax.xaxis.set_ticks_position("top")
        ax.xaxis.set_label_position("top")
        ax.spines["top"].set_position(("axes", 1.02))
        
        
    for ax in [ax2, ax3, ax4, ax6]:
        plt.setp(ax.get_yticklabels(), visible = False)
        
    plt.tight_layout()
    fig.subplots_adjust(wspace = 0.15)
    
    for key in lithology_numbers.keys():
     color = lithology_numbers[key]['color']
     hatch = lithology_numbers[key]['hatch']
     ax4.fill_betweenx(well['DEPTH_MD'], 0, well['LITHOLOGY'], 
                      where=(well['LITHOLOGY']==key),
                      facecolor=color, hatch=hatch)
    
    


makeplot(data, 230,330)


plt.savefig("input.jpg")


from PIL import ImageTk, Image

root = Tk()

img = ImageTk.PhotoImage(Image.open("input.jpg"))  


l=Label(image=img)


l.pack()

root.mainloop()



I have these codes, in order to make comparison and plot. And also Im trying to create an interface using tkinter. I use the LITHOLOGY column for drawing after printing. On the other hand, tkinter uses the first version of this printed excel file instead of the current one(you can see in the first picture). However, when I run the same codes again, i.e. run the same for the second time, you can see the result in second picture(this shows how my output should be). There is no problem as the LITHOLOGY column is already printed to the excel file when i run it for the second time. I want to eliminate this problem. I have to draw the data directly from the dataframe, but when I replace the well[LITHOLOGY] with df[LITHOLOGY], it only shows the values ​​instead of drawing and painting as you can see in the third figure. How can I solve it? Thanks.

First

Second

Third



Solution 1:[1]

Your problem is the order you do your operations. Look at what you do:

df = pd.read_excel(file)
...
data = df.loc[df['WELL'] == e2_string ][['DEPTH_MD', 'CALIPER', 'GR', 'LITHOLOGY', 'SHALLOW', 'DEEP']]
...
df.loc[(df.GR < 30.0000) & (df.CALIPER > e1_float) & (df.SHALLOW > 2.60000)  , "LITHOLOGY"] = '1' 
df.loc[(df.GR > 100.0000) , "LITHOLOGY"] = '3' 
df.LITHOLOGY.fillna('2', inplace=True)

df.to_excel(file)
...
makeplot(data, 230, 330)

Your complaint is that the plot is plotting your data WITHOUT yourLITHOLOGY column. That happens because you asked it to do that: you extract the data dataframe before you fill in the LITHOLOGY column in your df, so data doesn't get any of the modified data.

The obvious solution is to move the data = ... line AFTER you setup the LITHOLOGY column. Right?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Roberts