Getting rid of empty space in tkinter embedded matplotlib graph - python-3.x

I'm embedding certain graphs into my tkinter GUI, and I noticed I have a lot of "empty space" around the figure.
There are 3 frames in the picture, first containing buttons above the figure, second containing the canvas that contains the figure, and third containing the test.
I've checked the graphs by plotting them outside of TkAgg and there is minimum empty space, so I suspect something goes wrong when I call FigureCanvasTkAgg.
Is it possible to control how much a matplotlib Figure fills Canvas widget?
code below:
canvas=FigureCanvasTkAgg(figure,master=canvasframe)
widget=canvas.get_tk_widget()
widget.pack()

Found the error. Error was in adding subplots to figure, I added with:
.add_subplots(221)
instead of
.add_subplots(121)
Really really basic error.

Related

showing PIL ImageGrab as numpy Array in Kivy Window

I am trying to capture a part of my screen with PIL.ImageGrab and then converting it to an numpy.array as you can see below.
np.array(ImageGrab.grab(bbox=(0, 0, 720, 480)))
What I can't figure out is, how to get this data into a Kivy window or a widget?
All my Widgets are in separate classes so I would like to have one Screen class with all the code necessary for this part of my Window.
I have already tried methods like this one, but I don't know how to implement it since Kivy confuses me a lot sometimes.
If my idea just isn't realizable, I guess you could save each captured frame as .jpg format and then load it into Kivy. But I imagine this is not the most efficient way.
Thanks a lot in advance! Dominik

Is it possible to update inline plots in Python (Spyder)?

Setup: Anaconda 3 (Win10 64), Spyder 4 and Python 3.7. The IPython Graphics setting is default (Inline).I'm still a new to Python but I've looked around and have not found an answer that solves my problem so far. Thanks everyone in advance.
So in this setup, whenever I create a plot using matplotlib, it appears in the plot pane of Spyder. e.g.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.DataFrame(np.random.randint(0,100,size=(100, 1)), columns=list('A'))
bp = df.boxplot(column = 'A')
creates a boxplot. Now, if I want to add a title to the plot, the code would be
bp.set_title("This Title")
This is where I'm getting some problems. If I run the entire block together
df = pd.DataFrame(np.random.randint(0,100,size=(100, 1)), columns=list('A'))
bp = df.boxplot(column = 'A')
bp.set_title("This Title")
then I get a box plot with "This Title" as the title, showing up in the plot pane,
which is what I want.
However, if I run the above code line by line in the IPython console, the 2nd line will produce a boxplot as expected, but the 3rd line will not have an effect on the image in the plot pane, so the image in the plot pane still do not have a title
Now,if i go to Tools > Preference >IPython Console > Graphics and set the graphics backend to Automatic instead of the default Inline, then when I run the code in the Console line by line, I get an image that pops up in another window, and that it does update/refreshes based on new lines entered into the console. I understand that the inline plots are supposed to be static, but I thought I saw another post where someone said that it is possible to update inline plots? So now my questions are:
Do plots only update/refresh by line codes in the IPython console if the Graphics Backend is not static like inline?
Why do I get different result when I run code blocks vs line by line?
If it is possible to update the inline plots (preferably in the plot pane of Spyder), how do you do it? I've tried various methods to redraw the plots,for example
plt.show()
plt.draw()
bp.get_figure().canvas.draw()
but none of these updates the image in the plot pane. I figured that even if I can't update the image, I should at least be able to redraw it (i.e a 2nd image appears in the plot pane with the update characteristics). But nothing I've tried worked so far. Please advise and thanks again.
(Spyder maintainer here) About your questions:
Do plots only update/refresh by line codes in the IPython console if the Graphics Backend is not static like inline?
Correct.
Why do I get different result when I run code blocks vs line by line?
Because when you run code cells (which is what I think you mean by "code blocks") your plot is shown at the end of that code and hence it takes all modifications you've done to it in intermediate lines.
If it is possible to update the inline plots (preferably in the plot pane of Spyder), how do you do it?
No, it's not possible. As you correctly mentioned above, inline plots are static images, so they can't be modified.

Italicize and color legend manually for different graphs using same labels and colors

I am trying to create multiple graphs that share the same legend.
I have found many ways to combine multiple graphs and it seems that ggarrange has the ability to create one shared legend for all that is supposed to be unique.
However I am having some problems when graphing since a few of the graphs do not have the same phyla (what defines the legend colors) present, but I would like them all to be the same colors throughout all of the graphs so the combined legend will have the correct colors.
For just one graph I would assign a color to the label manually like below
labs<-c("Arthropoda"="#FF66CC"
,"Cercozoa"="#FF6000")
and plot with the addition of scale_fill_manual(values=labs) and this seems to work
then I modified it so I could have portions of them italicized.
labsPhylum <-c('expression(paste(italic("Arthropoda")))'="#CC0000"
,'expression(paste(italic("Cercozoa")))'= "#FF6000"
,'expression (paste("unknown", ~italic("Eukaryota")))'= "#990000")`
However when I create a plot using ggplot and scale_color_manual() using the labsPhylum that I think should be italicized and colored I plot an empty graph with this warning so there is something vital I am not understanding here.
ggplot(data=sigtab_dil, aes(x=Species, y=log2FoldChange, color=Phylum))+
geom_point(size=2) +
scale_color_manual(values=labsPhylum)
Warning message:
Removed 9 rows containing missing values (geom_point).
Could someone please help me figure out where I am going wrong?
Thank you
Answered my own question
I realized I had to make separate vectors for breaks, labels, and values rather than combining them.
In short
colsPhylum <-c("Arthropoda"="#CC0000"
,"Cercozoa"= "#FF6000"
,"Chlorophyta"= "#CC9900"
labsPhylum <-c(expression(paste(italic("Arthropoda")))
,expression(paste(italic("Cercozoa")))
,expression(paste(italic("Chlorophyta ")))
breaksPhylum <-c("Arthropoda", "Cercozoa","Chlorophyta", "Choanozoa"
,"Ciliophora"
,"Cryptista"

Tkinter enlarging graphs drawn on canvas without changing the canvas size

I am currently creating a GUI which plots data points from a textfile. I would like to enlarge the graph. Would like to ask if it is possible because everything I tried seems to yield no result. Below I attached the image of the graphs which I wish to enlarge. Thanks.

Getting rid of border on tkinter label

In my most recent posting, the code does exactly what I need it do...is there a way to get rid of the border/lighter gray area around the label? I want to only see the text.
Splitting tkinter canvas and frame apart
I have tried changing the label code to
v = Label(s, bg='gray',fg='black',borderwidth=0,highlightthickness=0,highlightcolor='gray',relief='sunken',anchor='w')
This still leaves the side borders that can be plainly seen in the code at the above link. I just inserted relief cause I saw someone else's post using it and thought I would give it a try versus leaving the relief flat as set by default. Made no difference.
How do I get rid of the light gray sides that are appearing on the Label. I know this is about to really become important probably as soon as this evening or tomorrow morning.

Resources