Add new sheet with graph - python

newby question.
I would like to add a new sheet to an existing wb that I've created with xlwings.
It seems that when I try to add e write the 2nd sheet the 1st one going to be overwritten.
Here the code :
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns # library for visualization
sns.set() # this command sets the seaborn chart style as the default
import xlwings as xw
from datetime import datetime
df=pd.read_excel('aspire_1909.xls')
df2=df.drop([0,2])
new_header = df2.iloc[1]
df2 = df2[2:]
df2.columns = new_header
df2=df2.set_index('User')
wb = xw.Book()
sht = wb.sheets[0]
sht.name = "Aspire Manager Graph"
sht.range('R1').value = df3
started=len(df3.loc[df3['Manager Review'] == 'Started'])
complete = len(df3.loc[df3['Manager Review'] == 'Complete'])
complete_reopened = len(df3.loc[df3['Manager Review'] == 'Complete (Reopened)'])
not_started = len(df3.loc[df3['Manager Review'] == 'Not Started'])
past_due = len(df3.loc[df3['Manager Review'] == 'Past Due'])
def insert_heading(rng,text):
rng.value = text
rng.font.bold = True
rng.font.size = 24
rng.font.color = (0,0,139)
insert_heading(sht.range("A2"),f"ASPIRE YEAR END REVIEW - MANAGER STATUS del {datetime.today().strftime('%d-%m-%Y')}")
data = {'Not Started':not_started, 'Started':started, 'Completed':complete,'Reopened' : complete_reopened,'Past Due ' : past_due }
status = list(data.keys())
values = list(data.values())
x_labels = list(a + ' ' + str(b) for (a, b) in zip(status, values))
fig = plt.figure(figsize = (10, 5))
# creating the bar plot
fig, ax = plt.subplots(figsize=(15, 15))
bars = ax.bar(status, values, color =['red','blue','green','yellow','violet'],
width = 0.4)
ax.bar_label(bars, fmt="%d", fontsize=26, rotation=0, padding=3)
plt.bar(status, values, color =['red','blue','green','yellow','violet'],
width = 0.4)
plt.xticks(status, x_labels)
plt.xticks(rotation = 45, fontsize = 13)
plt.xlabel("Year End Review Completion Status")
plt.ylabel("No Users",rotation=45,fontsize = 13)
plt.title("Aspire Mgr Year End Review")
plt.show()
sht.pictures.add(fig,
name = "Aspire Mgr Status Graph",
update = True,
left =sht.range("A4").left,
top = sht.range("A4").top,
height= 500,
width= 700)
sht1 = wb.sheets[0]
wb.sheets.add('Aspire Employees Graph')
sht1.range('R1').value = df2
started=len(df2.loc[df2['Aspire year-end reflection (FY22)'] == 'Started'])
complete = len(df2.loc[df2['Aspire year-end reflection (FY22)'] == 'Complete'])
complete_reopened = len(df2.loc[df2['Aspire year-end reflection (FY22)'] == 'Complete (Reopened)'])
not_started = len(df2.loc[df2['Aspire year-end reflection (FY22)'] == 'Not Started'])
past_due = len(df2.loc[df2['Aspire year-end reflection (FY22)'] == 'Past Due'])
def insert_heading(rng,text):
rng.value = text
rng.font.bold = True
rng.font.size = 24
rng.font.color = (0,0,139)
insert_heading(sht1.range("A2"),f"ASPIRE YEAR END REVIEW EMPLOYEE STATUS del {datetime.today().strftime('%d-%m-%Y')}")
data = {'Not Started':not_started, 'Started':started, 'Completed':complete,'Reopened' : complete_reopened,'Past Due ' : past_due }
status = list(data.keys())
values = list(data.values())
x_labels = list(a + ' ' + str(b) for (a, b) in zip(status, values))
fig = plt.figure(figsize = (10, 5))
# creating the bar plot
fig, ax = plt.subplots(figsize=(15, 15))
bars = ax.bar(status, values, color =['red','blue','green','yellow','violet'],
width = 0.4)
ax.bar_label(bars, fmt="%d", fontsize=26, rotation=0, padding=3)
plt.bar(status, values, color =['red','blue','green','yellow','violet'],
width = 0.4)
plt.xticks(status, x_labels)
plt.xticks(rotation = 45, fontsize = 13)
plt.xlabel("Year End Review Completion Status")
plt.ylabel("Nb. Users",rotation=45,fontsize = 13)
plt.title("Aspire Employee Year End Review")
plt.show()
sht1.pictures.add(fig,
name = "Aspire Employee Status Graph",
update = True,
left =sht.range("A4").left,
top = sht.range("A4").top,
height= 500,
width= 700)
Could someone would be able to help me get what Is wrong ? ( I know ,almost everything :-) )
Thanks a lot in advance

In the second half of the code you have:
sht1 = wb.sheets[0]
wb.sheets.add('Aspire Employees Graph')
sht1.range('R1').value = df2
What wb.sheets[0] is returning is the very first sheet of the workbook. Towards the beginning you have the first section, which is:
sht = wb.sheets[0]
sht.name = "Aspire Manager Graph"
sht.range('R1').value = df3
As you use wb.sheets[0] both times, but haven't inserted a sheet at the beginning, you are just referring to the same sheet. The addition of the new sheet is correct, but you haven't set that as variable sht1.
Instead, for the second section, you could re-write to the following, combining the two lines into one so that the variable is the correct sheet:
sht1 = wb.sheets.add('Aspire Employees Graph')
sht1.range('R1').value = df2
Edit
To change the colour of the sheet tab:
sht1.api.Tab.ColorIndex = 3
The full list of colours can be found in the VBA ColorIndex documentation.
For more specific colours, see the answers to this question.

Related

Barplot subplot legend python

Hello everyone how can i make legend for 3 different bar color that code with subplot?
Data frame:
This is my code:
fig,axs = plt.subplots(2,3, figsize=(30,20))
axs[0,1].bar(x = df_7_churn_tenure['Kategori'],height = df_7_churn_tenure['Jumlah Churn'],color = ['lightsalmon','maroon','darkorange'])
axs[0,1].legend(['Low Tenure = x<24','Medium Tenure = 24<= x <=48','High Tenure = x >=48'],loc='best',fontsize=12)
plt.show()
And the result for barplot legend only shows 1 label like this:
Is there any solution to shows all of my legend?
Try this:
fig,axs = plt.subplots(2,3, figsize=(30,20))
axs[0,1].bar(x = df_7_churn_tenure['Kategori'],height = df_7_churn_tenure['Jumlah Churn'],color ['lightsalmon','maroon','darkorange'])
axs = axs[0,1]
lns1 = axs.plot('-',label = 'Low Tenure = x<24')
lns2 = axs.plot('-',label = 'Medium Tenure = 24<= x <=48')
lns3 = axs.plot('-',label = 'High Tenure = x >=48')
# added these three lines
lns = lns1+lns2+lns3
labs = [l.get_label() for l in lns]
axs.legend(lns, labs,loc=0)
plt.show()

Creating a legend, and printing a graph with python

I am wanting to add a legend to the graph below and download it as a pdf. The code I have for the graph is below.
%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
mta = pd.read_csv('../project/all_mta_data_cleanded.csv') # mta data cleanded into similare catagories
cata = pd.read_csv('../project/mta_catagories_breakdown.csv') #document combining all of the catagories
weather = pd.read_csv('../project/New York Tempeture Data.csv')
mta['Total Lost Items'] = mta['Total Lost Items'] = (mta['Accessories']+ mta['Books'] + mta['Bags'] + mta['Cellphones'] + mta['Clothing'] + mta['Money'] + mta['Eletronics'] + mta['Entrainment'] + mta['Glasses'] + mta['Shoes'] + mta['Household Items'] + mta['Indentification'] + mta['Jewlery'] + mta['Keys'] + mta['Medical Equipment'] + mta['Miscellaneous'] + mta['Instruments'] + mta['NYCT Equipment'] + mta['Sports Equipment'] + mta['Tickets'] + mta['Tools'] + mta['Toys'] + mta['Wallets/Purses'])
clear = mta.iloc[[13, 40,68,92,115,138,157,176,200,222,241,245,246,250],:] #selected the last pull of each month
compl = pd.merge(clear,weather, left_on='Date',right_on='Time',how='outer').drop(columns=['Time'])
fig, ax1 = plt.subplots()
ax1.plot(compl['Date'], compl['Temp'] ,color = 'red',marker='o')
ax2= ax1.twinx()
ax2.plot(compl['Date'], compl['Total Lost Items'],color= 'purple',marker='^')
ax1.set_ylabel('Tempeture in Fahrenheit', )
for tick in ax1.get_xticklabels():
tick.set_rotation(90)
ax2.set_ylabel('Number Of Items Lost')
ax1.set_title('Average Weather In New Your City vs Total Items Lost Each Month')
plt.set_legend()
plt.figure.savefig('Project Figure.pdf')```
to add a legend to your graph you have to specify the field "label" in plt.plot(), use plt.legend() and plt.show()
fig, ax1 = plt.subplots()
l1 = ax1.plot(compl['Date'], compl['Temp'] ,color = 'red',marker='o', label = 'label_1')
ax2= ax1.twinx()
l2 = ax2.plot(compl['Date'], compl['Total Lost Items'],color= 'purple',marker='^', label = 'label_2')
ax1.set_ylabel('Temperature in Fahrenheit')
for tick in ax1.get_xticklabels():
tick.set_rotation(90)
ax2.set_ylabel('Number Of Items Lost')
ax1.set_title('Average Weather In New Your City vs Total Items Lost Each Month')
plt.legend([l1,l2],['lab1', 'lab2'])
plt.show()
plt.savefig('Project Figure.pdf')

Why are the indicators on my chart delayed by at least 1 day, making them not flush on the blue line? Is it because the time frame is too wide?

Why are the up triangles, when the program is supposed to buy, not on the line when it crosses under, or in the other scenario, the down triangle, when the program is supposed to sell, not on the line when it crosses on top? The blue line is the price and the red line is the EMA, tracking the price.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import requests
plt.style.use("fivethirtyeight")
df = pd.read_csv("TSLA.csv")
df = df.set_index(pd.DatetimeIndex(df["Date"].values))
ShortEMA = df.Close.ewm(span=5, adjust = False).mean()
MiddleEMA = df.Close.ewm(span = 21, adjust = False).mean()
LongEMA = df.Close.ewm(span = 53, adjust = False).mean()
df['Short'] = ShortEMA
df['Middle'] = MiddleEMA
df['Long'] = LongEMA
def MyStrat(data):
bought_list = []
sold_list = []
In = False
Out = True
for i in range(0, len(data)):
if data["Close"][i] > data["Short"][i] and In == False and Out == True:
bought_list.append(data["Close"][i])
sold_list.append(np.nan)
In = True
Out = False
elif data["Close"][i] < data["Short"][i] and In == True and Out == False:
sold_list.append(data["Close"][i])
bought_list.append(np.nan)
In = False
Out = True
else:
bought_list.append(np.nan)
sold_list.append(np.nan)
return(bought_list,sold_list)
df["Bought"] = MyStrat(df)[0]
df["Sold"] = MyStrat(df)[1]
print(df)
plt.figure(figsize=(16, 5))
plt.title('Buy and Sell', fontsize = 18)
plt.plot(df['Close'], label = 'Close Price', color = 'blue', alpha = 0.35)
plt.plot(ShortEMA, label = 'Short', color = 'red', alpha = 0.35)
plt.scatter(df.index, df["Bought"], color = "purple", marker = "^", alpha = 1)
plt.scatter(df.index, df["Sold"], color = "blue", marker = "v", alpha = 1)
plt.xlabel("Date", fontsize = 18)
plt.ylabel("Close", fontsize = 18)
plt.show()
You can use this data for reference:
Date,Open,High,Low,Close,Adj Close,Volume
2022-01-06,1077.000000,1088.000000,1020.500000,1064.699951,1064.699951,30112200
2022-01-07,1080.369995,1080.930054,1010.000000,1026.959961,1026.959961,28054900
2022-01-10,1000.000000,1059.099976,980.000000,1058.119995,1058.119995,30605000
2022-01-11,1053.670044,1075.849976,1038.819946,1064.400024,1064.400024,22021100
2022-01-12,1078.849976,1114.839966,1072.589966,1106.219971,1106.219971,27913000
2022-01-13,1109.069946,1115.599976,1026.540039,1031.560059,1031.560059,32403300
2022-01-14,1019.880005,1052.000000,1013.380005,1049.609985,1049.609985,24308100
2022-01-18,1026.609985,1070.790039,1016.059998,1030.510010,1030.510010,22247800
2022-01-19,1041.709961,1054.670044,995.000000,995.650024,995.650024,25147500
2022-01-20,1009.729980,1041.660034,994.000000,996.270020,996.270020,23496200
2022-01-21,996.340027,1004.549988,940.500000,943.900024,943.900024,34472000
2022-01-24,904.760010,933.510010,851.469971,930.000000,930.000000,50521900
2022-01-25,914.200012,951.260010,903.210022,918.400024,918.400024,28865300
2022-01-26,952.429993,987.690002,906.000000,937.409973,937.409973,34955800
2022-01-27,933.359985,935.390015,829.000000,829.099976,829.099976,49036500
2022-01-28,831.559998,857.500000,792.010010,846.349976,846.349976,44929700
2022-01-31,872.710022,937.989990,862.049988,936.719971,936.719971,34812000
2022-02-01,935.210022,943.700012,905.000000,931.250000,931.250000,24379400
2022-02-02,928.179993,931.500000,889.409973,905.659973,905.659973,22264300
2022-02-03,882.000000,937.000000,880.520020,891.140015,891.140015,26285200
2022-02-04,897.219971,936.500000,881.169983,923.320007,923.320007,24541800
2022-02-07,923.789978,947.770020,902.710022,907.340027,907.340027,20331500
2022-02-08,905.530029,926.289978,894.799988,922.000000,922.000000,16909700
2022-02-09,935.000000,946.270020,920.000000,932.000000,932.000000,17419800
2022-02-10,908.369995,943.809998,896.700012,904.549988,904.549988,22042300
2022-02-11,909.630005,915.960022,850.700012,860.000000,860.000000,26548600
2022-02-14,861.570007,898.880005,853.150024,875.760010,875.760010,22585500
2022-02-15,900.000000,923.000000,893.380005,922.429993,922.429993,19095400
2022-02-16,914.049988,926.429993,901.210022,923.390015,923.390015,17098100
2022-02-17,913.260010,918.500000,874.099976,876.349976,876.349976,18392800
2022-02-18,886.000000,886.869995,837.609985,856.979980,856.979980,22833900
2022-02-22,834.130005,856.729980,801.099976,821.530029,821.530029,27762700
2022-02-23,830.429993,835.299988,760.559998,764.039978,764.039978,31752300
2022-02-24,700.390015,802.479980,700.000000,800.770020,800.770020,45107400
2022-02-25,809.229980,819.500000,782.400024,809.869995,809.869995,25355900
2022-02-28,815.010010,876.859985,814.710022,870.429993,870.429993,33002300
2022-03-01,869.679993,889.880005,853.780029,864.369995,864.369995,24922300
2022-03-02,872.130005,886.479980,844.270020,879.890015,879.890015,24881100
2022-03-03,878.770020,886.440002,832.599976,839.289978,839.289978,20541200
2022-03-04,849.099976,855.650024,825.159973,838.289978,838.289978,22333200
2022-03-07,856.299988,866.140015,804.570007,804.580017,804.580017,24164700
2022-03-08,795.530029,849.989990,782.169983,824.400024,824.400024,26799700
2022-03-09,839.479980,860.559998,832.010010,858.969971,858.969971,19728000
2022-03-10,851.450012,854.450012,810.359985,838.299988,838.299988,19549500
2022-03-11,840.200012,843.799988,793.770020,795.349976,795.349976,22272800
2022-03-14,780.609985,800.700012,756.039978,766.369995,766.369995,23717400
2022-03-15,775.270020,805.570007,756.570007,801.890015,801.890015,22280400
2022-03-16,809.000000,842.000000,802.260010,840.229980,840.229980,28009600
2022-03-17,830.989990,875.000000,825.719971,871.599976,871.599976,22194300
2022-03-18,874.489990,907.849976,867.390015,905.390015,905.390015,33408500
2022-03-21,914.979980,942.849976,907.090027,921.159973,921.159973,27327200
2022-03-22,930.000000,997.859985,921.750000,993.979980,993.979980,35289500
2022-03-23,979.940002,1040.699951,976.400024,999.109985,999.109985,40225400
2022-03-24,1009.729980,1024.489990,988.799988,1013.919983,1013.919983,22973600
2022-03-25,1008.000000,1021.799988,997.320007,1010.640015,1010.640015,20642900
2022-03-28,1065.099976,1097.880005,1053.599976,1091.839966,1091.839966,34168700
2022-03-29,1107.989990,1114.770020,1073.109985,1099.569946,1099.569946,24538300
2022-03-30,1091.170044,1113.949951,1084.000000,1093.989990,1093.989990,19955000
2022-03-31,1094.569946,1103.140015,1076.640015,1077.599976,1077.599976,16265600
2022-03-31,1094.569946,1103.139893,1076.640991,1077.599976,1077.599976,16330919
The problem with this is that the point of intersection occurs between days, not on a specific day. As the data is not continuous, but rather just one point per business day, it is not possible to put the arrow on the intersection itself. I have enlarged a portion of the graph here so you can see what I mean. The change occurs between the 9th and 10th. The data is only on the 9th or the 10th, so the arrow is plotted, and the buy occurs, on the 10th.
The buy/sell is on the next possible day, causing the mis-alignment of the arrows.

How to change the Y scale for my plot graph in matplotlib.pyplot

with open('data/covid.csv', encoding="utf8") as file_obj:
file_data = csv.DictReader(file_obj, skipinitialspace = True)
#storing info of dose 1 in dictionary
dicti = {}
for row in file_data:
key = row['State/UTs']
if key in dicti:
dicti[key] = row['Dose 1']
else:
key = row['State/UTs']
dicti[key] = row['Dose 1']
print(dicti)
valuesL = list(dicti.values())
for i in range(0, len(valuesL)):
valuesL[i] = int(valuesL[i])
plt.figure(figsize=(18,12))
ax = plt.bar(list(dicti.keys()),valuesL, color = "green")
plt.xlabel("State/uts", fontsize = 30)
plt.ylabel("Number of people who have taken dose 1", fontsize = 30)
plt.title('States VS people who have taken dose 1', fontsize = 30)
plt.xticks(rotation=80)
for patch in ax.patches:
width = patch.get_width()
height = patch.get_height()
x = patch.get_x()
y = patch.get_y()
plt.text(x , y + height + 4, '{}'.format(height))
plt.show()
this is my code the graph shows the y axis to be ranging from 0.5 to 1, I want it to change according to the values of dictionary that range from 10000 to 10000000
Seems like your looking for set_ylim.
bottom = 10000
top = 10000000
ax.set_ylim(bottom, top)
or
plt.ylim(bottom, top)

how to add range to the secondary_y axis in plotly?

I want to create a graph where I want to add range to the secondary y-axis. The graph which I have right now is like this.
The code for this graph is like this:
import plotly.offline as pyo
import plotly.graph_objs as go
from plotly import tools
from plotly.subplots import make_subplots
import pandas as pd
import xlwings as xw
import logging
fileName = 'solar data.xlsx'
app = xw.App(visible=False)
try:
wb = app.books.open(fileName)
sheet = wb.sheets[4]
lastCell = sheet.range('A1').end('down').last_cell.row
solarOne = sheet.range('B2:B'+ str(lastCell)).value
bessOne = sheet.range('D2:D'+ str(lastCell)).value
socOne = sheet.range('E2:E'+ str(lastCell)).value
solarOne_Plus_bessOne = [i+j for i,j in zip(solarOne,bessOne)]
# solarTwo = sheet.range('F2:F' + str(lastCell)).value
# bessTwo = sheet.range('H2:H' + str(lastCell)).value
# socTwo = sheet.range('I2:I' + str(lastCell)).value
# solarTwo_Plus_bessTwo = [i + j for i, j in zip(solarTwo, bessTwo)]
except Exception as e:
logging.exception("Something awful happened!")
print(e)
finally:
app.quit()
app.kill()
fig = go.Figure()
projectTime = pd.date_range("2020-10-01 00:00:00", "2020-10-01 23:59:00", freq="1T")
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(go.Scatter(x = projectTime, y = solarOne, name = 'Solar', fill='tozeroy', line=dict(width=0.5, color='rgb(255,167,0)')), secondary_y=False)
fig.add_trace(go.Scatter(x = projectTime, y = bessOne, name = 'Storage', fill='tozeroy', line=dict(width=0.5, color='rgb(43, 201, 34)')), secondary_y=False)
fig.add_trace(go.Scatter(x = projectTime, y = socOne, name = 'SoC', fill='tozeroy', line=dict(width=0.5, color='rgb(250, 67, 67)')), secondary_y=True)
fig.add_trace(go.Scatter(x = projectTime, y = solarOne_Plus_bessOne, name = 'Solar + BESS', fill='tozeroy',), secondary_y=False)
# Add figure title
fig.update_layout(
title_text="Solar with 0.5MW 0.5Hour storage"
)
# Set x-axis title
fig.update_xaxes(title_text="Time")
# Set y-axes titles
fig.update_yaxes(title_text="Dispatch(MW) and SoC(MWh)")
pyo.plot(fig, filename='Solar with 0.5MW 0.5Hour storage example 1.html')
I have tried to add layout_yaxis_range=[-0.6, 0.7] in secondary y-axis but it throws an error
TypeError: add_trace() got an unexpected keyword argument 'layout_yaxis_range'
I want this secondary y-axis with the same x-axis. Can anyone please help?
This works for me in similar cases:
fig.update_yaxes(range=[-0.6, 0.7], secondary_y=True)

Categories