Question

In: Computer Science

python 2.7 Dash How to draw a bar & line & pie chart for below dict...

python 2.7 Dash

How to draw a bar & line & pie chart for below dict data by using Dash?

The value is the percentage of each month. So total will be 100.

{'Mar': 17.973851593144104, 'Feb': 10.540182187664472, 'Sep': 8.200076731097335, 'Apr': 12.080717324339435, 'Jan': 16.118724221364918, 'Nov': 12.29654875876966, 'Dec': 11.378407574427893, 'Oct': 11.411491609192186}

Please provide the python 2.7 code.

Solutions

Expert Solution

Python Code:

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objects as go

app = dash.Dash()

# The given dictionary
dict = {'Mar': 17.973851593144104, 'Feb': 10.540182187664472, 'Sep': 8.200076731097335, 'Apr': 12.080717324339435, 'Jan': 16.118724221364918, 'Nov': 12.29654875876966, 'Dec': 11.378407574427893, 'Oct': 11.411491609192186}

# Extracting the keys from the given dictionary
# i.e. the months
Xvalues = []
for x in dict:
   Xvalues.append(x)

# Extracting the values for each month
# from the dictionary
Yvalues = []
for x in dict:
   Yvalues.append(dict[x])
  
  

# defining the body of the web page
app.layout = html.Div(children=[
    html.H1(
        children='Graphs',
        style={
            'textAlign': 'center'
        }
    ),
    html.Div(children='Bar Graph', style={
        'textAlign': 'center'
    }),
   # Graph 1: Bar Graph
    dcc.Graph(
        id='bar',
        figure={
            'data': [
               # using plotly to define the graphs
               # x variable contains the months and corresponds to x axis
               # y variable contains the values and corresponds to y axis
               # name matters when there are different types of data to be compared
               go.Bar(x = Xvalues, y = Yvalues, name = 'Month')
            ]
        }
    ),
    html.Div(children='Line Graph', style={
        'textAlign': 'center'
    }),
   # Graph 2: Line Graph
   dcc.Graph(
        id='line',
        figure={
            'data': [
               # Scatter function is used for both line and scatter graphs
               # the difference is seen depending on the mode
               # if mode = 'lines', a line graph is displayed
               # if mode = 'markers', a scatter graph is displayed (with dots at the values)
               # if mode = 'lines + markers', a line graph, with dots at the values
               go.Scatter(x = Xvalues, y = Yvalues, name = 'Month', mode='markers + lines'),
            ]
        }
    ),
    html.Div(children='Pie Graph', style={
        'textAlign': 'center'
    }),
   dcc.Graph(
        id='pie',
        figure={
            'data': [
               # Pie graph
               go.Pie(labels=Xvalues, values=Yvalues, name='Months'),
            ]
        }
    )
])

# Running the server in localhost:8050
if __name__ == '__main__':
    app.run_server(debug=True)

Web page screenshots:

Bar graph:

Line Graph:

Pie Graph:


Related Solutions

Use python 2.7 & plotly (dash) to draw bar/line graph for below data OrderedDict([('0K', 7.239253544865276), ('PK',...
Use python 2.7 & plotly (dash) to draw bar/line graph for below data OrderedDict([('0K', 7.239253544865276), ('PK', 3.236322216916338), ('1', 6.415793586505012), ('2', 6.020145027564326), ('3', 5.658685936530415), ('4', 5.37435274038192), ('5', 5.1860079887723085), ('6', 5.035941053040876), ('7', 5.1264549715408), ('8', 5.553318856838249), ('9', 12.200551540951867), ('10', 11.195203964258715), ('11', 8.990680759944928), ('12', 12.767287811888968)]) Make sure all keys, especially the 0K & PK, are showing in the x-axis.
What are the advantages and disadvantages of using a bar chart, a pie chart, a doughnut...
What are the advantages and disadvantages of using a bar chart, a pie chart, a doughnut chart, and a Pareto chart?
Research the internet for an example of a pie chart or bar chart. Post a copy...
Research the internet for an example of a pie chart or bar chart. Post a copy along with its source. Include a question regarding the chart for your classmates to respond to.
True or False 1. PIE CHART, HISTOGRAM, and BAR CHART can be produced when one select...
True or False 1. PIE CHART, HISTOGRAM, and BAR CHART can be produced when one select T-TEST statistical function. 2. Descriptive statistics are run when researchers want to find out relationship between phenomena, such as if a higher gas price leads to more use of public transportation system. 3. In SPSS, DATA VIEW allows researchers to see actual numerical data that researchers have entered. 4. When running SPSS to generate PEARSON CORRELATION, one will use ANALYZE-à CORRELATE. 5. In PEARSON...
How to calculate the variance in a pie chart?
How to calculate the variance in a pie chart?
1.Are there circumstances in which a pie chart cannot be drawn, but a bar graph could...
1.Are there circumstances in which a pie chart cannot be drawn, but a bar graph could be drawn? If so, what are these circumstances and give examples. 2. when is a pie chart or bar chart preferred over the other one?
The simple bar diagram and the pie chart may be used to illustrate similar types of...
The simple bar diagram and the pie chart may be used to illustrate similar types of data. With the aid of two examples, demonstrate the preference of one over the other.
1. Provide circumstances and examples where a pie chart cannot be drawn, but a bar graph...
1. Provide circumstances and examples where a pie chart cannot be drawn, but a bar graph could be drawn? Same for where a pie chart cannot be drawn, but a bar graph could be drawn. 2. For each variable, identify the type of variable, discuss the type of graphical summary you might be able to draw to provide a visual summary of the data. The data: you learned the following information about 50 individuals: age, income, marital status, number of...
1. Identify which graphical display might be appropriate in each case. Bar chart, Pie Chart, Side-by-side...
1. Identify which graphical display might be appropriate in each case. Bar chart, Pie Chart, Side-by-side bar chart, segmented bar chart, histogram, dotplot, side-by-side boxplots, scatterplot Scenarios: Scenario Graphic Statisical Procedure 1.Investigate the number of months in your community during the last year                                           2.Investigate the relationship between GPA and SAT scores for stem related majors 3.Compare the annual incomes among engineering, science, and business majors 4.Compare the percentages of commuters among freshmen, sophomores, juniors, and seniors 5.Investigate the favorite method...
Draw a circle and construct "a pie chart" that describes your identity as you see it...
Draw a circle and construct "a pie chart" that describes your identity as you see it in terms of groups with which you are associated and that you consider to be important. The degree of importance of each group is represented by the magnitude of each slice of the pie chart. Please describe each slice of your circle and how much it means to you. We all belong to different groups (be specific): Possible groups: family (you are a husband,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT