Plotly producing a blank bar chart - python

I'm trying to produce a dashboard with plotly, wherein the graphs are sourced off a loaded dataframe. My problem currently is that upon creating a bar chart, the output is blank, even when I use a line chart, it works fine.
Here is a preview of my dataset:
It's a grouped-by object, I essentially have a larger dataset that I wanted topline figures for showing topline time series trends.
preview of my dataset
data set types
import plotly.offline as offline
import plotly.graph_objs as go
data = [go.Bar(
x=[Safe.Period],
y=[Safe.Units]
)]
pl.offline.iplot(data, filename='basic-bar')
To which I get this blank output:
blank output
Now I've got it to work like this:
trace = go.Scatter(x= Topline.Period,
y= Topline.Units,
mode = "lines")
data = [trace]
pl.offline.iplot({"data":data})
Line chart
So in a line chart, or at least a scatter connected by lines, the code works. but not when its done as a bar chart.
Can anybody shed some light on this/ recommend any fixes/checks? I'm still getting accustomed to plotly and hope to one day move my excel dashboarding into python/plotly.

Related

Is there a way to plot lines over a datashader plot in Bokeh (Python)?

I am working with relatively large datasets (approximately 10x20.000.000 data point), for which Datashader is a useful visualisation tool. To give more information in these visualisations, I would like to add lines showing averages/standarddeviations on top of this datashade figure. Does anyone know how this would be possible?
My current code:
from bokeh.plotting import figure
from bokeh.io import show
x = 'xcol'
y= 'ycol'
data = dataframe
fig = figure(x_axis_label=x, y_axis_label=y)
points = hv.Points(data[[x, y]], label=('Title'))
hd.datashade(points, cmap='crest')
What I would like to do is for example add the following line to the figure generated with the code above:
fig.line([1,10,20], [0, 1000,2000], line_width=4)
Thanks in advance.

Combining a bar plot and a line plot in matplotlib without shifting the bar plot

I am looking for a way to combine a bar and a line plot, without the bar plot shifting when the line plot is added.
The following code is used to generate the barplot
import matplotlib.pyplot as plt
import pandas as pd
data = pd.DataFrame([[4,30,0,3,2,2,], [5,24,0,3,1,1,], [6,34,0,4,2,1], [7,18,0,2,1,1], [8,34,0,3,3,2]], columns=['t', 'Cost', 0,1,2,3])
data[[1,2,3]].plot(kind='bar')
Thus, the data looks as follows
and the following plot is generated:
Next, I add the cost information using
data['Cost'].plot(style='o--', c='black', secondary_y=True)
Running it all together returns the following graph:
The issue is that the outer bars are no longer visible. I tried changing the range on the x-axis with xlim, but that did not help, it only made it worse. There is probably an easy fix for it, which I have not been able to find anywhere online.
I don't have the issue, running your code:
That said, an easy fix is to run ax.set_xlim(-0.5, 4.5)

Stuck on How to Display Hover Data in Specific Format

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

Matplotlib bug in histogram - all stacked in one bar

When I run :
plt.hist('%s/draws.csv' % CONFIG['build']['draw_data'])
I get this weird histogram,
all my data is put under the same frequency bar, and I cannot get rid of the file path below, neither show x.ticks.
Its weird because, my data is simply a csv file exported from np.random.normal(size=100000).
When I run directly the code :
data = np.random.normal(size=10000)
plt.hist(data)
I get a normal histogram :
What could be the issue here?

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

Categories

Resources