How to use rangebreaks from plotly? - python

I would like to remove certain intervals from my xaxis in some plots using plotly. I found the attached example from https://plotly.com/python/time-series/. But running it gives me the error
ValueError: Invalid property specified for object of type
plotly.graph_objs.layout.XAxis: 'rangebreaks'
I even upgraded my plotly-version. How can I use the rangebreaks property?
import plotly.express as px
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
fig = px.scatter(df, x='Date', y='AAPL.High', range_x=['2015-12-01', '2016-01-15'],
title="Hide Gaps with rangebreaks")
fig.update_xaxes(
rangebreaks=[
dict(bounds=["sat", "mon"]), #hide weekends
dict(values=["2015-12-25", "2016-01-01"]) # hide Christmas and New Year's
]
)
fig.show()

if you are running the latest version of plotly this should work, maybe try to restart the kernel
and
check if you have these files in your system:
...\Lib\site-packages\plotly\validators\layout\yaxis_rangebreaks.py
..\Lib\site-packages\plotly\validators\layout\xaxis_rangebreaks.py

I am running colab and this worked for me:
!pip install --upgrade dash
Followed by restarting the kernel (or just run the update when you are first importing and installing packages).
Not really sure what's going on, but found the answer here.

Related

Plotly Express choropleth map not showing in visual studio code

Below shown the syntax used to get a map visualized and plotted from Plotly Express - choropleth from a "csv" DataFrame.
import pandas as pd
import numpy as np
import plotly.express as px
df = "//location.csv"
fig = px.choropleth(data_frame = df,
locations= df["location"],
locationmode='country names',
color=df["location"],
hover_name=df["location"],
title = "Location Data",
color_continuous_scale = px.colors.sequential.Oranges)
fig["layout"].pop("updatemenus")
fig.show()
However, when I use the above syntax on the Visual Studio Code Jupyter Notebook, the map does not get visualized and plotted. Which is shown as below,
But when I run the same code on the Anaconda Jupyter Notebook, I do get the map visualized and plotted as shown below,
Why isn't the map not getting visualized and plotted on VS code, and is there any way to resolve this issue on VS code?
I was interested in this question because I usually work with jypyterLab. I ran it based on this answer, and when I ran it in vscode, it displayed correctly in my default browser. The code I ran was based on the code in the official reference.
import plotly.express as px
from plotly.offline import plot
df = px.data.gapminder().query("year==2007")
fig = px.choropleth(df, locations="iso_alpha",
color="lifeExp", # lifeExp is a column of gapminder
hover_name="country", # column to add to hover information
color_continuous_scale=px.colors.sequential.Plasma)
# fig.show()
plot(fig)

Latex in y-axis title in plotly not working

Can you please tell me why fig.update_yaxes(title=dict(text="$\text{This is a test:} \sqrt{2^4}$") does not work in the following code
import plotly.express as px
fig = px.bar(df, x=["Apples", "Oranges"], y=[10,20], color=["Here", "There"],
labels=dict(x="Fruit", y="Amount", color="Place")
)
fig.update_yaxes(title=dict(text="$\text{This is a test:} \sqrt{2^4}$", font_size=16)
)
fig.show()
gives
To know for sure why it's not working on your end I would have to know:
your plotly version, and
how you're displaying your figure (JupyterLab?), and
whether or not there's enough space for your title where you're outputting your figure.
Because it works fine on my end:
I'm running Plotly '4.14.3' in JupyterLab.
Same code as yours:
import plotly.express as px
fig = px.bar(df, x=["Apples", "Oranges"], y=[10,20], color=["Here", "There"],
labels=dict(x="Fruit", y="Amount", color="Place")
)
fig.update_yaxes(title=dict(text="$\text{This is a test:} \sqrt{2^4}$", font_size=16)
)
fig.show()
Edit 1: Google Chrome
It turns out that this issue might be related to the browser your running. I'm running Microsoft Edge at the moment. But here's the same code and figure using Chrome wiht no title:

Plotly Mapbox images showing blank with blue background (Jupyter Notebook)

I am running the following code to plot points against a city backdrop using Mapbox within Plotly in a Jupyter Notebook, but the plot does not show up, I just get a blue background.
I suspect that I am not using the token correctly?
import plotly.express as px
MBToken = 'pk.[mypublickey]'
px.set_mapbox_access_token(MBToken)
fig = px.scatter_mapbox(dfMaster.dropna()
, lat="latitude"
, lon="longitude"
, color="nta"
, size="count_of_testers"
#, color_continuous_scale=px.colors.cyclical.IceFire
#, size_max=15
#, zoom=10
)
fig.show()
#fig = px.scatter(x='latitude',y='longitude',data_frame=df)
#fig.show()
Running that gives me:
It does not appear to be a Plotly issue, the commented out code creates a scatter plot (although that has the same blue background, but the points show)
Some other posts have pointed to Jupyter offline mode being the possible culprit, but adding this did not resolve
import plotly.offline as pyo
pyo.init_notebook_mode()
Additionally, tried starting up the Jupyter notebook with a higher data rate limit as suggested, but no luck there either
This ended up being a silent data integrity error, as the size field was a string and needed to be converted into numeric

matplotlib.pyplot x-axis is nanosec even if index in pandas is datetime

I have a pandas data frame where the index is of type pandas.core.indexes.datetimes.DatetimeIndex. I have two columns Mx and My, and I want to plot it using matplotlib.
Code:
start=pd.Timestamp('2017-02-10 00:00')
stop=pd.Timestamp('2017-03-10 16:00')
plt.figure()
plt.plot(mydata[start:stop]['Mx'], label='Mx')
plt.plot(mydata[start:stop]['My'], label='My')
plt.title('Timeseries')
plt.tight_layout()
plt.legend()
plt.ylabel('Angle [deg]')
plt.xlabel('UTC time')
plt.show()
For some reason, this works fine otherwise on my colleague's computers, but not mine. Any ideas on why it is like this?
Found the answer. I had matplotlib 2.0.0 installed. Upgraded to the latest (2.1.2) and it worked.

Seaborn stripplot doesn't show the facecolor with only 3 records

Explanation:
I am making a plot with stripplot method using hue argument, but some markers of the result image didn't have face colors I intended, instead they have black, gray or white.
The simplified code below makes a image alike in my case. There are only 3 records to plot, and their markers lose face colors.
I am new in Python, so I might miss something... If this post needs more info, please tell me.
Question:
Is there any work around that the only 3 record have face color?
Environment:
MacOS 10.11.1
Python 2.7.10 (Homebrew install)
seaborn 0.6.0 (pip install)
matplotlib 1.5.0 (pip install)
Code:
#!/usr/bin/env python
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
data = sns.load_dataset("tips")
data = data[data.sex == "Female"][0:3]
plot = sns.stripplot(
x="total_bill",
y="sex",
data=data
)
plt.show()
This problem has been already referred on GitHub.
See github.com/mwaskom/seaborn/issues/753

Categories

Resources