I have a figure that I would like to resize and afterwards print as a PDF.
Using something like
set(hFig, 'PaperUnits', 'centimeters')
set(hFig, 'PaperSize', [x_B x_H]);
works as long as I do not resize the figure too drastically. If I reduce the height then at some points the xlabel moves out of the figure. I have searched a lot but only found an solution to manually resize the underlying axes-object
scalefactor = 0.96;
movefactor = 0.82;
hAx = get(gcf,'CurrentAxes');
g = get(hAx,'Position');
% 1=left, 2=bottom, 3=width, 4=height
g(2) = g(2) + (1-movefactor)/2*g(4);
g(4) = scalefactor*g(4);
set(hAx,'Position',g);
I do not like this approach since I have to manually adjust the two factors.
Before printing I set the 'interpreter' to 'latex' of all text-objects (if that is of concern).
Printing is achieved using
print(hFig, '-dpdf', '-loose', 'test.pdf');
I hoped to loosen the bounding box by using '-loose'. Any help is highly appreciated!
edit:
It seems that really the interpreter (none, tex, latex) plays a role in this. I got inspired by this post here (http://stackoverflow.com/questions/5150802/how-to-save-plot-into-pdf-without-large-margin-around) and came up with this solution:
tightInset = get(gca, 'TightInset');
position(1) = tightInset(1);
position(3) = 1 - tightInset(1) - tightInset(3);
if strcmpi(x_Interpreter,'latex')
position(2) = tightInset(2)+ 1*tightInset(4);
position(4) = 1 - tightInset(2) - 2*tightInset(4);
else
position(2) = tightInset(2)+ 0*tightInset(4);
position(4) = 1 - tightInset(2) - 1*tightInset(4);
end
set(gca, 'Position', position);
This may not solve your problem completely (it may just help clean up your code), but I found the fig code in the file exchange to be helpful: it lets you easily set the exact size of figures without bordering white space.
Related
I'm looking to set up keybindings to increase/decrease gaps and margins in Qtile similar to what the following does in i3-gaps:
bindsym $mod+equal gaps inner current plus 5
bindsym $mod+minus gaps inner current minus 5
bindsym $mod+Shift+equal gaps outer current plus 5
bindsym $mod+Shift+minus gaps outer current minus 5
I can somewhat get the equivalent of outer-gaps to work with the following code:
def increase_gap(qtile):
qtile.screens[0].top.size = screens[0].top.size+5
qtile.screens[0].right.size = screens[0].top.size+5
qtile.screens[0].left.size = screens[0].top.size+5
#margs = screens[0].bottom.margin
screens[0].bottom.margin[0] = screens[0].bottom.margin[0]+5
screens[0].bottom.size=20
qtile.screens[0].cmd_resize()
def decrease_gap(qtile):
qtile.screens[0].top.size = max(screens[0].top.size-5, 0)
qtile.screens[0].right.size = max(screens[0].top.size-5, 0)
qtile.screens[0].left.size = max(screens[0].top.size-5, 0)
#margs = screens[0].bottom.margin
screens[0].bottom.margin[0] = max(screens[0].bottom.margin[0]-5, 0)
screens[0].bottom.size=20
qtile.screens[0].cmd_resize()
Key([mod, "shift"], "equal", lazy.function(increase_gap), desc="Increase gap"),
Key([mod, "shift"], "minus", lazy.function(decrease_gap), desc="Decrease gap"),
I'm not sure this is the right way to do things, though. I'm not sure if this is supposed to be manipulated this way. I'm not really sure that the cmd_resize() function is what I should be using, however from trial and error it's what I've found to work. I'm not sure why the screen[0].bottom.size=20 (20 is the size of my bottom bar. I know I shouldn't hardcode, but I'm trying to produce a proof of concept before I clean the code) is needed, but the bar starts floating if I don't have that there. Finally, increasing and decreasing the gap gets close, but not quite to the original configuration. The gaps look slightly different than the original. So, I'm not sure this is the right way to accomplish this, and I could use the advice.
Secondly, though that gets close to accomplishing what I want on the outer gaps, I have not been able to make any headway to getting the inner gaps to work. I initially tried changing the margin parameter of a layout, and when that didn't work I tried simply initializing a new layout and replacing the old one as posted below, but neither approach worked.
def column_increase_margin(qtile):
current_margin = current_margin + 5
layouts[0] = layout.Columns(border_focus_stack='#d75f5f', margin=current_margin, border_width=0)
screens[0].bottom.size=20
qtile.screens[0].cmd_resize()
I've tried going through the code here, but it's a big project and I'm struggling to make heads or tails from it.
Any advice would be appreciated.
If I understand what you want, you can add the following to the Layout class in libqtile/layout/base.py:
def cmd_increase_margin(self):
self.margin += 10
self.group.layout_all()
def cmd_decrease_margin(self):
new_margin = self.margin - 10
if new_margin < 0:
new_margin = 0
self.margin = new_margin
self.group.layout_all()
You can then add some keys in your config.py to increase and decrease margins. e.g.
KeyChord([mod], "m", [
Key([], "Up", lazy.layout.increase_margin()),
Key([], "Down", lazy.layout.decrease_margin())
],
mode="Margins"
),
I'm new to qtile, so maybe there is something wrong with the above approach, but it seems to work.
I was wondering today about how finding a specific value on a plot and drawing the right line that goes with. I used to do that on an old chart library, and I was wondering that perhaps this functionnality exist but I don't know how to find it.
The result should look like this: https://miro.medium.com/max/1070/1*Ckhi9soE9Lx2lIf9tPVLMQ.png
To provide some context, I'm doing a PCA over my data, and I would like to point out some thresholds at 97.5, 99 and 99.5% of explained cumuled variance.
Have a great day!
EDIT:
See Answer
As solved by ImportanceOfBeingErnest, here is the code:
whole_pca = PCA().fit(np.array(inputs['Scale'].tolist()))
cumul = np.cumsum(np.round(whole_pca.explained_variance_ratio_, decimals=3)*100)
over_95 = np.argmax(cumul>95)
over_99 = np.argmax(cumul>99)
over_995 = np.argmax(cumul>99.5)
plt.plot(cumul)
plt.plot([0,over_95,over_95], [95,95,0])
plt.plot([0,over_99,over_99], [99,99,0])
plt.plot([0,over_995,over_995], [99.5,99.5,0])
plt.xlim(left=0)
plt.ylim(bottom=80)
plt.ylabel('% Variance Explained')
plt.xlabel('# of Features')
plt.title('PCA Analysis')
Result in:
Thank you!
I use Selenium to automate some GUI tests using the firefox web driver.
It occurred to me that it would be also make sense to create screenshots during the run of the tests to use those in the manual.
The screens of my application are relatively static - no timestamps visible and so on. So my expectation would be, if I create a screenshot from lets say the start page, navigate later to the start page again, the screenshots should be identical. Also if I run the test twice, the screenshot of the start page should be identical between both runs.
I save the screenshots as PNG, I even process the screenshots (save without date) before saving them, so that the files should be really identical.
Nevertheless, if I compare the pictures with each other (e.g. subtract them from each other), there are minor differences between them (not visible to the naked eye), some faint lines at the border of tables, or around fonts.
My question:
1) Why are there differences at all?
2) What could be the easiest way to ensure that the screenshots are identical? (what kind of post processing could I do)
PS: I also tried to change the renderer from skia to windows to cairo, but although the differences are slightly different, it still doesn't solve the problem.
How do you save the images ?
In ruby I was using something like this ?
def take_screenshot(image_id)
scenario = Zpg::HooksHelper::FeatureHelper.scenario_name
Dir.mkdir('output', 0o777) unless File.directory?('output')
Dir.mkdir('output/screenshots', 0o777) unless File.directory?('output/screenshots')
screenshot = "./output/screenshots/#{image_id}#{scenario.tr(' ', '_').gsub(/[^0-9A-Za-z_]/, '')}.png"
if page.driver.browser.respond_to?(:save_screenshot)
page.driver.browser.save_screenshot(screenshot)
else
save_screenshot(screenshot)
end
FileUtils.chmod(0o777, screenshot)
end
And I was checking the image diff
# return[hash]
def image_diff(imageid_1 = nil, _imageid_2 = nil)
scenario = Zpg::HooksHelper::FeatureHelper.scenario_name
if imageid_1.class != Integer
image_1 = "./features/support/data/images/#{Zpg.brand}/#{imageid_1}.png"
image_2 = "./output/screenshots/#{_imageid_2}#{scenario.tr(' ', '_').gsub(/[^0-9A-Za-z_]/, '')}.png"
else
image_1 = "./output/screenshots/#{imageid_1}#{scenario.tr(' ', '_').gsub(/[^0-9A-Za-z_]/, '')}.png"
image_2 = "./output/screenshots/#{_imageid_2}#{scenario.tr(' ', '_').gsub(/[^0-9A-Za-z_]/, '')}.png"
end
images = [
ChunkyPNG::Image.from_file(image_1),
ChunkyPNG::Image.from_file(image_2)
]
diff = []
images.first.height.times do |y|
images.first.row(y).each_with_index do |pixel, x|
diff << [x, y] unless pixel == images.last[x, y]
end
end
puts "pixels (total): #{images.first.pixels.length}"
puts "pixels changed: #{diff.length}"
puts "pixels changed (%): #{(diff.length.to_f / images.first.pixels.length) * 100}%"
# init empty hash
diff_hash = {}
# return pixels changed number
diff_hash[:pixels_changed] = diff.length
# return pixels changed percentage
diff_hash[:pixels_changed_percentage] = (diff.length.to_f / images.first.pixels.length) * 100
# return diff hash
diff_hash
end
you can get different results if the browser DOM is not 100% loaded . I would try to have a threshold in which I would expect my image to be .
There is a very good project here in .Net https://www.codeproject.com/Articles/374386/Simple-image-comparison-in-NET which you can convert in any language you want.
I want to create half a pie chart to use in an existing builder script and I'm not sure how to go about it. (its actually to implement a speedometer)
Does anyone have any suggestions?
I realise I'll need to dip into some python code.
This old posting in Google Groups https://groups.google.com/forum/#!topic/psychopy-users/JcnS7ZtuVlM gives some sample code utilising pylab but they couldn't get it to work when it was dropped into Builder.
Thanks,
I've worked out how to do this now so here's my method for the record.
# create 1st sector - use the **ori** parameter to control what bearing it
# starts on (0 is vertical)
rad1 = visual.RadialStim( win=win, name='rad1', color=[1,-1,-1],
angularCycles = 0, radialCycles = 0, radialPhase = 0.5, colorSpace = 'rgb',
ori= -90.0, pos=(0.5, -0.3), size=(0.3,0.3), visibleWedge=(0.0, 135.0) )
rad1.draw()
#now draw another sector next to it by using **ori** again to line them up
rad2 = visual.RadialStim( win=win, name='rad1', color=[-1,1,-1],
angularCycles = 0, radialCycles = 0, radialPhase = 0.5, colorSpace = 'rgb',
ori= 45.0, pos=(0.5, -0.3), size=(0.3,0.3), visibleWedge=(0.0, 45.0) )
rad2.draw()
This works fine in Builder, dropping it into a code segment and knowing that Builder uses a window called win
Note that this uses the default mode for size (-1 to 1) so it will look different on different screen sizes. you may want to change to cm (and calibrate your monitor) if you want it to be round.
Thanks to Jon P for his suggestion (via google groups) to use RadialStim)
I create 2 Spartials and set one as RigidBodyControl, second as BetterCharacterControl.
Both are boxes of sizes = (10f, 1f, 10f) and (0.5f,0.5f,0.5f).
floor = createFloor();
CollisionShape sceneShape = CollisionShapeFactory.createMeshShape(floor);
landscape = new RigidBodyControl(sceneShape, 0);
floor.addControl(landscape);
rootNode.attachChild(floor);
character = createCharacter();
player = new BetterCharacterControl(1F,1F,0.01f);
character.addControl(player);
rootNode.attachChild(character);
bulletAppState.getPhysicsSpace().add(landscape);
bulletAppState.getPhysicsSpace().add(character);
landscape.setPhysicsLocation(new Vector3f(0,-4,5));
player.warp(new Vector3f(0,0,0));
Now if i run it ... the character just bounces on the floor and i don't know why.
If i use the standard CharacterControl it works, though.
I'am aware that the character box doesn't match the shape but that shouldn't be the problem i guess.
Thanks in advance!
Did you try and turn on debugging?
bulletAppState = new BulletAppState();
bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
stateManager.attach(bulletAppState);
bulletAppState.setDebugEnabled(true);
Using debugging could help you see the collision shapes.
thanks for sharing the debug tip.
The problem was:
player = new BetterCharacterControl(1F,1F,0.01f);
You shouldn't create a Cylinder that has a smaller radius than the sizes.
player = new BetterCharacterControl(1F,1F,1F);
worked fine.
In my case, I needed to increase the height of my character by increasing BetterCharacterControl(1.5f, 1f, 1f) to BetterCharacterControl(1.5f, 6f, 1f). I'm not sure why this solved the problem for me, but it did.