Tkinter input for pyplot graph - matplotlib

I am very new to Python(coming from C/C++) and I have some trouble getting this little program to work.
It will get some market date from yahoo and print out two graphs. The graphs were working at first and then I added the tkinter input window. Just press "submit" and "print". The problem is that the graphs are not plotted, I assume it has sth to do with the tkinter not finishing or that it cannot open a second window anymore, any help would be highly appreciated
import pandas.io.data as web
import pandas as pd
import numpy as np
import datetime
import math
import matplotlib.pyplot as plt
import random
from itertools import accumulate
from tkinter import *
from pandas.io.data import DataReader
#Functions
def VWAP(data):
return(np.cumsum(data.Bought*data.Close)/
np.cumsum(data.Bought)).round(2)
def Print():
df2 = web.DataReader(conversion.moniker, 'yahoo', conversion.start_date, conversion.end_date)
df2['30_SMA_Close'] = pd.stats.moments.rolling_mean(df2['Close'], 30)
df2['150_SMA_Close'] = pd.stats.moments.rolling_mean(df2['Close'], 150)
df2["Bought"] = pd.Series(np.random.randint(1,1000,len(df2.index)), index=df2.index)
df2["vwap"] = pd.Series(VWAP(df2), index=df2.index)
#print("VWAP : ", df2["vwap"])
print("----------------PLOT-----------------")
top = plt.subplot2grid((6,6), (0, 0), rowspan=3, colspan=6)
top.plot(df2.index, df2["Close"], label='Close')
top.plot(df2.index, df2['30_SMA_Close'], label='30 Day SMA')
top.plot(df2.index, df2['150_SMA_Close'], label='150 Day SMA')
top.plot(df2.index, df2['vwap'], label='VWAP',color='k',linestyle="--")
plt.title('S&P Price from 2007 - 2012')
plt.legend(loc='upper left', numpoints = 1, prop={'size':7})
bottom = plt.subplot2grid((6,6), (4,0), rowspan=2, colspan=6)
bottom.bar(df2.index, df2['Volume'])
plt.title('S&P Trading Volume in Millions')
plt.gcf().set_size_inches(15,8)
print("----------------PLOT END-----------------")
def conversion():
print("Data conversion")
conversion.moniker = e1.get()
conversion.start_date = datetime.datetime.strptime(e2.get(),'%Y-%m-%d')
conversion.end_date = datetime.datetime.strptime(e3.get(),'%Y-%m-%d')
master = Tk()
Label(master, text="Moniker Name").grid(row=0)
Label(master, text="Start Date").grid(row=1)
Label(master, text="End Date").grid(row=2)
e1 = Entry(master)
e1.insert(10,"SPY")
e2 = Entry(master)
e2.insert(10,"2010-12-31")
e3 = Entry(master)
e3.insert(10,"2014-01-01")
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
def close_window (): master.destroy()
Button(master, text='Cancel', command=close_window).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Submit', command=conversion).grid(row=3, column=1, sticky=W, pady=4)
Button(master, text='Print', command=Print).grid(row=3, column=2, sticky=W, pady=4)
#df2 = web.DataReader(conversion.moniker, 'yahoo', conversion.start_date, conversion.end_date)
#df2 = web.DataReader(conversion.moniker, 'yahoo',datetime.datetime(2010,1,1),datetime.datetime(2014,1,1) )
#mainloop( )
master.destroy()

I can't run all code because I don't have some modules
but if I press submit and then print I got some graph.
I changed almost nothing.
EDIT: I found problem - you need plt.show() before print('PLOT END')
Here my code with mods
button Print calls conversion so I don't need button Submit
dates in plot title.
.
import pandas as pd
import pandas.io.data as web
import numpy as np
import datetime
#import math
import matplotlib.pyplot as plt
import random
#from itertools import accumulate
from tkinter import *
from pandas.io.data import DataReader
#Functions
def VWAP(data):
return(np.cumsum(data.Bought*data.Close)/
np.cumsum(data.Bought)).round(2)
def ploting():
conversion()
df2 = web.DataReader(conversion.moniker, 'yahoo', conversion.start_date, conversion.end_date)
df2['30_SMA_Close'] = pd.stats.moments.rolling_mean(df2['Close'], 30)
df2['150_SMA_Close'] = pd.stats.moments.rolling_mean(df2['Close'], 150)
df2["Bought"] = pd.Series(np.random.randint(1,1000,len(df2.index)), index=df2.index)
df2["vwap"] = pd.Series(VWAP(df2), index=df2.index)
#print("VWAP : ", df2["vwap"])
print("----------------PLOT-----------------")
top = plt.subplot2grid((6,6), (0, 0), rowspan=3, colspan=6)
print top.plot(df2.index, df2["Close"], label='Close')
print top.plot(df2.index, df2['30_SMA_Close'], label='30 Day SMA')
print top.plot(df2.index, df2['150_SMA_Close'], label='150 Day SMA')
print top.plot(df2.index, df2['vwap'], label='VWAP',color='k',linestyle="--")
print plt.title('S&P Price from %s - %s' % (e2.get(), e3.get()))
print plt.legend(loc='upper left', numpoints = 1, prop={'size':7})
bottom = plt.subplot2grid((6,6), (4,0), rowspan=2, colspan=6)
print bottom.bar(df2.index, df2['Volume'])
print plt.title('S&P Trading Volume in Millions')
print plt.gcf().set_size_inches(15,8)
print plt.show() # <---- HERE
print("----------------PLOT END-----------------")
def conversion():
print("Data conversion")
conversion.moniker = e1.get()
conversion.start_date = datetime.datetime.strptime(e2.get(),'%Y-%m-%d')
conversion.end_date = datetime.datetime.strptime(e3.get(),'%Y-%m-%d')
#-----------------------------------------------------------------------------
def close_window ():
master.destroy()
master = Tk()
Label(master, text="Moniker Name").grid(row=0)
Label(master, text="Start Date").grid(row=1)
Label(master, text="End Date").grid(row=2)
e1 = Entry(master)
e1.insert(10,"SPY")
e2 = Entry(master)
e2.insert(10,"2010-12-31")
e3 = Entry(master)
e3.insert(10,"2014-01-01")
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
Button(master, text='Cancel', command=close_window).grid(row=3, column=0, sticky=W, pady=4)
#Button(master, text='Submit', command=conversion).grid(row=3, column=1, sticky=W, pady=4)
Button(master, text='Plot', command=ploting).grid(row=3, column=2, sticky=W, pady=4)
#df2 = web.DataReader(conversion.moniker, 'yahoo', conversion.start_date, conversion.end_date)
#df2 = web.DataReader(conversion.moniker, 'yahoo', datetime.datetime(2010,1,1),datetime.datetime(2014,1,1) )
master.mainloop()
2008-12-31 - 2013-01-01
1998-12-31 - 2004-01-01

Related

How can the x-axis dates be formatted without hh:mm:ss using matplotlib DateFormatter?

I am pulling in data on Japanese GDP and graphing a stacked barchart overlayed with a line. I would like for the x-axis to have only yyyy-mm and no timestamp. I read about a compatability issue with pandas and matplotlib epochs. Is that the issue here? When I try to use matplotlib Dateformatter, the returned dates begin with 1970. How can I fix this?
import pandas as pd
import pandas_datareader.data as web
import datetime
import requests
import investpy
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
start1 = '01/01/2013' #dd/mm/yyyy
end1 = '22/04/2022'
# Real GDP growth
# Source: Cabinet Office http://www.esri.cao.go.jp/en/sna/sokuhou/sokuhou_top.html
# Get the data
url = 'https://www.esri.cao.go.jp/jp/sna/data/data_list/sokuhou/files/2021/qe214_2/tables/nritu-jk2142.csv'
url2 = url.replace('nritu','nkiyo') # URL used for GDP growth by component
url3 = url.replace('nritu-j', 'gaku-m')
url4 = url.replace('nritu', 'gaku')
url5 = url.replace('nritu', 'kgaku')
df = pd.read_csv(url2, header=5, encoding='iso-8859-1').loc[49:]
gdpkeep = {
'Unnamed: 0': 'date',
'GDP(Expenditure Approach)': 'GDP',
'PrivateConsumption': 'Consumption',
'PrivateResidentialInvestment': 'inv1',
'Private Non-Resi.Investment': 'inv2',
'Changein PrivateInventories': 'inv3',
'GovernmentConsumption': 'gov1',
'PublicInvestment': 'gov2',
'Changein PublicInventories': 'gov3',
'Goods & Services': 'Net Exports'
}
df = df[list(gdpkeep.keys())].dropna()
df.columns = df.columns.to_series().map(gdpkeep)
# Adjust the date column to make each value a consistent format
dts = df['date'].str.split('-').str[0].str.split('/ ')
for dt in dts:
if len(dt) == 1:
dt.append(dt[0])
dt[0] = None
df['year'] = dts.str[0].fillna(method='ffill')
df['month'] = dts.str[1].str.zfill(2)
df['date2'] = df['year'].str.cat(df['month'], sep='-')
df['date'] = pd.to_datetime(df['date2'], format='%Y-%m')
# Sum up various types of investment and government spending
df['Investment'] = df['inv1'] + df['inv2'] + df['inv3']
df['Government Spending'] = df['gov1'] + df['gov2'] + df['gov3']
df = df.set_index('date')[['GDP', 'Consumption', 'Investment', 'Government Spending', 'Net Exports']]
df.to_csv('G:\\AutomaticDailyBackup\\Python\\MacroEconomics\\Japan\\Data\\gdp.csv', header=True) # csv file created
print(df.tail(8))
# Plot
df['Net Exports'] = df['Net Exports'].astype(float)
ax = df[['Consumption', 'Investment', 'Government Spending', 'Net Exports']]['2013':].plot(label=df.columns, kind='bar', stacked=True, figsize=(10, 10))
ax.plot(range(len(df['2013':])), df['GDP']['2013':], label='Real GDP', marker='o', linestyle='None', color='black')
plt.title('Japan: Real GDP Growth')
plt.legend(frameon=False, loc='upper left')
ax.set_frame_on(False)
ax.set_ylabel('Annual Percent Change')
# dfmt = mdates.DateFormatter("%Y-%m") # proper formatting Year-month
# ax.xaxis.set_major_formatter(dfmt)
plt.savefig('G:\\AutomaticDailyBackup\\Python\\MacroEconomics\\Japan\\Data\\RealGDP.png')
plt.show()```
Don't use DateFormatter as it is causing trouble, rather change format of the dataframe index using df.index = pd.to_datetime(df.index, format = '%m/%d/%Y').strftime('%Y-%m')
Here is what I did with your gdp.csv file
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib.dates as mdates
from matplotlib.dates import DateFormatter
import matplotlib.dates
df=pd.read_csv("D:\python\gdp.csv").set_index('date')
df.index = pd.to_datetime(df.index, format = '%m/%d/%Y').strftime('%Y-%m')
# Plot
fig, ax = plt.subplots()
df['Net Exports'] = df['Net Exports'].astype(float)
ax = df[['Consumption', 'Investment', 'Government Spending', 'Net Exports']]['2013':].plot(label=df.columns, kind='bar', stacked=True, figsize=(10, 10))
ax.plot(range(len(df['2013':])), df['GDP']['2013':], label='Real GDP', marker='o', linestyle='None', color='black')
plt.legend(frameon=False, loc='upper left')
ax.set_frame_on(False)
plt.savefig(r'D:\python\RealGDP.png')
plt.show()

Matplotlib clearing the figure/axis for new plot

am trying to figure out how to clear the axis in readiness for new plotting, I have tried ax.clf(), fig.clf() but nothing is happening. where am I not doing well? at the moment am not getting any errors and am using Matplotlib vers. 3.4.3.
from tkinter import *
import matplotlib.pyplot as plt
import numpy as np
import time
import datetime
import mysql.connector
import matplotlib.dates as mdates
my_connect = mysql.connector.connect(host="localhost", user="Kennedy", passwd="Kennerdol05071994", database="ecg_db", auth_plugin="mysql_native_password")
mycursor = my_connect.cursor()
voltage_container = []
time_container = []
def analyze_voltage_time():
global ax, fig
pat_id = 1
query = "SELECT voltage, time FROM ecg_data_tbl where patient_id = " +str(pat_id)
mycursor.execute(query)
result = mycursor .fetchall()
voltage, time = list(zip(*result))
for volts in voltage:
voltage_container.append(volts)
for tim in time:
time_container.append(str(tim))
fig = plt.figure(1, figsize = (15, 6), dpi = 80, constrained_layout = True)
ax = fig.add_subplot()
ax.plot(time_container, voltage_container)
for label in ax.get_xticklabels():
label.set_rotation(40)
label.set_horizontalalignment('right')
ax.set_title("Electrocadiogram")
ax.set_xlabel("Time(hh:mm:ss)")
ax.set_ylabel("Voltage(mV)")
ax.grid(b=True, which='major', color='#666666', linestyle='-')
ax.minorticks_on()
ax.grid(b=True, which='minor', color='#666666', linestyle='-', alpha=0.2)
plt.show()
def clear_():
ax.cla()
fig.clf()
# =================================MAIN GUI WINDOW======================================
analysis_window = Tk()
analysis_window.configure(background='light blue')
analysis_window.iconbitmap('lardmon_icon.ico')
analysis_window.title("ECG-LArdmon - ANALYZER")
analysis_window.geometry('400x200')
analysis_window.resizable(width=False, height=False)
# ===========================BUTTONS===================================
analyse_btn = Button(analysis_window, text='analyze', width = 20, command=analyze_voltage_time)
analyse_btn.pack()
clear_btn = Button(analysis_window, text= 'clear', width = 20, command=clear_)
clear_btn.pack()
analysis_window.mainloop()

pulling data out of bins in density map created with matplotlib

I have created a lightning density map using lines of data representing lightning strikes. One line is shown below:
1996-01-17 03:54:35.853 44.9628 -78.9399 -37.9
Now that I have applied these lines of data to the density map and distributed them into their appropriate bins based on Lat/Long, I would like to pull the data back out specific to the bin that it fell into so that I can manipulate that data further.
I have tried to find answers to this online but have failed to find anything that is specific to what I am trying to do. Any and all help is greatly appreciated!
my code:
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.axes as ax
import numpy as np
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from metpy.plots import USCOUNTIES
from matplotlib.axes import Axes
from cartopy.mpl.geoaxes import GeoAxes
GeoAxes._pcolormesh_patched = Axes.pcolormesh
import datetime
fig, ax = plt.subplots(figsize=(15,15),subplot_kw=dict(projection=ccrs.Stereographic(central_longitude=-76, central_latitude=43)))
ax.set_extent([-79, -73, 42, 45],ccrs.Geodetic())
ax.add_feature(USCOUNTIES.with_scale('500k'), edgecolor='gray', linewidth=0.25)
ax.add_feature(cfeature.STATES.with_scale('50m'))
winter = [12, 1, 2]
summer = [6, 7, 8]
seasondata = []
lons=[]
lats=[]
f = open("2007-2016.txt", "r")
for line in f.readlines():
parts = line.split()
dates = parts[0]
charges = float(parts[4])
date = datetime.datetime.strptime(dates, "%Y-%m-%d")
#if date.month in summer:
if date.month in winter:
seasondata.append(line)
if charges <= 0:
seasondata.append(line)
lon = float(parts[3])
lat = float(parts[2])
lons.append(lon)
lats.append(lat)
if charges >= 15:
seasondata.append(line)
lon = float(parts[3])
lat = float(parts[2])
lons.append(lon)
lats.append(lat)
lons=np.array(lons)
lats=np.array(lats)
ax.set_title('2007-2016 Jan, Feb, Dec: Lightning Density', loc ='Left')
xynps = (ax.projection.transform_points(ccrs.Geodetic(), lons, lats))
bins=[300,240]
h2d, xedges, yedges, im = ax.hist2d(xynps[:,0], xynps[:,1], bins=bins, cmap=plt.cm.YlOrRd, zorder=10, alpha=0.4)
lons=[]
lats=[]
f = open("turbine.txt", "r")
for line in f.readlines():
parts = line.split()
lat=float(parts[0])
lon=float(parts[1])
lats.append(lat)
lons.append(lon)
markerSymbol='o'
markerSize=10
ax.scatter(lons, lats, transform=ccrs.PlateCarree(), marker = markerSymbol, s=markerSize, c='b')
cbar = plt.colorbar(im, fraction=0.046, pad=0.04)
cbar.set_label("Flashes per 2km^2")
plt.show()

how can i make my figure made by matplotlib response to a mouse click event?

I read the document of matplotlib and write the following code, it supposed to capture my mouse event and move the grey line position when i clicked. I read this code in jupiter notebook online, it stop to show the coordinate of my cursor as it usually do, What's happend? Can anyone help me?
import pandas as pd
import numpy as np
import matplotlib.colors as mcol
import matplotlib.cm as cm
from scipy import stats
from matplotlib.lines import Line2D
import matplotlib.pyplot as plt
import scipy.spatial as spatial
np.random.seed(12345)
df = pd.DataFrame([np.random.normal(33500,150000,3650),
np.random.normal(41000,90000,3650),
np.random.normal(41000,120000,3650),
np.random.normal(48000,55000,3650)],
index=[1992,1993,1994,1995])
fig, ax = plt.subplots()
year_avg = df.mean(axis = 1)
year_std = df.std(axis = 1)
yerr = year_std / np.sqrt(df.shape[1]) * stats.t.ppf(1-0.05/2, df.shape[1]-1)
bars=ax.bar(range(df.shape[0]), year_avg, yerr = yerr, color = 'lightslategrey')
threshold=42000
line=plt.axhline(y = threshold, color = 'grey', alpha = 0.5)
cm1 = mcol.LinearSegmentedColormap.from_list("CmapName",["yellow", "orange", "red"])
cpick = cm.ScalarMappable(cmap=cm1)
percentages = []
cpick.set_array([])
def setColor(bars, yerr,threshold):
for bar, yerr_ in zip(bars, yerr):
low = bar.get_height() - yerr_
high = bar.get_height() + yerr_
percentage = (high-threshold)/(high-low)
if percentage>1: percentage = 1
if percentage<0: percentage=0
percentages.append(percentage)
cpick.to_rgba(percentages)
bars = ax.bar(range(df.shape[0]), year_avg, yerr = yerr, color = cpick.to_rgba(percentages))
return bars
line=plt.axhline(threshold, color = 'grey', alpha = 0.5)
setColor(bars, yerr,threshold)
plt.colorbar(cpick, orientation='horizontal')
plt.xticks(range(df.shape[0]), df.index)
fig = plt.figure()
plt.show()
def onclick(event):
print('%s click: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
('double' if event.dblclick else 'single', event.button,
event.x, event.y, event.xdata, event.ydata))
line.set_ydata(event.ydata)
#plt.draw()
cid = fig.canvas.mpl_connect('button_press_event', onclick)

How to prevent stack plots repeating plots?

I have a code that should produce two different graphs and place them into one image and cannot figure out why it returns the last mentioned graph twice. The code is as follows:
import spacepy as sp
from spacepy import pycdf
from pylab import *
from spacepy.toolbox import windowMean, normalize
from spacepy.plot.utils import annotate_xaxis
import pylab
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.cbook as cbook
import matplotlib.ticker as ticker
from matplotlib.colors import LogNorm
from matplotlib.ticker import LogLocator
from matplotlib.dates import DateFormatter
from matplotlib.dates import DayLocator, HourLocator, MinuteLocator
from matplotlib import rc, rcParams
import matplotlib.dates as mdates
import datetime as dt
import bisect as bi
import seaborn as sea
import sys
import os
import multilabel as ml
import pandas as pd
sea.set_context('poster')
# sea.set_style('ticks',{'axes.facecolor':'yellow'})
sea.set_style('whitegrid')
sea.set_palette('muted',color_codes=True)
rc('text', usetex=True)
rc('font', family='Mono')
rcParams['text.latex.preamble']=[r'\usepackage{amsmath}']
MMS_1_HPCA_SURVEY_ION = pycdf.CDF(r'/home/ary/Desktop/Arya/Project/Data/MMS/1/HPCA/Survey/Ion/mms1_hpca_srvy_l2_ion_20151025120000_v1.0.0.cdf')
EPOCH_SURVEY_ION_1 = MMS_1_HPCA_SURVEY_ION['Epoch'][...]
H_Flux_SURVEY_ION_1 = MMS_1_HPCA_SURVEY_ION['mms1_hpca_hplus_flux'][...]
O_Flux_SURVEY_ION_1 = MMS_1_HPCA_SURVEY_ION['mms1_hpca_oplus_flux'][...]
Ion_Energy_SURVEY_ION_1 = MMS_1_HPCA_SURVEY_ION['mms1_hpca_ion_energy'][...]
MMS_SURVEY_ION_1_Start_time = dt.datetime(2015, 10, 25, 12, 0, 0, 908117)
MMS_SURVEY_ION_1_Finish_time = dt.datetime(2015, 10, 25, 16, 22, 24, 403623)
dt_MMS = dt.timedelta(seconds = 15)
plt.close('all')
fig_MMS, axs_MMS = plt.subplots(2,sharex=True)
cmap = plt.get_cmap(cm.jet)
cmap.set_bad('black')
sidx_MMS_1_SURVEY_ION = bi.bisect_left(EPOCH_SURVEY_ION_1,MMS_SURVEY_ION_1_Start_time)
sidx_MMS_1_SURVEY_ION = int(sidx_MMS_1_SURVEY_ION-(sidx_MMS_1_SURVEY_ION/100))
lidx_MMS_1_SURVEY_ION = bi.bisect_left(EPOCH_SURVEY_ION_1, MMS_SURVEY_ION_1_Finish_time)
lidx_MMS_1_SURVEY_ION = int(lidx_MMS_1_SURVEY_ION+((len(EPOCH_SURVEY_ION_1)-lidx_MMS_1_SURVEY_ION)/100))
if MMS_SURVEY_ION_1_Start_time.date() == MMS_SURVEY_ION_1_Finish_time.date():
stopfmt = '%H:%M'
else:
stopfmt = '%-m/%-d/%y %H:%M'
title_1 = MMS_SURVEY_ION_1_Start_time.strftime('%m/%d/%y %H:%M')+' -'+MMS_SURVEY_ION_1_Finish_time.strftime(stopfmt)
if dt_MMS.seconds !=0:
title_1 = title_1 + ' with '+str(dt_MMS.seconds)+' second time averaging'
for j, ax in enumerate(axs_MMS.T.flatten()):
flix_1 = np.array(H_Flux_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION,
j, :].T)
if dt_MMS==dt.timedelta(0):
fluxwin_1 = flix_1
timewin_1 = EPOCH_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION]
else:
fluxwin_1=[[0 for y in range(len(flix_1))] for x_1 in range(len(flix_1))]
for i, flox in enumerate(flix_1):
fluxwin_1[i], timewin_1 = windowMean(flox, EPOCH_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION],
winsize=dt_MMS, overlap=dt.timedelta(0))
fluxwin_1[i] = np.array(fluxwin_1[i])
for x_1 in np.where(np.diff(EPOCH_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION])
>dt.timedelta(hours=1))[0]+sidx_MMS_1_SURVEY_ION:
fluxwin_1[i][bi.bisect_right(timewin_1, EPOCH_SURVEY_ION_1[x_1]):bi.bisect_right(timewin_1,
EPOCH_SURVEY_ION_1[x_1+1])]=0
fluxwin_1 = np.array(fluxwin_1)
fluxwin_1[np.where(fluxwin_1<=0)] = 0
x_1 = mdates.date2num(timewin_1)
pax_1 = ax.pcolormesh(x_1, Ion_Energy_SURVEY_ION_1, fluxwin_1, shading='turkey',cmap=cmap, vmin=1,
vmax=np.nanmax(H_Flux_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION,:,:]),
norm=LogNorm())
sax_1 = ax.twinx()
plt.setp(sax_1.get_yticklabels(), visible=False)
sax_1.tick_params(axis='y', right='off')
ax.set_xlim(MMS_SURVEY_ION_1_Start_time, MMS_SURVEY_ION_1_Finish_time)
ax.set_yscale('log')
ax.set_yticks([10, 100, 1000,10000])
#Allows non-log formatted values to be used for ticks
ax.yaxis.set_major_formatter(plt.ScalarFormatter())
axs_MMS[0].set_ylabel('Energy (eV)')
axs_MMS[0].set_title(title_1)
for j, ax in enumerate(axs_MMS.T.flatten()):
flix_2 = np.array(O_Flux_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION,
j, :].T)
if dt_MMS==dt.timedelta(0):
fluxwin_2 = flix_2
timewin_2 = EPOCH_SURVEY_ION_2[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION]
else:
fluxwin_2=[[0 for y in range(len(flix_2))] for x_2 in range(len(flix_2))]
for i, flox in enumerate(flix_2):
fluxwin_2[i], timewin_2 = windowMean(flox, EPOCH_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION],
winsize=dt_MMS, overlap=dt.timedelta(0))
fluxwin_2[i] = np.array(fluxwin_2[i])
for x_2 in np.where(np.diff(EPOCH_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION])
>dt.timedelta(hours=1))[0]+sidx_MMS_1_SURVEY_ION:
fluxwin_2[i][bi.bisect_right(timewin_2, EPOCH_SURVEY_ION_1[x_2]):bi.bisect_right(timewin_2,
EPOCH_SURVEY_ION_1[x_1+1])]=0
fluxwin_2 = np.array(fluxwin_2)
fluxwin_2[np.where(fluxwin_2<=0)] = 0
x_2 = mdates.date2num(timewin_2)
pax_2 = ax.pcolormesh(x_2, Ion_Energy_SURVEY_ION_1, fluxwin_2, shading='turkey',cmap=cmap, vmin=1,
vmax=np.nanmax(O_Flux_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION,:,:]),
norm=LogNorm())
sax_2 = ax.twinx()
plt.setp(sax_2.get_yticklabels(), visible=False)
sax_2.tick_params(axis='y', right='off')
ax.set_xlim(MMS_SURVEY_ION_1_Start_time, MMS_SURVEY_ION_1_Finish_time)
ax.set_yscale('log')
ax.set_yticks([10, 100, 1000,10000])
#Allows non-log formatted values to be used for ticks
ax.yaxis.set_major_formatter(plt.ScalarFormatter())
axs_MMS[1].set_ylabel('Energy (eV)')
cbar_ax_1 = fig_MMS.add_axes([0.93, 0.15, 0.02, 0.7])
cb_MMS_1 = fig_MMS.colorbar(pax_1, cax=cbar_ax_1)
cb_MMS_1.set_label(r'Counts sec$^{-1}$ ster$^{-1}$ cm$^{-2}$ keV$^{-1}$')
#Sets the colorbar value range
cb_MMS_1.set_clim(1, np.nanmax(H_Flux_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION,:,:]))
#Redraws the colorbar
cb_MMS_1.draw_all()
and the image returned looks as such:
enter image description here
Consider the following example which corresponds to your code:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2,sharex=True, figsize=(4,2.4))
for j, ax in enumerate(axs.T.flatten()):
x_1 = [[0,1,2],[0,1,2],[0,1,2]]
y_1 = [[0,0,0],[1,1,1],[2,2,2]]
z_1 = [[6,5,4],[2,3,4],[6,5,4]]
pax_1 = ax.pcolormesh(x_1, y_1, z_1)
axs[0].set_ylabel('Energy (eV)')
axs[0].set_title("Title1")
for j, ax in enumerate(axs.T.flatten()):
x_2 = [[3,4,5],[3,4,5],[3,4,5]]
y_2 = [[0,0,0],[1,1,1],[2,2,2]]
z_2 = [[2,1,2],[2,1,2],[2,1,2]]
pax_2 = ax.pcolormesh(x_2, y_2, z_2)
axs[1].set_ylabel('Energy (eV)')
plt.show()
Here you plot each plot both axes.
Instead you need to plot to the different axes:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2,sharex=True, figsize=(4,2.4))
x_1 = [[0,1,2],[0,1,2],[0,1,2]]
y_1 = [[0,0,0],[1,1,1],[2,2,2]]
z_1 = [[6,5,4],[2,3,4],[6,5,4]]
pax_1 = axs[0].pcolormesh(x_1, y_1, z_1)
axs[0].set_ylabel('Energy (eV)')
axs[0].set_title("Title1")
x_2 = [[3,4,5],[3,4,5],[3,4,5]]
y_2 = [[0,0,0],[1,1,1],[2,2,2]]
z_2 = [[2,1,2],[2,1,2],[2,1,2]]
pax_2 = axs[1].pcolormesh(x_2, y_2, z_2)
axs[1].set_ylabel('Energy (eV)')
plt.show()