How to change plotly bar size - python

I am trying to plot a bar chart using python and plotly, but the size of the bar is high. I want to reduce the size of the bar.
Can anyone help me?
and I am sharing my code.
data_plots = go.Bar(x=df1['MeterReading_DateTime'], y= df1['ACT_IMP_TOT'],marker = {'color' : '#060805'},width = 1)
layout = {'title': '','xaxis': {'title': 'Date and time'},'yaxis': {'title': 'Total Import(KWH)'},'plot_bgcolor':'#8099b3' }
fig = {'data': data_plots, 'layout': layout}
plot_div = offline.plot(fig, output_type='div')
return plot_div

Related

Range Breaks not working for financial years and week crossover - plotly

I have a graph in plotly with the xaxis (int64) which is the yearweek. So the 52nd week in FY22 would be 2252 and the 1st week in FY23 2301.
This has been ok, plotting wise until I have come to a crossover of years. Visually 2252, 2301 and 2302 should all have a difference of one unit however 2252 to 2301 visually looks like 49 units and then 2301 to 2302 is one unit.
I have put in and used rangebreaks but visually nothing is changing.
I want that big long line to be removed
I have the following code and added in rangebreaks but to no avail.
`def sales_line_chart_weekly(location):
sales_line_weekly_chart_df = sales_line_chart_df.groupby(['YearWeek','Location Name'])['Sales ex VAT'].sum()
sales_line_weekly_chart_df = sales_line_weekly_chart_df.reset_index()
traces = []
for location in sales_line_weekly_chart_df['Location Name'].unique():
sales_line_chart_by_location = sales_line_weekly_chart_df[(sales_line_weekly_chart_df['Location Name']== location)]
traces.append(go.Scatter(
x = sales_line_chart_by_location['YearWeek'],
y = sales_line_chart_by_location['Sales ex VAT'],
name=location,
))
return {'data': traces,
'layout': go.Layout(title='Sales by Location by Week',
xaxis={'title': 'Date',
'type': 'linear',
'rangebreaks': [{'bounds': [2253, 2300]}]},
yaxis={'title': 'Sales ex VAT', 'tickformat': '.1f'})
}`
Didn't want the periods, weeks, quarters to be dates so I created a new column that ranked from 1. This will reset everytime new data is added.
I used this as the tickval and the ticktext was YearWeek.
def sales_line_chart_weekly(location):
sales_line_weekly_chart_df = sales_line_chart_df.groupby(['WeekDatesRanked','Location Name', 'YearWeek'])['Sales ex VAT'].sum()
sales_line_weekly_chart_df = sales_line_weekly_chart_df.reset_index()
traces = []
for location in sales_line_weekly_chart_df['Location Name'].unique():
sales_line_chart_by_location = sales_line_weekly_chart_df[(sales_line_weekly_chart_df['Location Name']== location)]
traces.append(go.Scatter(
x = sales_line_chart_by_location['WeekDatesRanked'],
y = sales_line_chart_by_location['Sales ex VAT'],
name=location,
))
return {'data': traces,
'layout': go.Layout(title='Sales by Location by Week',
xaxis={'title': 'Date',
'tickvals': sales_line_chart_by_location['WeekDatesRanked'].astype(str),
'ticktext': sales_line_chart_by_location['YearWeek']
},
yaxis={'title': 'Sales ex VAT', 'tickformat': '.1f'})
}

Dropdown filters for plotly dashboard

