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:
Related
Is there a way to create a subplot with Plotly Express using a for to plot the data? I've tried some things here but nothing worked as expected. This is my last try:
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=int(len(unique_pds)/2), cols=2)
for pds in unique_pds:
fig.add_trace(
go.Histogram(x=df[(df['Resolved'] >= resolved_date) &
(df['Components'].astype(str).str.contains(components)) &
(df['planned_effort'] == pds)]['lead_time']
)
).show()
The solution works almost perfectly, but the problem is that it is generating various subplots separated. I need all of them to be under the same subplot.
Thanks for the help.
Marcelo
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)
I am testing some plotly code here.
import plotly.express as px
# find business profits
pd.options.display.float_format = '{:.2f}'.format
df_gains = df_rev_exp[((df_rev_exp.ltd_spending) < df_rev_exp.REV2)]
df_gains.tail()
# scatter plot of losses
import plotly.express as px
fig = px.scatter(df_gains, x="site_name",
y="gain_or_loss",
color="gain_or_loss",
size='REV2', hover_data=['site_name','REV2'])
fig.update_xaxes(tickangle=325)
fig.show()
Everything plots just fine but the REV2 is pretty large, and as such it is hard to read when I hover over the data points in the chart. I'm trying to figure out a way to show numbers as millions. For instance, In would like to see 1.25M and not 1257789.84, which is what I am seeing now. I tried playing around with fig.update but I couldn't get anything working. How can I modify the formatting on these plotly charts?
I'm on Plotly 4.14.3 and this version shows 2.2M straight out of the box when the source is x=[10000000, 22000000, 34000000]:
import numpy as np
import plotly.graph_objects as go
fig = go.Figure()
fig.add_traces(go.Scatter(x=[10*10**6, 22*10**6, 34*10**6],
y=[10,12,14]))
fig.show()
So two things come to mind:
Update Plotly.
Check that you're inputting your values as values and not strings
I am a new user to Python. I am attempting to create a US county level chloropleth map. To get started I've been reading tutorials on how to do this here . The problem is when I execute the code exactly as it is written in the tutorial, I can't actually see the finished figure. A figure-object is created, but no plot. At the end of the day I am really looking to take this example and apply it to my own data.
Here is the code:
from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
counties = json.load(response)
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", dtype={"fips": str})
import plotly.express as px
fig = px.choropleth_mapbox(df, geojson=counties, locations='fips', color='unemp',
color_continuous_scale="Viridis",
range_color=(0, 12),
mapbox_style="carto-positron",
zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
opacity=0.5,
labels={'unemp':'unemployment rate'})
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
In case you are running your code from a standalone script, you can set the default renderer for plotly by adding the following lines at the beginning of your script:
import plotly.io as pio
pio.renderers.default = "browser"
In this case, your default browser will be used as a renderer. Other renders are available (take a look here).
Moreover, if you are running your code in a jupyter notebook, it is enough to have fig as last command of the cell and the figure will be displayed.
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.