Python Plotly not showing 3d interactive plot - python

I am using Spyder ide. I have plotly 5.5.0 installed on my pc. The following executes with no errors but does not show/popup the 3d interactive plot.
Code:
import plotly.express as px
df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
color='species')
fig.show()

To be able to interact with the figure running from Spyder you will need to call show with the 'browser' renderer (this will show the figure on your default browser). The code provided with this change will look something like this:
import plotly.express as px
df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
color='species')
fig.show(renderer='browser')

Related

Candlestick on Tkinter

For the moment this code always opens the following candlestick chart on a google chrome page, however, I wanted to appear on a Tkinter.
I tried to use candlestick_ohlc for Tkinter (it works best on apparently Tkinter) but it does not give me the same appearance as this candlestick.
Is there any way that I can make this candlestick chart ( using go.candlestick) appear on Tkinter?
import yfinance as yf
import plotly.graph_objs as go
from plotly.subplots import make_subplots
import matplotlib.pyplot as plt
data = yf.download(tickers='BTC-USD', period='1mo', interval='5m')
print(data.tail())
fig3 = make_subplots(specs=[[{"secondary_y": True}]])
fig3.add_trace(go.Candlestick(x=data.index,
open=data['Open'],
high=data['High'],
low=data['Low'],
close=data['Close'],
))
fig3.update_yaxes(range=[0,700000000],secondary_y=True)
fig3.update_yaxes(visible=False, secondary_y=True)
fig3.update_layout(xaxis_rangeslider_visible=False)
fig3.update_layout(title={'text':'BTC-USD', 'x':0.5},yaxis_title='Price',xaxis_title='Date-Time')
fig3.show()

Blank Bar Charts in Plotly

I have a df - Wards - that contains the number of different events that happen on each ward of a hospital. I just want a simple bar chart of the totals of these events. I have used plotly before - I am no means an expert (evidently!) but I can't figure out where I am going wrong! With the code below I am seeing anything with fig.show(). I added the fig.write_image command to test - this returns the correct graph - but I can't figure out why my fig.show() command doesn't work
import plotly.express as px
import matplotlib.pyplot as plt
fig = px.bar(Wards, x='Ward', y='Total_Tasks')
fig.write_image("fig1.png")
fig.show()
I tried to duplicate your code but of course I don't have the data, so I made some up and I also didn't know for sure what modules you are importing, so I used the help at:
https://plotly.com/python/getting-started/
but anyway, here is what I came up with
that seems to work.
import plotly.express as px
import matplotlib.pyplot as plt
fig = px.bar(x=["a", "b", "c"], y=[1, 3, 2])
plt.savefig('fig1.png',bbox_inches="tight",dpi=600)
fig.show()

ipywidgets interactive with plotly doesn't produce a plot

I'm using GoogleColab. I'm trying to use interactive widgets with python plotly.
A function for produce a simple plotly just works:
from ipywidgets import interactive
import plotly.express as px
def g(x,y):
fig = px.scatter(x=df[x], y=df[y])
fig.show()
g('a','b')
output
But when I try to involve interactive widgets, the output comes with the widgets but without the plot:
def g(x,y):
fig = px.scatter(x=df[x], y=df[y])
fig.show()
interactive_plot = interactive(g, x=columns, y=columns)
interactive_plot
interactive output
When I change the library back to matplotlib it works well, as in this question. However, I have to use plotly because of their 'hovering' feature, which is truly convenient.

Plots not showing in Jupyter notebook

I am trying to create a 2x2 plots for Anscombe data-set
Loading Data-set and separating each class in data-set
import seaborn as sns
import matplotlib.pyplot as plt
anscombe = sns.load_dataset('anscombe')
dataset_1 = anscombe[anscombe['dataset'] == 'I']
dataset_2 = anscombe[anscombe['dataset'] == 'II']
dataset_3 = anscombe[anscombe['dataset'] == 'III']
dataset_4 = anscombe[anscombe['dataset'] == 'IV']
Creating a figure and dividing into 4 parts
fig = plt.figure()
axes_1 = fig.add_subplot(2,2,1)
axes_2 = fig.add_subplot(2,2,2)
axes_3 = fig.add_subplot(2,2,3)
axes_4 = fig.add_subplot(2,2,4)
axes_1.plot(dataset_1['x'], dataset_1['y'], 'o')
axes_2.plot(dataset_2['x'], dataset_2['y'], 'o')
axes_3.plot(dataset_3['x'], dataset_3['y'], 'o')
axes_4.plot(dataset_4['x'], dataset_4['y'], 'o')
axes_1.set_title('dataset_1')
axes_2.set_title('dataset_2')
axes_3.set_title('dataset_3')
axes_4.set_title('dataset_4')
fig.suptitle('Anscombe Data')
fig.tight_layout()
The only output which i'm getting at each plot is
[<matplotlib.lines.Line2D at 0x24592c94bc8>]
What am I doing wrong?
If you are working with a Jupyter Notebook then you can add the following line to the top cell where you call all your imports. The following command will render your graph
%matplotlib inline
Add%matplotlib inline or use matplotlib.pyplot.ion()
after you imported matplotlib.
From plotting docs:
Starting with IPython 5.0 and matplotlib 2.0 you can avoid the use of
IPython’s specific magic and use
matplotlib.pyplot.ion()/matplotlib.pyplot.ioff() which have the
advantages of working outside of IPython as well.
I've tried all of the above, eventually I've found out there is a conflict between matplotlib and a library called dtale. When I removed the import dtale command and restarted the kernel all was working prefectly good.
%matplot plt
Executing this after plt.show() displayed the plots for me.
Found this here: How do I make matplotlib work in AWS EMR Jupyter notebook?

Idle and Pandas-Python

I am trying to launch this code that I have found from a IPython notebook
(I also add some code like :interactive(True)...)
And my problem is when I use "run module" with Idle it launches the "data.plot"
then it loads and nothing happens. The data.plot doesn't seem to work.
Thanks if you have any idea.
Note: Without "interactive(True)" a box show up with "Runtime error"
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import interactive
interactive(True)
# read data into a DataFrame
data = pd.read_csv('http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv', index_col=0)
print(data.head())
# print the shape of the DataFrame
print data.shape
# visualize the relationship between the features and the response using scatterplots
fig, axs = plt.subplots(1, 3, sharey=True)
data.plot(kind='scatter', x='TV', y='Sales', ax=axs[0], figsize=(16, 8))
data.plot(kind='scatter', x='Radio', y='Sales', ax=axs[1])
data.plot(kind='scatter', x='Newspaper', y='Sales', ax=axs[2])
Try display(data.plot(PARAMS)). To import it try from IPython.core.display import display.
This worked for me in some iPython Notebooks.

Categories

Resources