I use the geopandas to visualize the data in the map after I merge the goepandas data frame with the numeric data frame. I want to render this data once the user hover over the country that this data belongs to it .right now I mange to render the coordinate as annotation where the mouse is over but I need the data
enter code genertate() : Map_Figure, Map_Graph = plt.subplots()
dataset = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world = pd.DataFrame(dataset[['name', 'geometry', 'iso_a3']])
data2 = (records['Location'].value_counts()).reset_index()
data2.columns = ['name', 'Number']
data2['Number'] = data2['Number'].apply(int)
annot = Map_Graph.annotate("", xy=(0, 0), xytext=(20, 20), textcoords="offset points",
bbox=dict(boxstyle="round", fc="w"),
arrowprops=dict(arrowstyle="->"))
datum = world.set_index('name').join(data2.set_index('name'))
World = geopandas.GeoDataFrame(datum)
#print(Map_Graph)
basePlot = World.plot(ax=Map_Graph, column='Number', linewidth=0.1, edgecolor='black', cmap='nipy_spectral',
vmin=World['Number'].min(), vmax=World['Number'].max(), legend=True)
# worldPlot= World.plot(ax=Map_Graph, color= 'white', edgecolor='black', cmap='tab10', scheme='QUANTILES', figsize=(8, 4))
locations = data2.values
box = []
for word, count in locations[:10]:
string = '%s | %s posts' % (word, human_format(count))
if len(string) >= 27:
string = string[:27] + " \n" + string[27:]
else:
string = string
box.append(string)
s = '\n'.join(box)
mapText = '%s ' % (s)
Map_Graph.text(-340, -80, mapText, size='small', wrap=True,
bbox={'boxstyle': 'round', 'facecolor': 'white', 'alpha': 0.5, 'pad': 0.8})
Map_Graph.set_axis_off()
Map_Figure.set_tight_layout(True)
Map_Figure.savefig("Report/data/Map.png")
mapCursor(Map_Graph)
Map_Figure.set_size_inches(6, 3.3)
return Map_Figure,basePlothere
and the fowling code to render the data where the mouse hover
enter code here
self.mapFigure,ax= AM.generate_Map()
#hover = HoverTool(tooltips=[('Country/region', '#country'), ('% obesity', '#per_cent_obesity')])
self.mapGraphs = FigureCanvasWxAgg(self.gPanel_map, -1, self.mapFigure)
datacursor(ax, xytext=(15, -15), bbox=dict(fc='white'), arrowprops=None, hover=True,
formatter='{x.d}: \n{y}'.format)
Related
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.
I need to copy the bar chart in the image with python.
bar chart I have to copy
What I have been able to achieve is next image.
bar chart I have achieved
And the code I have used is:
import matplotlib.pyplot as plt
ausgaben = 130386
einnahmen = 147233
profit = einnahmen-ausgaben
titles = ["Ausgaben", "Profit", "Einnahmen"]
euros = [ausgaben, profit, einnahmen]
colors = ['#6F8CA7', '#F6BC06', '#59908F']
dummysum1 = []
dummysum2 = []
for i in range(len(euros)):
dummysum1.append(euros[i]+4000)
dummysum2.append(max(euros)+15000)
if euros[1] > 0:
dummysum1[1] = euros[1]+4000
if euros[1] <= 0:
dummysum1[1] = 4000
position1 = (euros[0]+euros[2])/2
percentile = (euros[2]-euros[0])/euros[0]*100
if percentile > 0:
label0 = '+{:.1f}%'.format(percentile)
else:
label0 = '{:.1f}%'.format(percentile)
fig, ax = plt.subplots(figsize=(7, 5))
fig.set_facecolor('#D0A210')
fig.patch.set_alpha(0.2)
ax.bar(titles[0], euros[0], alpha=0.6, color=colors[0])
ax.bar(titles[1], euros[1], alpha=0.6, color=colors[1])
ax.bar(titles[2], euros[2], alpha=0.6, color=colors[2])
plt.axhline(y=euros[0], color='#BCBCBC')
plt.axhline(y=euros[2], color='#BCBCBC')
ax.set_facecolor('#D0A210')
ax.patch.set_alpha(0.02)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
ax.spines.right.set_visible(False)
ax.spines.left.set_visible(False)
ax.spines.top.set_visible(False)
ax.spines.bottom.set_visible(False)
ax.text(titles[0], dummysum1[0], '{} €'.format(euros[0]), horizontalalignment='center')
ax.text(titles[1], dummysum1[1], '{} €'.format(euros[1]), horizontalalignment='center')
ax.text(titles[2], dummysum1[2], '{} €'.format(euros[2]), horizontalalignment='center')
ax.text(2.58, position1-1000, label0)
ax.text(titles[0], dummysum2[0], titles[0], horizontalalignment='center')
ax.text(titles[1], dummysum2[1], titles[1], horizontalalignment='center')
ax.text(titles[2], dummysum2[2], titles[2], horizontalalignment='center')
plt.show()
. How can I get the yellow bar chart starting at y=130386 instead of y=0 and the yellow arrow at the right hand side?
(The first question is the most important!)
Thank you all!
For the first question, just add a value for the bottom parameter. I have also added the arrow using annotate:
import matplotlib.pyplot as plt
ausgaben = 130386
einnahmen = 147233
profit = einnahmen-ausgaben
titles = ["Ausgaben", "Profit", "Einnahmen"]
euros = [ausgaben, profit, einnahmen]
colors = ['#6F8CA7', '#F6BC06', '#59908F']
dummysum1 = []
dummysum2 = []
for i in range(len(euros)):
dummysum1.append(euros[i]+4000)
dummysum2.append(max(euros)+15000)
if euros[1] > 0:
dummysum1[1] = euros[1]+4000
if euros[1] <= 0:
dummysum1[1] = 4000
position1 = (euros[0]+euros[2])/2
percentile = (euros[2]-euros[0])/euros[0]*100
if percentile > 0:
label0 = '+{:.1f}%'.format(percentile)
else:
label0 = '{:.1f}%'.format(percentile)
fig, ax = plt.subplots(figsize=(7, 5))
fig.set_facecolor('#D0A210')
fig.patch.set_alpha(0.2)
ax.bar(titles[0], euros[0], alpha=0.6, color=colors[0])
ax.bar(titles[1], euros[1], alpha=0.6, color=colors[1], bottom=ausgaben)
ax.bar(titles[2], euros[2], alpha=0.6, color=colors[2])
plt.axhline(y=euros[0], color='#BCBCBC')
plt.axhline(y=euros[2], color='#BCBCBC')
ax.set_facecolor('#D0A210')
ax.patch.set_alpha(0.02)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
ax.spines.right.set_visible(False)
ax.spines.left.set_visible(False)
ax.spines.top.set_visible(False)
ax.spines.bottom.set_visible(False)
ax.text(titles[0], dummysum1[0], '{} €'.format(euros[0]), horizontalalignment='center')
ax.text(titles[1], dummysum1[1]+ausgaben, '{} €'.format(euros[1]), horizontalalignment='center')
ax.text(titles[2], dummysum1[2], '{} €'.format(euros[2]), horizontalalignment='center')
ax.text(2.58, position1-1000, label0)
ax.text(titles[0], dummysum2[0], titles[0], horizontalalignment='center')
ax.text(titles[1], dummysum2[1], titles[1], horizontalalignment='center')
ax.text(titles[2], dummysum2[2], titles[2], horizontalalignment='center')
ax.annotate("", xy=(2.5, ausgaben+profit*1.05), xytext=(2.5, ausgaben), arrowprops=dict(arrowstyle="->", color="orange", lw=2.0))
plt.show()
for i in range(5):
for j in range(5):
sub_image = self.image[i*8:i*8+8, j*8:j*8+8]
ax = plt.subplot(gs[4 - i, j], picker = True, label = self.iD)
c = ax.pcolormesh(sub_image, vmin=0, vmax=maxZ, cmap="viridis")
ax.axis("off")
ax.set_aspect("equal")
self.grid[4-i,j] = self.iD
self.iD += 1
fig.subplots_adjust(right=0.71, left=.285, top=0.9, bottom=0.1)
self.cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
self.cbar = fig.colorbar(c, cax=self.cbar_ax)
self.cbar.set_label("Charge (Photoelectrons)", rotation=270, size=24, labelpad=24)
self.cbar_ax.tick_params(labelsize=15)
fig.suptitle(f"Run {self.run} Event {self.ev}", fontsize=30)
fig.canvas.mpl_connect("pick_event",self.nextWindow)
self.layout.addWidget(self.static_canvas,1,1)#,1,2)
self.toolBar = NavigationToolbar(self.static_canvas, self)
self.layout.addWidget(self.toolBar,2,1,1,2)
snapshot of plot
I am trying to create the functionality for when a pick_event is processed, the value that corresponds with the colorbar is printed. When the cursor is over any part of the plot, the NavigationToolbar2QT displays the value I want on the bottom right, but I dont know how to access it otherwise.
This is how I was able to access the data. I then formatted the string to get the data I want.
data = self.toolBar._mouse_event_to_message(event.mouseevent)
I've made a function to graph economic performance, but the output is often lopsided on the y-axis.
The below graph shows the problem. The range of y values makes the chart default to the max/min as the range of the y axis.
Is there any way to force the chart to center itself on 0, or do I need derive the max and min y values within the function?
The function is below. If you'd like me to replace the variables with values to repro the chart lmk- it's a bit of a task.
def recession_comparison(key, variable, dimension):
'''
Creates the "scary chart"- proportional growth for a single area/industry. All recessions included in chart.
Parameters:
key (str or int): area-fips or industry_code
variable (str): determines what economic indicator will be used in the timeline. Must be one of ['month3_emplvl' (employment), 'avg_wkly_wage' (wages), 'qtrly_estabs_count'(firms)]
dimension (str): dimension of data to chart.
Returns:
fig (matplotlib plot)
'''
fig, ax = plt.subplots(figsize =(15, 10))
if dimension == 'area':
index = 'area_fips'
title = 'Recession Comparison, ' + area_titles[key] + " (" + str(key) + ")"
elif dimension == 'industry':
index = 'industry_code'
title = 'Recession Comparison: ' + industry_titles[key] + " (" + str(key) + ")"
for recession in recessions_int.keys():
if recession == 'full':
break
loadpath = filepath(variable = variable, dimension = dimension, charttype = 'proportional', recession = recession, filetype = 'json')
df = pd.read_json(loadpath)
df.set_index(index, inplace = True)
ax.plot(df.loc[key][1:-1]*100, label = str(recession), linewidth = 1.5, alpha = 0.8)
ax.axvline(x = 6, color = 'black', linewidth = 0.8, alpha = 0.5, ls = ':', label = 'Event Quarter')
ax.axhline(y = 0, color = 'black', linewidth = 0.8, alpha = 0.5, ls = '--', label = 'Pre-Recession baseline')
ax.set_xlabel('Quarters since start of recession')
ax.set_ylabel('Growth: ' + var_display[variable])
ax.set_title(title)
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
plt.legend()
plt.show()
return fig
edit: full code solution from DapperDuck:
def recession_comparison(key, variable, dimension):
fig, ax = plt.subplots(figsize =(15, 10))
if dimension == 'area':
index = 'area_fips'
title = 'Recession Comparison, ' + area_titles[key] + " (" + str(key) + ")"
elif dimension == 'industry':
index = 'industry_code'
title = 'Recession Comparison: ' + industry_titles[key] + " (" + str(key) + ")"
for recession in recessions_int.keys():
if recession == 'full':
break
loadpath = filepath(variable = variable, dimension = dimension, charttype = 'proportional', recession = recession, filetype = 'json')
df = pd.read_json(loadpath)
df.set_index(index, inplace = True)
ax.plot(df.loc[key][1:-1]*100, label = str(recession), linewidth = 1.5, alpha = 0.8)
ax.axvline(x = 6, color = 'black', linewidth = 0.8, alpha = 0.5, ls = ':', label = 'Event Quarter')
ax.axhline(y = 0, color = 'black', linewidth = 0.8, alpha = 0.5, ls = '--', label = 'Pre-Recession baseline')
yabs_max = abs(max(ax.get_ylim(), key=abs))
ax.set_ylim(ymin=-yabs_max, ymax=yabs_max)
ax.set_xlabel('Quarters since start of recession')
ax.set_ylabel('Growth: ' + var_display[variable])
ax.set_title(title)
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
plt.legend()
plt.show()
return fig
Corrected image:
Add the following code right after ax.axhline(y = 0, color = 'black', linewidth = 0.8, alpha = 0.5, ls = '--', label = 'Pre-Recession baseline'):
yabs_max = abs(max(ax.get_ylim(), key=abs))
ax.set_ylim(ymin=-yabs_max, ymax=yabs_max)
I have created the following doughnut chart in bokeh, using annular_wedge glyphs. I've created a tooltip, that shows percents & category properly for each wedge, but always includes an extra category : ??? and percents:1
Why is it appearing, and how can I remove it ?
Here's a link to the notebook used for generating the plot : link
# define starts/ends for wedges from percentages of a circle
percents = [0, 0.14, 0.22, 0.40, 0.83, 0.99, 1.0]
category = ['Extreme ', 'High ', 'Light ', 'Medium ', 'Not Classified', 'Very Light ']
starts = [p*2*pi for p in percents[:-1]]
ends = [p*2*pi for p in percents[1:]]
# a color for each pie piece
colors = brewer['Spectral'][len(percents)]
# create source
source = ColumnDataSource(
data=dict(
x=[0 for x in percents],
y=[0 for x in percents],
ymin = [0.5 for x in percents],
ymax = [1 for x in percents],
percents=percents,
category= category,
starts=starts,
colors=colors,
ends=ends,
)
)
# create chart
TOOLS = "hover"
p = bk.figure(title='Chronic',title_text_font ='Roboto', title_text_font_size ='14pt', title_text_alpha =0.7,
x_range=(-1.1,1.1), y_range=(-1.1,1.1), width=250, height=250, tools=TOOLS)
p.annular_wedge(x='x', y='y', inner_radius='ymin', outer_radius='ymax', direction="anticlock",
start_angle='starts', end_angle='ends', color='colors', source=source)
hover = p.select(dict(type=HoverTool))
hover.tooltips = [
('category', '#category'),
('percents','#percents')
]
# displays
bk.show(p)
I solved the problem by setting these values directly in the function call:
p.annular_wedge(x=0, y=0, inner_radius=0.5, outer_radius=1, ... )