Specify Width and Height of Plot - graphics

I have a panel containing three plots. How can I use par to specify the width and height of the main panel so it is always at a fixed size?

You do that in the device, e.g.
x11(width=4, height=6)
and similarly for the file-based ones
pdf("/tmp/foo.pdf", width=4, height=6)
You can read the physical size via par("cin") etc but not set it.

Neither solution works in Jupyter notebooks. Here is a general approach that works in any environment:
options(repr.plot.width=6, repr.plot.height=4)
Just keep the following function handy:
set_plot_dimensions <- function(width_choice, height_choice) {
options(repr.plot.width=width_choice, repr.plot.height=height_choice)
}
EXAMPLE
Data
x <- c(37.50,46.79,48.30,46.04,43.40,39.25,38.49,49.51,40.38,36.98,40.00,38.49,37.74,47.92,44.53,44.91,44.91,40.00,41.51,47.92,36.98,43.40)
Call function with dimensions, and draw plot:
set_plot_dimensions(6, 4)
show_distribution(x, 'test vector')
set_plot_dimensions(16, 4)
show_distribution(x, 'test vector')

I usually set this at the start of my session with windows.options:
windows.options(width=10, height=10)
# plot away
plot(...)
If you need to reset to "factory settings":
dev.off()
windows.options(reset=TRUE)
# more plotting
plot(...)

Related

Change the features of Prophet plot

While using model.plot(forecast) the figure that appears has small labels and x/y values.
Is there any way to customize customize prophet's figures ?
I found there is a way to customize the options in fbprophet such as increasing size of labels or tick values.
fig = model.plot(forecast, xlabel='Date', ylabel='Value')
ax = fig.gca()
ax.set_title("Title", size=34)
ax.set_xlabel("X", size=34)
ax.set_ylabel("Y", size=34)
ax.tick_params(axis="x", labelsize=24)
ax.tick_params(axis="y", labelsize=24)
The Prophet.plot is quite limiting.
However, the fbprophet.plot.plot function allows the option of specifying the figure size. The default is (10, 6) e.g.
from fbprophet.plot import plot
plot(model, forecast, figsize=(20, 12))

Why does this code slow down? Graphics.py?

I have some code that reads a small BMP (128x96) file and puts the RGB values into a list.
I then run a nested loop and read the RGB values in reverse from the list and draw them on the screen.
It starts quite quickly and draws the first 20 lines in a second, but progressively slows down to such an extent I've never seen it finish. It only a small 128x96 image.
I feel it's the calls to the graphics.py library, buy why, or is it something else?
I'm running this on a raspberry pi, if that's of use. Python 3.4.2
If your interested in trying you can find the supporting files here https://drive.google.com/open?id=1yM9Vn1Nugnu79l1UNShamEAGd2VWF3T4
(It's the graphics.py library I'm using and the tiny bmp file, also the actual file in question called SlowDownWhy.py)
import math
import sys
from graphics import *
from PIL import Image
# Initialise Vars for Image width n height
iw=0
ih=0
img=Image.open("ani1.bmp","r") # Open Image
iw, ih = img.size # Set image width n height
ch = int(1000/ih) # Cube height set
cw = ch # Cube width set
win = GraphWin("My Window", iw*cw, ih*ch)
win.setBackground(color_rgb(128,128,128))
#Transfer Bitmap RGB vales to csv list - 'RGBlist'
pix_val = list(img.getdata())
RGBlist = [x for sets in pix_val for x in sets]
noe = (iw * ih * 3)-3
x = iw
y = ih
for vy in list(range(ih)):
y = y-1
x = iw
for vx in list(range(iw)):
x = x-1
r=RGBlist[noe]
g=RGBlist[noe+1]
b=RGBlist[noe+2]
noe=noe-3
cx=x*cw
cy=y*ch
aPoint = Rectangle(Point(cx,cy), Point(cx+cw,cy+ch))
aPoint.setFill(color_rgb(r,g,b))
aPoint.draw(win)
It should create a window no bigger than 1000 pixels in height and start drawing the picture from the bottom right to the top left, line by line. but slows down progressively.
Ignoring the invalid syntax, this is simply because of the way graphics.py is programmed: It is not designed to handle this many objects put onto the screen. (It uses tkinter in the back-end, which will slow down with 128*96=12,288 objects). For rendering images, you should either directly integrate them or use another library, as example pygame.
To integrate it into the graphics.py program, there is the Image-class, which you overwrote with the PIL.Image-library (this is the reason why you never do import *). Look here: Importing custom images into graphics.py