I have this dataset below and this code that outputs this chart
Column A
Tools
Category
role
figure
Occurences After
idx
0
PostgreSQL
DatabaseHaveWorkedWith
Developer, front-end
38.044286
4
False
1
MySQL
DatabaseHaveWorkedWith
Developer, front-end
45.883489
2
False
0
MongoDB
DatabaseWantToWorkWith
Developer, front-end
39.018110
1
True
1
PostgreSQL
DatabaseWantToWorkWith
Developer, front-end
48.236203
3
False
0
MySQL
DatabaseHaveWorkedWith
Developer, back-end
26.096002
1
True
1
PostgreSQL
DatabaseHaveWorkedWith
Developer, back-end
33.771734
2
False
0
Redis
DatabaseWantToWorkWith
Developer, back-end
28.495408
1
True
1
PostgreSQL
DatabaseWantToWorkWith
Developer, back-end
40.314136
1
True
from itertools import cycle
import plotly.express as px
fig = go.Figure()
palette = cycle(px.colors.qualitative.Alphabet)
colors = {c:next(palette) for c in daata['Tools'].values}
#Build dropdown Labels
labels = daata['role'].unique()
for i, row in daata.iterrows():
fig.add_trace(
go.Bar(x=[[row['role']],[row["Category"]]],
y=[row["figure"]],
name=row["Tools"],
text=str(round(row["figure"],2))+'%', showlegend=row['idx'],marker_color=colors[row["Tools"]],
legendgroup=row["Tools"]# Fix legend
))
fig.update_layout({
'barmode': 'group',
'xaxis': {
'tickangle': -45,
},
'yaxis': {
'title_text': "figures",
},
})
fig.show()
The above code outputs this chart
However, I want to also add a dropdown filter for the roles (i.e the chart can be filtered by 'Developer, back-end', 'Developer, front-end' and 'All'
I have tried adding button labels, all what have tried kept filtering the chart wrongly
With dash you can add Dropdown and then use value to return fig. Please check below code:
from dash import Dash, dcc, html, dash_table, Input, Output, callback
import plotly.express as px
import dash_bootstrap_components as dbc
import pandas as pd
from itertools import cycle
palette = cycle(px.colors.qualitative.Alphabet)
colors = {c:next(palette) for c in daata['Tools'].values}
#Build dropdown Labels
labels = daata['role'].unique()
app = Dash(__name__, external_stylesheets=[dbc.themes.LUX])
app.layout = html.Div([
dcc.Dropdown(id='dropdown',
options=[
{'label': x, 'value': x} for x in df['role'].unique()],
value=[],
multi=True,
disabled=False,
clearable=True,
searchable=True),
dcc.Graph(id='graph',figure={})
])
#app.callback(Output('graph','figure'),
Input('dropdown','value'))
def update_graph(role):
if role == []:
dff= daata.copy()
fig = go.Figure()
for i, row in dff.iterrows():
fig.add_trace(
go.Bar(x=[[row['role']],[row["Category"]]],
y=[row["figure"]],
name=row["Tools"],
text=str(round(row["figure"],2))+'%', showlegend=row['idx'],marker_color=colors[row["Tools"]],
legendgroup=row["Tools"]# Fix legend
))
fig.update_layout({
'barmode': 'group',
'xaxis': {'tickangle': -45},
'yaxis': {'title_text': "figures",},
})
else:
dff= daata[daata['role'].isin(role)]
fig = go.Figure()
for i, row in dff.iterrows():
fig.add_trace(
go.Bar(x=[[row['role']],[row["Category"]]],
y=[row["figure"]],
name=row["Tools"],
text=str(round(row["figure"],2))+'%', showlegend=row['idx'],marker_color=colors[row["Tools"]],
legendgroup=row["Tools"]# Fix legend
))
fig.update_layout({
'barmode': 'group',
'xaxis': {'tickangle': -45},
'yaxis': {'title_text': "figures",},
})
return fig
if __name__ == "__main__":
app.run_server(debug=False)
Hope this help

Python: Adding multiple lines on a map between sets of coordinates

