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):
Related
I'm new to python.
Again, uh..I apologize for the noobnes.
so, I have the following script
Basically I'm trying to learn by making projects, for example a .py that changes your wallpaper, plays a creepy sound and shows a 'message box'. and it repeats for 3 times.
import ctypes
from ctypes import wintypes as w
from playsound import playsound
count = 0
while (count < 3):
count = count + 1
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, "horror.jpg" , 0)
user32 = ctypes.WinDLL('user32')
MessageBox = user32.MessageBoxW
MessageBox.argtypes = w.HWND,w.LPCWSTR,w.LPCWSTR,w.UINT
MessageBox.restype = ctypes.c_int
MessageBox(None, u'works 3 times', u'test:', 0)
playsound('cursed.mp3')
my question would be : why is it working so slow or what am I doing wrong ?
For example, it firsts changes the wallpaper and shows the notification...after I click ok it plays the sound :/
Then it repeats...but it takes like 2 seconds in-between-commands...if that makes sense...I apologise for the extreme stupid question, but I would like some clarification...Have I skipped the 'syntax' class ?
I am trying to draw a sine wave as a beginner python project. So far everything was going as smoothly as possible for me, but when I started working on drawing lines between points, I started to get the Error mentioned below, which hasnt happened before. As I am fairly new to programming in general, I don't understand a) what __init__() is and does and b) what it means by "arguments" and where I am giving it too many/of the wrong kind (also I dont really know what a class is so try to keep your answer simple)
As stated before, the program used to work fine under python 2.7 on windows 10 64bit until I added the
l = Line(prex, prey, x, truey)
l.draw(win)
bit.
I have tried putting everything in a seperate class as I have seen it work in a similar example, this has however only led to more problems and confusion and since it didnt sem to help I have abandoned this approach.
I have tried googling and looking on the internet but I havent found anything that has satisfied me.
from math import sin
from graphics import *
def main():
global x #sets up variables and defines start values
global y
global truey #used for drawing points
global prex #used for drawing lines between points
global prey #used for drawing lines between points
truey = 1
x = 1
y = 1
prex = 0
prey = 0
win = GraphWin("Sin wave", 1000, 500) #creates the window
for i in range(1, 1000): #repeats the "logic"
x = x + 2.3 #goes along the x-axis to repeat calculations
y = sin(x) #calculates y-value which will be used later
truey = (y * 100) + 100 #calculates actual y value used to draw
c = Circle(Point(x, truey),2) #draws a point to represent wave
l = Line(prex, prey, x, truey) #connects points with lines, WIP
l.draw(win) #draws the line, WIP
c.setFill("black") #fills "points"
c.draw(win) #draws circles
print truey
win.getMouse() #keeps window open
win.close() #keeps window open
main()
And the error I get is:
Traceback (most recent call last):
File "C:/Users/Lion/.PyCharmCE2018.2/config/scratches/Sin wave.py", line 31, in <module>
main()
File "C:/Users/Lion/.PyCharmCE2018.2/config/scratches/Sin wave.py", line 23, in main
l = Line(prex, prey, x, truey)
TypeError: __init__() takes exactly 3 arguments (5 given)
Process finished with exit code 1
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.
I am trying to learn creating GUI using Tkinter .I created a window which includes text,Messagebox,Entry widget,labels and Radio buttons.
I used grid method for frames and tried to make entry boxes in row0 and row1 .And a message Box with Some text.But these are not properly aligned even though i gave correct rows and columns but output is not in order.
Entry box is created very far though i mentioned column1 .And message box is created as per the column specified.Can anyone help me how to solve this.If i am missing anything please let me now .
from Tkinter import*
import tkMessageBox
class Example:
def __init__(self,root):
root.title("Sample")
#Entry functions ---------------------------------------
Label(root, text="First Name").grid(row=0)
Label(root, text="Last Name").grid(row=1)
self.e1 = Entry(root)
self.e1.bind("<Return>",self.ShowChoice_radio)
self.e2 = Entry(root)
self.e2.bind("<Return>",self.ShowChoice_radio)
self.e1.grid(row=0,column=1)
self.e2.grid(row =1,column = 1)
#------------------------------------------------------------------------
self.frame=Frame(root)
self.frame.grid(row=3,sticky=W)
self.label=Label(self.frame, text="mine", width=12,bg="green",fg="white",justify=LEFT)
self.label.grid(row=3,column=4,sticky=W,pady=4)
root.minsize(width=666, height=220)
self.v=IntVar()
role=[("ENGLISH",1),("SPANISH",2),("GERMAN",3)]
Label(self.frame,text="Choose your role of target:",justify=LEFT,padx=2,pady=2).grid(row=4,sticky=W)
i=0
for txt,val in role:
i=i+1
self.rad_bt=Radiobutton(self.frame,text=txt,padx=20,variable=self.v,
command=self.ShowChoice_radio,value=val)
self.rad_bt.grid(row=4,column=i+1)
self.bottomframe = Frame(root)
self.bottomframe.grid(row=12,sticky=W)
self.hello(12)
T=Text(self.bottomframe,height=2,width=30)
T.pack(padx=100,side=TOP)
T.insert(END,"just a normal text to display!\n")
self.mbutton=Button(self.bottomframe,text='Quit',command=self.callback,state='normal')
self.mbutton.pack(padx=3,pady=3,side='left')
self.help=Button(self.bottomframe,text='Help',command=self.help_msg,width=5,justify=CENTER)
self.help.pack(padx=93,pady=3,side='left')
def ShowChoice_radio(self):
print self.v.get()
def help_msg(self):
tkMessageBox.showinfo("Help to print ",message="Not yet implemented")
root.minsize(width=666, height=666)
self.show_entry_fields()
self.help.config(state=DISABLED)
def callback(self):
if tkMessageBox.askyesno('verify','Really Quit?'):
root.destroy()
def hello(self,name):
w=Label(root,text="Hello Tkinter!",width=15).grid(row=10)
whatever_you_do = "Whatever . it is my test that \n i can anble to display manner in this case find out whether it is correct one or wrong \n)"
msg=Message(root, anchor='s',width=200,text = whatever_you_do)
msg.config(bg='lightgreen', font=('times', 14, 'italic'))
msg.grid(row=10,column=1,sticky=W)
def show_entry_fields(self):
print "First Name: %s\nLast Name: %s" % (self.e1.get(), self.e2.get())
if __name__=="__main__":
root=Tk()
app=Example(root)
root.mainloop()
Even the quit and Help buttons are not proper...!!!
I initially voted to close this because there is not a clear question, but mostly only a series of statements and opinions, at least one of which is incorrect. After looking more, I think I can answer your implied question "Why is tkinter behaving in a way that seems wrong to me?". The answer, I believe, is that you do not understand that grid coordinates are independent (start over) for each container gridded. Also, coordinates not used are ignored. In particular:
Root has a grid of 5 rows and 2 columns. Renumbering the rows 0, 1, 2, 3, 4 instead of the confusing labeling you used, there is no entry in column 1 for rows 2 and 4. The width of column 0 is determined by the width of self.frame in row 2, column 0. The entry boxes are far to the right because column 0 is very wide.
Self.frame has a grid of 2 rows and 4 columns. The first 3 columns of row 0 are empty. Self.bottomframe is packed instead of gridded. The buttons are to the left of where you want because you packed them to the left. In other words, tkinter did just what you said, which is apparently not what you want.
You might list the result better if you got rid of self.frame, put 'mine' in (2,0) or (2,0), 'Choose...' in (3, 0), and a frame with 3 radio buttoms in (3,1). Then root column 0 would not be so overly wide.
My Data Set Looks like
1
2
3
4
5
...
I have an intermediate step which should do the folowing
1
1,2
1,2,3
1,2,3,4
1,2,3,4,5
....
And finally calculate its mean
1
1.5
2
2.5
3
...
Questions
a) Is there a way to implement this in python / py-spark?.
b) Is there a method/api which does this out of the box.
c) I googled around for this kind of solution the closest i got was to moving mean/ rolling average / moving group. Is there a term for this operation?
In Pandas, this is called an expanding_mean:
import pandas as pd
df = pd.Series(range(1,6))
s = pd.Series(range(1,6))
pd.expanding_mean(s)
Out[128]:
0 1.0
1 1.5
2 2.0
3 2.5
4 3.0
dtype: float64
I'm not sure how you'd do this in Spark, but that said, I'm also not sure if this is a "parallelalizable" task: since each step relies on the previous step, I'm not sure how you'd break this up into steps.