In the code below I'm using get_window_extent() to get the height of a text label. I have set the figure dpi value to 72 dpi in an attempt to make the screen display and font size have a 1:1 relationship. My expectation is that the value retrieved by get_window_extent() would match the text point size value.
To test this out I created a loop to draw a set of text labels of increasing size and I'm finding that the value retrieved by get_window_extent() matches for some font sizes but not for others.
Here is the output produced by the code below:
Font Size
Set Returned
9 9.0
10 10.0
11 10.0
12 13.0
13 13.0
14 14.0
15 15.0
16 15.0
17 18.0
18 18.0
It appears that either the figure dpi setting is not actually at 72 dpi, or that something is amiss with the get_window_extent() method.
I'm running Matplotlib 1.5.0 on macOS 10.12.5, using the WXagg backend. Any ideas as why this is occurring would be welcome.
import matplotlib as mpl
mpl.use('wxagg')
import matplotlib.pyplot as plt
# points per inch
points_per_inch = 72
# set figure size in inches
myfsize = (8, 6)
# create figure and subplot axes matrix
myfig, ax = plt.subplots(1, 1, dpi=72, figsize=myfsize)
# adjust subplot spacing
plt.subplots_adjust(wspace=0.04, hspace=0.04, right=0.8,
bottom=0.1, top=0.9, left=0.125)
# draw canvase to get positions
plt.gcf().canvas.draw()
string = 'curve'
print
print 'Font Size'
print 'Set', '\t', 'Returned'
# loop over a range of font sizes and print retrieved font size
for i in range(10):
text_size = 9 + i
text_position = i / 10.0
txt = ax.text(0.0, text_position, string, fontsize=text_size,
transform=ax.transAxes)
plt.gcf().canvas.draw()
txt_height_display = txt.get_window_extent().height
print text_size, '\t', txt_height_display
plt.show()
Due to the discretization of the text onto screen pixels, there may always be a deviation between the number of pixels filled by the font and the fontsize. This deviation may be up tp 2 pixels - one to each side.
I therefore wouldn't be worried or supprised by the results you get.
Related
I am trying to create a corner plot for an upcoming paper, but I'm running into difficulty. I am creating an N x N array of subplots (currently, N = 6) and then deleting a bit over half of them. The issue is that the figure doesn't seem to resize itself after I delete the extraneous subplots, so when I later add a legend using a dummy subplot, it exists in the area where a full row and column of deleted subplots were, thus enlarging the figure. I've been working on this for several hours now and haven't found a solution. Here is the MWE:
import matplotlib.pyplot as plt
%matplotlib notebook
n_char = 8
# Set up the main figure.
fig, ax = plt.subplots(n_char, n_char, figsize=(n_char, n_char))
# Get rid of the axis labels unless it's on the left-most column or bottom-most row.
for i in range(0, n_char):
# For each row, loop over each column.
for j in range(0, n_char):
# If the plot isn't in the bottom-most row, get rid of the x-axis tick labels.
if i != n_char - 1:
ax[i, j].set_xticklabels([])
# If the plot isn't in the left-most column, get rid of the y-axis tick labels.
if j != 0:
ax[i, j].set_yticklabels([])
# Remove the plots that are repetitive or boring (plotting against the same characteristic).
for i in range(0, n_char):
# For each row, loop over each column.
for j in range(0, n_char):
# Delete the offending axes.
if j >= i:
ax[i, j].remove()
# Set the spacing between the plots to a much smaller value.
fig.subplots_adjust(hspace=0.00, wspace=0.00)
# Create a big plot for the legend. Have the frame hidden.
fig.add_subplot(111, frameon=False, xticks=[], yticks=[], xticklabels=[], yticklabels=[])
# Create some dummy data to serve as the source of the legend.
plt.scatter([10], [10], color="k", s=5, zorder=2, label="Targets")
# Set the x-axis limits such that the dummy data point is invisible.
fig.gca().set_xlim(-1, 1)
# Add the legend to the plot. Have it located in the upper right.
plt.legend(scatterpoints=1, loc="upper right", fontsize=5)
# Save the final plot.
fig.savefig("./../Code Output/Other Plots/Corner_Plot_Test.png", bbox_inches="tight", dpi=500)
I have looked at many different questions here on Stack Overflow. The two most promising candidates was this one, but I found the solution wasn't quite workable due to the large number of plots (and, to be frank, I didn't fully understand the solution). I thought that the first answer in this one might also work, as I thought it was a sizing issue (i.e. the figure wasn't resizing, so creating a new subplot was creating one the size of the original figure), but all it did was resize the entire figure, so that didn't work either.
To help, I will also include an image. I took the output of the code above and edited it to show what I want:
I should add that if I don't add a subplot, the output is as I expected (i.e. it's the proper size), so the issue comes in when adding the subplot, i.e. the line fig.add_subplot(111, frameon=False, xticks=[], yticks=[], xticklabels=[], yticklabels=[]).
The use of GridSpec may help.
GridSpec is used to specify array of axes to plot. You can set widths for columns and heights for rows as ratios in the option. The unneeded row should have very small height ratio, while unneeded column very small width ratio.
Here is the runnable code and output plot:-
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
#import numpy as np
fig = plt.figure(figsize=(8, 8))
nn = 6
# will create gridspec of 6 rows, 6 columns
# 1st row will occupy v small heights
# last column will occupy v small widths
sm = 0.01 # the v small width/height
wh = (1.-sm)/(nn-1.) # useful width/height
gs = gridspec.GridSpec(nn, nn, width_ratios=[*[wh]*(nn-1), sm], \
height_ratios= [sm, *[wh]*(nn-1)])
cols, rows = nn, nn
ax = [[0 for i in range(cols)] for j in range(rows)]
for ea in range(nn):
for eb in range(nn):
ax[ea][eb] = fig.add_subplot(gs[ea, eb])
ax[ea][eb].set_xticklabels([])
ax[ea][eb].set_yticklabels([])
if eb>=ea:
ax[ea][eb].remove()
# plot data on some axes
# note that axes on the first row (index=0) are gone
ax[2][0].plot([2,5,3,7])
ax[4][2].plot([2,3,7])
# make legend in upper-right axes (GridSpec's first row, last column)
# first index: 0
# second index: nn-1
rx, cx = 0, nn-1
ax[rx][cx] = fig.add_subplot(gs[rx,cx])
hdl = ax[rx][cx].scatter([10], [10], color="k", s=5, zorder=2, label="Targets")
ax[rx][cx].set_axis_off()
#ax[rx][cx].set_visible(True) # already True
ax[rx][cx].set_xticklabels([])
ax[rx][cx].set_yticklabels([])
# plot legend
plt.legend(bbox_to_anchor=(1.0, 1.0), loc='upper right', borderaxespad=0.)
fig.subplots_adjust(hspace=0.00, wspace=0.00)
plt.show
I am new to python. I tried the example given in here http://docs.bokeh.org/en/latest/docs/gallery/color_scatter.html with my own dataset, which looks like this
Unnamed: 0 fans id stars
0 0 69 18kPq7GPye-YQ3LyKyAZPw 4.14
1 1 1345 rpOyqD_893cqmDAtJLbdog 3.67
2 2 105 4U9kSBLuBDU391x6bxU-YA 3.68
3 3 2 fHtTaujcyKvXglE33Z5yIw 4.64
4 4 5 SIBCL7HBkrP4llolm4SC2A 3.80
here's my code:
import pandas as pd
from bokeh.plotting import figure, show, output_file
op = pd.read_csv('FansStars.csv')
x = op.stars
y = op.fans
radii = 1.5
colors = ["#%02x%02x%02x" % (int(r), int(g), 150) for r, g in zip(50+2*x, 30+2*y)]
TOOLS="hover,crosshair,pan,wheel_zoom,zoom_in,zoom_out,box_zoom,undo,redo,reset,tap,save,box_select,poly_select,lasso_select,"
p = figure(tools=TOOLS)
p.scatter(x, y, radius=radii,
fill_color=colors, fill_alpha=0.6,
line_color=None)
output_file("color_scatter.html", title="color_scatter.py example")
show(p)
However, when I run this code, I get no error and a webpage is opened, but BLANK. On reloading several times, I can finally see the tools, but that's all.
Can anyone tell me where am I going wrong?
Thanks!
I cant replicate this on Python 3.4 with Bokeh 0.12.3. So in that way, your code seems fine. I tried it both in the notebook (output_notebook) and to a file like you do and both seem to work fine.
The radius of 1.5 which you specify is taken in data units (x apparently), this makes the circles extremely big, covering the entire screen at first render. But using the wheelzoom to zoom out a bit reveals all circles as expected. Here is what your code looks like in Firefox for me (after zooming out):
How to set specific color such as #B0BF1A instead of black,white,grey
window.configure(background='white')
browse_label = gui.Label(window, text="Image path :", bg="white").place(x=20, y=20)
You can replace it from your example. such as if you want to use #B0BF1A
window.configure(background='#B0BF1A')
browse_label = gui.Label(window, text="Image path :", bg="#B0BF1A").place(x=20, y=20)
From the official tkinter documentation:
Colors can be given as the names of X colors in the rgb.txt file, or
as strings representing RGB values in 4 bit: "#RGB", 8 bit: "#RRGGBB",
12 bit” "#RRRGGGBBB", or 16 bit "#RRRRGGGGBBBB" ranges, where R,G,B
here represent any legal hex digit. See page 160 of Ousterhout’s book
for details.
I'm not sure whether this is compatible in python 2.7, but try this:
Default window colour Tkinter and hex colour codes
The code of the accepted answer is as follows (NOT MINE):
import Tkinter
mycolor = '#%02x%02x%02x' % (64, 204, 208) # set your favourite rgb color
mycolor2 = '#40E0D0' # or use hex if you prefer
root = Tkinter.Tk()
root.configure(bg=mycolor)
Tkinter.Button(root, text="Press me!", bg=mycolor, fg='black',
activebackground='black', activeforeground=mycolor2).pack()
root.mainloop()
I am using opencv-python library to do the liquid level detection. So far I was able to convert the image to gray scale and applying canny edge detection the container has been identified.
import numpy as np
import cv2
import math
from matplotlib import pyplot as plt
from cv2 import threshold, drawContours
img1 = cv2.imread('botone.jpg')
kernel = np.ones((5,5),np.uint8)
#convert the image to grayscale
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(imgray,120,230)
I need to know how to find water level from this stage.
Should I try machine learning, or is there any other option or algorithm available?
I took an approach of finding out the horizontal line in the edge detected image. If the horizontal line crosses certain threshold I can consider it as level.But the result is not consistent.
I want to know if there are any other approaches i can go with or white papers for reference?
I don't know how you would do that with numpy and opencv, because I use ImageMagick (which is installed on most Linux distros and is avilable for OSX and Windows), but the concept should be applicable.
First, I would probably go for a Sobel filter that is rotated to find horizontal edges - i.e. a directional filter.
convert chemistry.jpg -morphology Convolve Sobel:90 sobel.jpg
Then I would probably look at adding in a Hough Transform to find the lines within the horizontal edge-detected image. So, my one-liner looks like this in the Terminal/shell:
convert chemistry.jpg -morphology Convolve Sobel:90 -hough-lines 5x5+30 level.jpg
If I add in some debug, you can see the coefficients of the Sobel filter:
convert chemistry.jpg -define showkernel=1 -morphology Convolve Sobel:90 -hough-lines 5x5+30 sobel.jpg
Kernel "Sobel#90" of size 3x3+1+1 with values from -2 to 2
Forming a output range from -4 to 4 (Zero-Summing)
0: 1 2 1
1: 0 0 0
2: -1 -2 -1
If I add in some more debug, you can see the coordinates of the lines detected:
convert chemistry.jpg -morphology Convolve Sobel:90 -hough-lines 5x5+30 -write lines.mvg level.jpg
lines.mvg
# Hough line transform: 5x5+30
viewbox 0 0 86 196
line 0,1.52265 86,18.2394 # 30 <-- this is the topmost, somewhat diagonal line
line 0,84.2484 86,82.7472 # 40 <-- this is your actual level
line 0,84.5 86,84.5 # 40 <-- this is also your actual level
line 0,94.5 86,94.5 # 30 <-- this is the line just below the surface
line 0,93.7489 86,95.25 # 30 <-- so is this
line 0,132.379 86,124.854 # 32 <-- this is the red&white valve(?)
line 0,131.021 86,128.018 # 34
line 0,130.255 86,128.754 # 34
line 0,130.5 86,130.5 # 34
line 0,129.754 86,131.256 # 34
line 0,192.265 86,190.764 # 86
line 0,191.5 86,191.5 # 86
line 0,190.764 86,192.265 # 86
line 0,192.5 86,192.5 # 86
As I said in my comments, please also think about maybe lighting your experiment better - either with different coloured lights, more diffuse lights, different direction lights. Also, if your experiment happens over time, you could consider looking at differences between images to see which line is moving...
Here are the lines on top of your original image:
Begin Edit
After initial post I continued playing with my code. In my subplots I am making four plots of the same data set, with each subplot having a different time range. However, if I give each subplot the SAME time range then the minor ticks do not disappear. This may be why Deditos could not reproduce my issue.
That being said, if I manually create each subplot (with each having a different x-axis range), set the minor tick locations, THEN set each subplot's xrange I do not see the minor ticks disappear until I set ax3's (i.e. the last subplot) range.
It seems the issue is in having different x-axis ranges. Bizarre, I would think that by setting each axis' properties individually they would not all be tied together.
End Edit
I am creating one figure that has four sub-plots, all of which are time series. I have the xaxis major ticks spaced every four hours, and want minor ticks every hour. When I set the minor ticks for the first subplot (called ax1) the minor ticks appear, as they should. However, when I set the minor ticks in ax2 they show up in ax2, but the minor ticks in ax1 disappear. This repeats for ax3, and ax4. So, in the end I have only minor xaxis ticks in the fourth subplot. I had the same problem with the yaxis, but resolved this issue using yaxis.set_minor_locator(MultipleLocator(5)) for each axis (see below). However, MultipleLocator does not seem to work for time series data. Does anyone know how I can keep my minor xaxis ticks?
from pylab import *
from matplotlib.ticker import AutoMinorLocator, MultipleLocator
minor = AutoMinorLocator()
# Start plotting
fig = figure( figsize=(22,11) )
ax1 = fig.add_subplot(221) # 8-August 2011
ax2 = fig.add_subplot(222) # 9-August 2011
ax3 = fig.add_subplot(223) # 23-August 2011
ax4 = fig.add_subplot(224) # 24-August 2011
# This is repeated for ax2, ax3, and ax4, yielding a 2x2 grid of subplots.
# Plot 8-August 2011 data
ax1.plot(tpan.index,tpan.no2,'.-',markersize=10)
ax1.errorbar(tacam.index,tacam.no2,yerr=0.15,fmt='r.',markersize=12)
# Format plots
suptitle('Pandora/ACAM NO$_2$ Comparison', fontsize=22)
# Define xtick locations/string labels
xtickloc = [dt.datetime.combine(dates[0],dt.time())+dt.timedelta(hours=h) for h in range(0,25,4)]
xticklab = [dt.datetime.strftime(h,'%H:%M') for h in xtickloc]
ax1.set_xlabel('Hour of Day (UTC, EST+5)',fontsize=14)
ax1.set_ylabel('NO$_2$ Column Density (molec*cm$^{-2}$ E16)',fontsize=14)
ax1.xaxis.set_ticks(xtickloc)
ax1.yaxis.set_ticks(linspace(0,1.5,7))
ax1.xaxis.set_minor_locator(minor)
ax1.yaxis.set_minor_locator(MultipleLocator(5))
ax1.set_xticklabels(xticklab,fontsize=12,fontweight='bold')
ax1.set_yticklabels(linspace(0,1.5,7),fontsize=12,fontweight='bold')
ax1.axis( (dates[0],dates[0]+dt.timedelta(days=1),-0.05,1.5),fontsize=6,fontweight='bold')
ax1.tick_params(which='both',width=2,top='on')
ax1.tick_params(which='major',length=7)
ax1.tick_params(which='minor',length=4)
ax1.grid(linestyle='-',which='major',linewidth=1)
ax1.set_title('08-August 2011',fontsize=16)
ax1.legend( ('Pandora VCD','ACAM dSCD'),loc=2,ncol=2)
I was facing the same problem. I think what you need to do is:
ax1.xaxis.set_minor_locator(AutoMinorLocator())
instead of
ax1.xaxis.set_minor_locator(minor)
You are passing the same object to each of your axes. This object's contents are modified when you plot on ax4 based on the range on that subplot. Hope it helps.