I have the following code which works well:
import plotly.graph_objects as go
fig = go.Figure(go.Scattermapbox(
mode = "markers+lines",
lon = [-74.164556, -73.214697],
lat = [41.515941, 41.474395],
marker = {'size': 10}))
fig.update_layout(
margin ={'l':0,'t':0,'b':0,'r':0},
mapbox = {
'center': {'lon': 10, 'lat': 10},
'style': "stamen-terrain",
'center': {'lon': -20, 'lat': -20},
'zoom': 1})
fig.show()
Result:
I am now trying to add multiple lines from my dataframe but am not having any luck. This is what I am trying (have highlighted the new areas):
import plotly.graph_objects as go
Start_Lat = data['Start_Lat'] ## New code
Start_Lng = data['Start_Lng'] ## New code
End_Lat = data['End_Lat'] ## New code
End_Lng = data['End_Lng'] ## New code
fig = go.Figure(go.Scattermapbox(
mode = "markers+lines",
lat = [Start_Lat, End_Lat], ## New code
lon = [Start_Lng, End_Lng], ## New code
marker = {'size': 10}))
fig.update_layout(
margin ={'l':0,'t':0,'b':0,'r':0},
mapbox = {
'center': {'lon': 10, 'lat': 10},
'style': "stamen-terrain",
'center': {'lon': -20, 'lat': -20},
'zoom': 1})
fig.show()
The data looks like this:
Is anybody able to tell me what I am doing wrong here?
Thank you :)
With your data format, it's best to loop over the start and end coordinate pairs. Otherwise I think it should be a list with alternating start and end coordinates.
import plotly.graph_objects as go
fig = go.Figure()
for row in data.itertuples():
fig.add_trace(go.Scattermapbox(
mode = "markers+lines",
lat = [row.Start_Lat, row.End_Lat],
lon = [row.Start_Lng, row.End_Lng],
marker = {'size': 10}))
fig.update_layout(
margin ={'l':0,'t':0,'b':0,'r':0},
mapbox = {
'center': {'lon': data['Start_Lng'].mean(), 'lat': data['Start_Lat'].mean()},
'style': "stamen-terrain",
'zoom': 5.5})
fig.show()

how to hide plotly yaxis title (in python)?

Editing:
The following example from Plotly for reference:
import plotly.express as px
df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
fig = px.bar(df, y='pop', x='country', text='pop')
fig.update_traces(texttemplate='%{text:.2s}', textposition='outside')
fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide')
fig.show()
How to remove the word 'pop'.
What I want to hide the y-axis title of'value'.
The following syntax doesn't work.
fig.update_yaxes(showticklabels=False)
Thanks.
Solution
You need to use visible=False inside fig.update_yaxes() or
fig.update_layout() as follows. For more details see the
documentation for plotly.graph_objects.Figure.
# Option-1: using fig.update_yaxes()
fig.update_yaxes(visible=False, showticklabels=False)
# Option-2: using fig.update_layout()
fig.update_layout(yaxis={'visible': False, 'showticklabels': False})
# Option-3: using fig.update_layout() + dict-flattening shorthand
fig.update_layout(yaxis_visible=False, yaxis_showticklabels=False)
Try doing the following to test this:
# Set the visibility ON
fig.update_yaxes(title='y', visible=True, showticklabels=False)
# Set the visibility OFF
fig.update_yaxes(title='y', visible=False, showticklabels=False)
A. How to create the figure directly with hidden-yaxis label and tickmarks
You can do this directly by using the layout keyword and
supplying a dict to go.Figure() constructor.
import plotly.graph_objects as go
fig = go.Figure(
data=[go.Bar(y=[2, 1, 3])],
layout_title_text="A Figure Displaying Itself",
layout = {'xaxis': {'title': 'x-label',
'visible': True,
'showticklabels': True},
'yaxis': {'title': 'y-label',
'visible': False,
'showticklabels': False}
}
)
fig
B. How to create the figure without the margin space around
Say, you suppressed the titles for both the axes. By default plotly
would still leave a default amount of space all around the figure:
this is known as the margin in Plotly's documention.
What if you want to reduce or even completely remove the margin?
This can be done using fig.update_layout(margin=dict(l = ..., r = ..., t = ..., b = ...)) as mentioned in the documentation:
https://plotly.com/python/reference/#layout-margin.
In the following example, I have reduced the left, right
and bottom margins to 10 px and set the top margin to 50 px.
import plotly.graph_objects as go
fig = go.Figure(
data=[go.Bar(y=[2, 1, 3])],
layout_title_text="A Figure with no axis-title and modified margins",
layout = {
'xaxis': {'title': 'x-label',
'visible': False,
'showticklabels': True},
'yaxis': {'title': 'y-label',
'visible': False,
'showticklabels': False},
# specify margins in px
'margin': dict(
l = 10, # left
r = 10, # right
t = 50, # top
b = 10, # bottom
),
},
)
fig
C. An Interesting Feature of Plotly: A hidden shorthand
It turns out that Plotly has a convenient shorthand notation
allowing dict-flattening available for input arguments such as this:
## ALL THREE METHODS BELOW ARE EQUIVALENT
# No dict-flattening
# layout = dict with yaxis as key
layout = {'yaxis': {'title': 'y-label',
'visible': False,
'showticklabels': False}
}
# Partial dict-flattening
# layout_yaxis = dict with key-names
# title, visible, showticklabels
layout_yaxis = {'title': 'y-label',
'visible': False,
'showticklabels': False}
# Complete dict-flattening
# layout_yaxis_key-name for each of the key-names
layout_yaxis_title = 'y-label'
layout_yaxis_visible = False
layout_yaxis_showticklabels = False
Now try running all three of the following and compare the outputs.
import plotly.graph_objects as go
# Method-1: Shortest (less detailed)
fig = go.Figure(
data=[go.Bar(y=[2, 1, 3])],
layout_title_text="A Figure Displaying Itself",
layout_yaxis_visible = False,
layout_xaxis_title = 'x-label'
)
fig.show()
# Method-2: A hibrid of dicts and underscore-separated-syntax
fig = go.Figure(
data=[go.Bar(y=[2, 1, 3])],
layout_title_text="A Figure Displaying Itself",
layout_xaxis_title = 'x-label',
layout_yaxis = {'title': 'y-label',
'visible': False,
'showticklabels': False}
)
fig.show()
# Method-3: A complete dict syntax
fig = go.Figure(
data=[go.Bar(y=[2, 1, 3])],
layout_title_text="A Figure Displaying Itself",
layout = {'xaxis': {'title': 'x-label',
'visible': True,
'showticklabels': True},
'yaxis': {'title': 'y-label',
'visible': False,
'showticklabels': False}
}
)
fig.show()
How to remove the word 'pop'?
Just pass yaxis_title=None to fig.update_layout to hide default title of Y axis (similarly for xaxis_title=None for X axis).
import plotly.express as px
df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
fig = px.bar(df, y='pop', x='country', text='pop')
fig.update_traces(texttemplate='%{text:.2s}', textposition='outside')
fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide', yaxis_title=None)
fig.show()

