I`m making a 'Rolling Dice' project with Plotly. before that, I install Plotly using the command:
$ python -m pip install --user plotly
When I run 'from plotly.gragh_objs import Bar, layout', I got the error message:
'ModuleNotFoundError: No module named 'plotly.gragh_objs''
I try to upgrade pip, but it doesn`t work. anyone know where are the problems.
My code as follow:
from plotly.gragh_objs import Bar, Layout
from ploty import offline
from dice import Die
die = Die()
results = []
for roll_num in range(1000):
result = die.roll()
results.append(result)
frequencies = []
for value in range(1, die.num_sides+1):
frequency = results.count(value)
frequencies.append(frequency)
x_values = list(range(1, die.num_sides+1))
data = [Bar(x=x_values, y=frequencies)]
x_axis_config = {'title':'Result'}
y_axis_config = {'title':'Frequency of Result'}
my_layout = Layout(title='Results of rolling one D6 1000 times',
xaxis=x_axis_config, yaxis=y_axis_config)
offline.plot({'data':data, 'layout':my_layout}, filename='d6.html')
The import code should be as follows:
from plotly.graph_objects import Bar, Layout
from plotly import offline
Related
I have a problem using python module in Jupyter. It was working fine until yesterday. The only new thing is that I updated Seaborn to th elatest version. I do not have the pb when I use Spyder directly.
For exemple, I have a file that would be like :
import numpy as np
import pandas as pd
import scipy
def test_skewn(TimeSeries):
tempTimeSeries = TimeSeries.copy()
temp_Rolling_Perf = (tempTimeSeries / tempTimeSeries.shift(1) -1 ).dropna()
current_skew = scipy.stats.skew(temp_Rolling_Perf.iloc[i:i+Maturity-1])
return(current_skew )
I work perfectly from Spyder but from Jupyter it return :
AttributeError: module 'scipy' has no attribute 'stats'
If I correct it like this :
import numpy as np
import pandas as pd
#import scipy
from scipy.stats import skew, kurtosis
def test_skewness(TimeSeries):
tempTimeSeries = TimeSeries.copy()
temp_Rolling_Perf = (tempTimeSeries / tempTimeSeries.shift(1) -1 ).dropna()
#current_skew = scipy.stats.skew(temp_Rolling_Perf.iloc[i:i+Maturity-1])
current_skew = skew(temp_Rolling_Perf.iloc[i:i+Maturity-1])
return(current_skew )
It works in Jupyter.
It was working with both version before and I am not confortable at all with this pb and I would like to understand where it can come from.
I am following a python tutorial about the use of plotly.
Here are some commands I have to run to import functions and methods I will use
import plotly.plotly as py
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
However, when I run commands on my jupyter nootbook, it says the use of plotly is deprecated and it recommends me to use the module chart_studio instead (error points to line import plotly.plotly as py):
ImportError: The plotly.plotly module is deprecated, please install
the chart-studio package and use the chart_studio.plotly module
instead.
So I run
pip install chart_studio
and try to replace the line above with functions and methods coming from the chart_studio module.
Here is my code:
import chart_studio.plotly as py
import plotly.graph_objects as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
data = dict(type= 'cloropleth',
locations = ['AZ','CA','NY'],
locationmode = 'USA-states',
colorscale = 'Portland',
text = ['text 1','text 2','text 3'],
z = [1,2,3],
colorbar = {'Title':'Colorbar title goes here'})
mylayout = dict(geo={'scope':'usa'})
choromap = go.Figure(data = [data], layout=mylayout, skip_invalid=True)
iplot(choromap)
The problem is that, when running the final line iplot(choromap), I get this empty graph
While in the tutorial this other graph appears
What is wrong?
Please note that I installed cufflinks-0.17.3 plotly-4.5.4
Did you try to cleanly install plotly?
Uninstall plotly using pip
!pip uninstall plotly
Then uninstall plotly using conda
!conda uninstall plotly
After that, install the last version using pip
!pip install plotly
check Plotly version
import plotly
plotly.__version__
SOLVED
In my code there were 2 errors:
at line
type= 'cloropleth',
I had the mispelled value 'cloropleth', where the correct value is 'choropleth',
and then at line
colorbar = {'Title':'Colorbar title goes here'})
I had 'Title', where the correct key is 'title' (lowercase).
Fixed them and now the map is correctly displayed.
Also, it was not necessary to install chart_studio.
So in the end the correct code is:
import plotly.graph_objects as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
data = dict(type= 'choropleth',
locations = ['AZ','CA','NY'],
locationmode = 'USA-states',
colorscale = 'Portland',
text = ['text 1','text 2','text 3'],
z = [1,2,3],
colorbar = {'title':'Colorbar title goes here'})
mylayout = dict(geo={'scope':'usa'})
choromap = go.Figure(data = [data], layout=mylayout)
iplot(choromap)
been playing around with plotly for Python, but constantly running into the same error message stated above. I installed plotly on Anaconda, getting the error message both on Jupyter and Spyder respectively:
#data manipulation
import pandas as pd
import numpy as np
#loading csv
df = df = pd.read_csv(r'C:\Users\hendev\Desktop\FIFA18 - Ultimate Team players.csv')
import plotly.graph_objs as go
# prepare data
x2017 = df.overall[df.added_date == 2017]
x2018 = df.overall[df.added_date == 2018]
trace1 = go.Histogram(
x=x2017,
opacity=0.75,
name = "2017",
marker=dict(color='rgba(171, 50, 96, 0.6)'))
trace2 = go.Histogram(
x=x2018,
opacity=0.75,
name = "2018",
marker=dict(color='rgba(12, 50, 196, 0.6)'))
data = [trace1, trace2]
layout = go.Layout(barmode='overlay',
title=' Rating added in 2017 and 2018',
xaxis=dict(title='students-staff ratio'),
yaxis=dict( title='Count'),
)
fig = go.Figure(data=data, layout=layout)
iplot(fig)
Any idea what I'm doing wrong?
Note: $ from plotly.plotly import iplot will work only till plotly v3.10.0 The plotly.plotly module has been deprecated in plotly v4.0. Check your version first:
$ python3 -c 'import plotly; print(plotly.__version__)' # for Python 3
$ python -c 'import plotly; print(plotly.__version__)' # for Python 2
v4.0 onwards, the functionality has been split into two modes: online and offline.
Online
If you wish to have the figures rendered online, you will now need to import chart-studio and use
from chart_studio.plotly import plot, iplot
Offline
If you want to render the images locally, you have multiple options:
from plotly.offline import iplot
# your code
iplot(fig)
or
from plotly.subplots import make_subplots
fig = make_subplots(# your args)
# your code
fig.show()
or
import plotly.io as pio
# your code
pio.show(fig)
or
import plotly.graph_objects as go
fig = go.Figure(# your args)
# your code
fig.show()
You can read up more on the renderers here.
You forgot to import it
from plotly.plotly import iplot
You need to call iplot with py:
import plotly.plotly as py
py.iplot(fig)
Just in case, if you want to plot in offline mode and save plot as file.hmtl:
import plotly.offline as py
plotly.offline.init_notebook_mode()
py.iplot(fig, filename="file.html")
Do not be afraid of looking through in documentation: here you can find nice and understandable examples how to use plotly properly. For example, here you can see how to plot simple bar chart, etc.
For Jupyter Notebook and Kaggle Notebooks, this helped me.
from plotly.offline import iplot
I want to play with the OpenAI gyms in a notebook, with the gym being rendered inline.
Here's a basic example:
import matplotlib.pyplot as plt
import gym
from IPython import display
%matplotlib inline
env = gym.make('CartPole-v0')
env.reset()
for i in range(25):
plt.imshow(env.render(mode='rgb_array'))
display.display(plt.gcf())
display.clear_output(wait=True)
env.step(env.action_space.sample()) # take a random action
env.close()
This works, and I get see the gym in the notebook:
But! it also opens an interactive window that shows precisely the same thing. I don't want this window to be open:
I made a working example here that you can fork: https://kyso.io/eoin/openai-gym-jupyter with two examples of rendering in Jupyter - one as an mp4, and another as a realtime gif.
The .mp4 example is quite simple.
import gym
from gym import wrappers
env = gym.make('SpaceInvaders-v0')
env = wrappers.Monitor(env, "./gym-results", force=True)
env.reset()
for _ in range(1000):
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
if done: break
env.close()
Then in a new cell
import io
import base64
from IPython.display import HTML
video = io.open('./gym-results/openaigym.video.%s.video000000.mp4' % env.file_infix, 'r+b').read()
encoded = base64.b64encode(video)
HTML(data='''
<video width="360" height="auto" alt="test" controls><source src="data:video/mp4;base64,{0}" type="video/mp4" /></video>'''
.format(encoded.decode('ascii')))
I just found a pretty nice work-around for this. This will work for environments that support the rgb_array render mode. Then we can use matplotlib's imshow with a quick replacement to show the animation. The following should work inside one cell of a jupyter notebook.
import os
import gym
import matplotlib.pyplot as plt
os.environ["SDL_VIDEODRIVER"] = "dummy"
from IPython.display import clear_output
env = gym.make("LunarLander-v2")#, render_mode="human")
env.action_space.seed(42)
observation, info = env.reset(seed=42, return_info=True)
for _ in range(1000):
observation, reward, done, info = env.step(env.action_space.sample())
if done:
observation, info = env.reset(return_info=True)
clear_output(wait=True)
plt.imshow( env.render(mode='rgb_array') )
plt.show()
env.close()
This worked for me in Ubuntu 18.04 LTS, to render gym locally. But, I believe it will work even in remote Jupyter Notebook servers.
First, run the following installations in Terminal:
pip install gym
python -m pip install pyvirtualdisplay
pip3 install box2d
sudo apt-get install xvfb
That's just it. Use the following snippet to configure how your matplotlib should render :
import matplotlib.pyplot as plt
from pyvirtualdisplay import Display
display = Display(visible=0, size=(1400, 900))
display.start()
is_ipython = 'inline' in plt.get_backend()
if is_ipython:
from IPython import display
plt.ion()
# Load the gym environment
import gym
import matplotlib.pyplot as plt
%matplotlib inline
env = gym.make('LunarLander-v2')
env.seed(23)
# Let's watch how an untrained agent moves around
state = env.reset()
img = plt.imshow(env.render(mode='rgb_array'))
for j in range(200):
# action = agent.act(state)
action = random.choice(range(4))
img.set_data(env.render(mode='rgb_array'))
plt.axis('off')
display.display(plt.gcf())
display.clear_output(wait=True)
state, reward, done, _ = env.step(action)
if done:
break
env.close()
I am currently trying to generate a heatmap in python from a text file, using R commands (with rpy2). It works fine in R, but when I take it to python, the Quartz interface displays quickly and then closes. I would like either to be able to save the quartz display to a file, or directly save my heatmap to a file without displaying it.
Here is the code I have been using:
import rpy2.robjects as robjects
robjects.r('''
library("gplots")
data = read.csv("/Users/.../Heatmap_data.txt")
DF = data.frame(data)
MD = data.matrix(DF,rownames.force=NA)
heatmap.2(MD, scale="none", col=redgreen(100), cexRow=0.1, key=FALSE, symkey=FALSE, trace="none", Colv=FALSE)
''')
I'm using python 2.7, on OS X Yosemite.
Thank you for any help.
import numpy as np
import rpy2.robjects as ro
import rpy2.robjects.numpy2ri
ro.numpy2ri.activate()
R = ro.r
data = np.random.random((10, 10))
R.png(file='/tmp/out.png')
R.heatmap(data)
R("dev.off()")
writes to the file /tmp/out.png without displaying the image:
.
Preventing the displayed image from immediately closing can be done like this:
script.py:
import numpy as np
import rpy2.robjects as ro
import rpy2.robjects.numpy2ri
import rpy2.rinterface as rinterface
import time
import threading
ro.numpy2ri.activate()
R = ro.r
def ion():
def r_refresh(interval = 0.03):
while True:
rinterface.process_revents()
time.sleep(interval)
t = threading.Thread(target=r_refresh)
t.daemon = True
t.start()
ion()
data = np.random.random((10, 10))
R.heatmap(data)
R("dev.copy(png,'/tmp/out2.png')")
R("dev.off()")
try:
# for Python2
raw_input()
except NameError:
# for Python3
input()
The raw_input or input call prevents the Python interpreter from exiting, thus allowing the window to stay open, until the user presses Enter.
The ion function calls rinterface.process_revents() periodically so the
displayed window will react to GUI events such as resizing or being closed.
dev.copy(png,'/tmp/out2.png') saves the already-displayed image to a
file.