How to remove the white border from my heat map using python

I've generated 2000 heat maps using seaboard in python3. The problem is that it makes a white border as well. I only want to save the heat map. I want to remove these white borders because I want to train my model based on these heat maps and I think having these borders might mess-up the result. Will having these borders matter since each heat map would have this border?
The code I wrote to generate these heat maps.
for i in range(len(h1)):
ax = sns.heatmap(h1[i], yticklabels = False,xticklabels = False, cbar = False)
fig = ax.get_figure()
fig.savefig(path.join(outpath,"neutral_{0}.png".format(i)))
Actual heat map
What I want:
If you really have same size heat map pictures, you can try trimming them by one more step.
Use PIL(pillow) module to do this work.
For example:
from PIL import Image
for i in range(len(h1)):
im = Image.open("neutral_{0}.png".format(i))
im = im.crop((left, upper, right, lower)) # You have to adjust parameter here
#im = im.crop((100, 75, 300, 150)) # ↓
# you will get an image which size is (width=200, height=75)
im.save("neutral_crop_{0}.png".format(i))
The coordinates of these parameters (left, upper, right, lower) are measured from the top left corner of your input image.

changing size of ticklines

I'm trying to change the size of the tick-lines (not the label) of a 3d plot. I've tried different methods. I can easily change the size of the tick-label and its color. But changing size of the tick-lines seems impossible. I'd appreciate any thoughts on this problem.
One of the things I've tried is the following line with trying different numbers and arguments:
plt.tick_params(axis='both', which='both', length=100, labelsize=7 ,width=1, pad=0.5, right='True', color='red', tickdir='in', tick1On = 'True')
but width, length, and tickdir don't change anything.
I've also tried:
for a in (ax.w_xaxis, ax.w_yaxis, ax.w_zaxis):
for n in a.get_ticklines():
# n.set_sketch_params(1)
n.set_alpha(0.5)
n.set_mfc('red')
n.set_mew(20)
n.set_mec('red')
# n.set_drawstyle('steps-pre')
n.set_c((1,0,0,1))
n.set_ms(100)
n.set_ls(' ')
n.set_lw(1.)
n.set_solid_joinstyle('round')
n.set_markersize(900)
n.set_marker('.')
I can change all the other parameters of the tick-lines but not their color or size.
grid didn't seem to help either, since I set its visibility to False.
I'm using python 3.5.0 and matplotlib 2.1.0

pygame.transform.scale not working on a font surface

I am trying to make a function in a pygame program for python 3. The function basically makes the multiple-line process of blitting words onto the screen simpler by making it one function, MakeWord(). It has some parameters to adjust size, font, position, etc. Instead of making the font based on normal font sizes, I wanted it based on pixel sizes so I did pygame.transfom.flip() on a font surface and it did not work. Can someone find the problem please?
def MakeWord(Phrase, Font, Color, Pos, Size):
FontType = pygame.font.SysFont(Font, 400) #400 because the size will be changed anyways
FontSurf = FontType.render(Phrase, True, Color)
pygame.transform.scale(FontSurf, Size) #Does not work
FontRect = FontSurf.get_rect()
FontRect.topleft = Pos
Display.blit(FontSurf, FontRect)
return FontRect #For FontRect.collidepoint(pygame.mouse.get_pos) used later
pygame.transform.scale returns a new Surface object. Try to assign it to FontSurf:
FontSurf = pygame.transform.scale(FontSurf, Size)

Resources