How to change data and graph using buttons in plotly python?

Based on this code directly from plotly's tut page:
https://plot.ly/python/dropdowns/
Now, what if I want to change not just the chart type but rather the data source and its chart type?
Is it possible?
EDIT:
I've played with this settings:
data1 = go.Surface(z=df.values.tolist(), colorscale='Viridis')
data2 = go.Heatmap(z=df.values.tolist())
buttons=list([
dict(
args=[data1],
label='3D Surface',
method='restyle'
),
dict(
args=[data2],
label='Heatmap',
method='restyle'
)
])
However, the graphs are shown, but overlayed. And when I click any item in the dropdown menu, the graph is completely gone.
I've found a solution myself, that is actually based on the tut:
import plotly
import plotly.graph_objs as go
from datetime import datetime
import pandas_datareader as web
df = web.DataReader("aapl", 'google',
datetime(2015, 1, 1),
datetime(2016, 7, 1))
trace_high = go.Bar( x=df.index,
y=df.High,
name='High')
trace_low = go.Scatter(x=df.index,
y=df.Low,
name='Low',
line=dict(color='#F06A6A'))
data = [trace_high, trace_low]
updatemenus = list([
dict(active=-1,
buttons=list([
dict(label = 'High',
method = 'update',
args = [{'visible': [True, False]},
{'title': 'Yahoo High'}]),
dict(label = 'Low',
method = 'update',
args = [{'visible': [False, True]},
{'title': 'Yahoo Low'}])
]),
)
])
layout = dict(title='Yahoo', showlegend=False,
updatemenus=updatemenus)
fig = dict(data=data, layout=layout)
plotly.offline.plot(fig, auto_open=False, show_link=False)

Categories

Resources