Im trying to plot a bar plot in plotly that is representing net-gains (will have positive and negative bar values). But somewhat the negative values in the y-axis are being represented in gibberish. I tried several things, including using update_layout function, but nothing seems to work. Im using the make_subplots function because i want to plot multiple viz on one figure.
Im using databricks for this code.
Attaching my code and viz output:
net_gains = pd.DataFrame()
net_gains["general_net_gain"] = [-2,2,-1,2]
fig = plotly.subplots.make_subplots(rows=1, cols=1)
fig.add_bar(x=net_gains.index, y=net_gains["general_net_gain"], row=1, col=1)
fig.update_layout(height=400,width=500,showlegend=True)
Related
I'm trying to reproduce a chart that I made in Python using matplotlib using plotly as I would like my chart to have more interactivity and potentially other features that plotly can offer in combination with Dash.
I want to achieve a couple of things so I will ask the following questions:
Is it possible in Plotly to overlay multiple pie charts, one on top of each other but of different sizes?
How can I adjust the radius in Plotly? I know this option is available in matplotlib but I haven't found that in Plotly.
Is there a way to control each slice individually such as adjust the radius to make the slice longer, adjust opacity but an individual slice? I found an example with hiding a particular slice but this one changes the pie chart completely, I would like for the slice that was turned off/made invisible to stay there, pie chart shouldn't change its composition. https://community.plotly.com/t/is-there-a-way-to-hide-some-slices-by-default-in-a-pie-chart/884
Is there a way to use continuous colors/colormap in a pie chart? From what I've read the documentation only mentions the option to use discrete.
Below I attempted to make a smaller pie chart on top of another using some example from the documentation but I failed to do so. Btw, I'm using some custom data.
import plotly.graph_objs as go
df = pd.read_csv('data.csv', index_col=0)
# change index to string
df.index = df.index.map(str)
names = ['Null', 'Pred=-1', 'Pred=0', 'Other', 'R_min'] + df.index[5:].tolist()
trace0 = go.Pie(
values=df['sum_cnt'], labels=names, opacity=0.5)
trace1 = go.Pie(
values=df['sum_cnt'], marker_colors=px.colors.sequential.Sunset,
textinfo='none',
hoverinfo='none',
domain={'x': [0.33, 0.66], 'y': [0.0, 0.6]})
data = [trace0,trace1]
layout = go.Layout(title="Chart",
)
fig = go.Figure(data=data, layout=layout, layout_showlegend=False)
fig.show()
I'm trying to create a histogram based on the following dataset.
I want independent x axes with labels, so I tried the following code:
fig = go.Figure()
fig = px.histogram( x=df["mun"], y=df["cust"], color=df["prod"], facet_col=df["pr"] )
fig.update_xaxes(matches=None, showticklabels=True)
fig.show()
As you can see the second plot does not show the labels for x. I don't understand why this is happening. How can I fix it?
I don't know why this is happening - it may be some bug in the categorical axis labels when plotly generates facet plots.
You can manually specify category_array=['D','E'] when you update the xaxes, which is admittedly a brittle workaround:
fig.update_xaxes(matches=None, showticklabels=True, categoryarray=['D','E'])
Here is a Plotly Express scatterplot with marker color, size and symbol representing different fields in the data frame. There is a legend for symbol and a colorbar for color, but there is nothing to indicate what marker size represents.
Is it possible to display a "size" legend? In the legend I'm hoping to show some example marker sizes and their respective values.
A similar question was asked for R and I'm hoping for a similar results in Python. I've tried adding markers using fig.add_trace(), and this would work, except I don't know how to make the sizes equal.
import pandas as pd
import plotly.express as px
import random
# create data frame
df = pd.DataFrame({
'X':list(range(1,11,1)),
'Y':list(range(1,11,1)),
'Symbol':['Yes']*5+['No']*5,
'Color':list(range(1,11,1)),
'Size':random.sample(range(10,150), 10)
})
# create scatterplot
fig = px.scatter(df, y='Y', x='X',color='Color',symbol='Symbol',size='Size')
# move legend
fig.update_layout(legend=dict(y=1, x=0.1))
fig.show()
Scatterplot Image:
Thank you
You can not achieve this goal, if you use a metric scale/data like in your range. Plotly will try to always interpret it like metric, even if it seems/is discrete in the output. So your data has to be a factor like in R, as you are showing groups. One possible solution could be to use a list comp. and convert everything to a str. I did it in two steps so you can follow:
import pandas as pd
import plotly.express as px
import random
check = sorted(random.sample(range(10,150), 10))
check = [str(num) for num in check]
# create data frame
df = pd.DataFrame({
'X':list(range(1,11,1)),
'Y':list(range(1,11,1)),
'Symbol':['Yes']*5+['No']*5,
'Color':check,
'Size':list(range(1,11,1))
})
# create scatterplot
fig = px.scatter(df, y='Y', x='X',color='Color',symbol='Symbol',size='Size')
# move legend
fig.update_layout(legend=dict(y=1, x=0.1))
fig.show()
That gives:
Keep in mind, that you also get the symbol label, as you now have TWO groups!
Maybe you want to sort the values in the list before converting to string!
Like in this picture (added it to the code above)
UPDATE
Hey There,
yes, but as far as I know, only in matplotlib, and it is a little bit hacky, as you simulate scatter plots. I can only show you a modified example from matplotlib, but maybe it helps you so you can fiddle it out by yourself:
from numpy.random import randn
z = randn(10)
red_dot, = plt.plot(z, "ro", markersize=5)
red_dot_other, = plt.plot(z*2, "ro", markersize=20)
plt.legend([red_dot, red_dot_other], ["Yes", "No"], markerscale=0.5)
That gives:
As you can see you are working with two different plots, to be exact one plot for each size legend. In the legend these plots are merged together. Legendsize is further steered through markerscale and it is linked to markersize of each plot. And because we have two plots with TWO different markersizes, we can create a plot with different markersizes in the legend. markerscale is normally a value between 0 and 1 but you can also do 150% thus 1.5.
You can achieve this through fiddling around with the legend handler in matplotlib see here:
https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html
I am not using subplots for this since the requirement is of legend beside the chart which is not possible by using subplots. Subplots only lets the legend at once place, hence I have thought of using to different plots and placing them beside each other.
Here is the code which produced two charts, but i am unable to place them beside each other.
fig = go.Figure()
fig.add_trace(go.Scatter(x=data['time'],y=data['x'],mode='lines',name='xyz',hoverlabel=dict(bgcolor=['white']),hovertemplate='abcd<br>Probability: %{y}'))
fig.add_trace(go.Scatter(x=data['time'],y=predict_data['y'],mode='lines',name='abc'))
fig.update_layout(title='TRend charts',yaxis_title='Probabilities',autosize=False,width=1000,height=600,margin=dict(l=50,r=50,b=100,t=100,pad=4),paper_bgcolor='lightpink',xaxis_showgrid=False)
p_8 = opy.plot(fig,auto_open=False,output_type='div')
fig = go.Figure()
fig.add_trace(go.Scatter(x=data['time'],y=data['xy'],mode='lines',name='xyz1'))
fig.add_trace(go.Scatter(x=data['time'],y=data['ab'],mode='lines',name='abc`'))
fig.update_layout(title='Second Chart',yaxis_title='Probabilities',autosize=False,width=100,height=600,margin=dict(l=50,r=-50,b=100,t=100,pad=3),paper_bgcolor='yellow',xaxis_showgrid=False)
p_9 = opy.plot(fig,auto_open=False,output_type='div')
I get them one below the other. Any attributes which can get them beside each other?
Try this
fig = fig.make_subplots(rows=2, cols=1)
I am trying to create a waterfall chart, which is like a bar chart, except that each bar starts at the end of its neighboring bars, at the end or beginning, so you have the total, and can see how it breaks down.
I am trying to create this chart in python, but there are no direct charts in matplot.lib called waterfall.
I found code for a vertical waterfall, but I could not transform it to horizontal.
How can I transform a barh matplot chart, for example, to a horizontal waterfall?
I want to create a HORIZONTAL waterfall.
For example, I am trying to make each bar in barh chart in matplotlib start at end of other, but I do not think I am approaching the problem the right way, because I have no results so far.
It should look like this:
Code to create the plot:
my_plot = trans.plot(
kind='barh',
stacked=True,
bottom=blank,legend=None,
figsize=(10, 5)
)
How do I separate the bars?
EDIT
I have found this ready to use python package, but it doesn't work with dataframes, so I cannot use it.
import waterfall_chart
from matplotlib import transforms
a = ['sales','returns','credit fees','rebates','late charges','shipping']
b = [10,-30,-7.5,-25,95,-7]
my_plot = waterfall_chart.plot(a, b, rotation_value=30, sorted_value=True, threshold=0.2,
formatting="$ {:,.1f}", net_label="end result", other_label="misc",
Title="chart", x_lab="X", y_lab="money", blue_color="blue",
green_color="#95ff24", red_color="r")
rot = transforms.Affine2D().rotate_deg(90)
my_plot.show()
I also found this tutorial, with code, for a vertical waterfall chart.
https://pbpython.com/waterfall-chart.html.
It works great, but I didn't manage to reproduce the same thing for a horizontal waterfall.