question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Plotting histogram instead of bar chart

See original GitHub issue

I am trying to make a stacked bar graph but when I give a list to the parameter “y” to the function “plotly.express.bar”, plotly creates a histogram instead of a bar chart.

import pandas as pd
import plotly.express as px

tweets = pd.read_csv("Tweets.csv")
tweets.head()

bar = tweets[['airline','airline_sentiment']]
bar.reset_index()

new_df = pd.DataFrame(columns = ["airline", "positive", "negative", "neutral"])

airlines = bar['airline'].unique()

for i in range(len(airlines)):
    new_df.loc[len(new_df),'airline']=airlines[i]
    x = bar[bar['airline'] == airlines[i]]
    new_df.loc[len(new_df)-1,'positive'] = (x['airline_sentiment'] == 'positive').sum()
    new_df.loc[len(new_df)-1,'negative'] = len(x[x['airline_sentiment'] == 'negative'])
    new_df.loc[len(new_df)-1,'neutral'] = len(x[x['airline_sentiment'] == 'neutral'])
             
new_df

fig = px.bar(new_df, x = "airline", y = ["positive","negative", "neutral"], title = "Sentiments")
fig.show()

plotly

The CSV file and Jupyter notebook are included in the zipped file. StackedBar.zip

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolaskruchtencommented, Oct 11, 2020

It would be nice for Plotly to do this, yes, but it’s more of a data-later responsibility here IMO than a vis-layer concern. If Pandas is unable to get the type right in this case, it’s unlikely that Plotly will be able to do a better job 😃

0reactions
SinghShreyascommented, Oct 11, 2020

Yeah, you are right Nicolas.

Anyways, as a programmer, we should be able to debug the problem and your comment helped me in understanding the problem which was great learning for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

histogram versus bar graph - storytelling with data
Histograms visualize quantitative data or numerical data, whereas bar charts display categorical variables. In most instances, the numerical ...
Read more >
Histogram vs Bar Graph – Difference Between Them - Guru99
The Histogram refers to a graphical representation that shows data by way of bars to display the frequency of numerical data whereas the...
Read more >
A Histogram is NOT a Bar Chart - Forbes
Histograms plot quantitative data with ranges of the data grouped into bins or intervals while bar charts plot categorical data.
Read more >
Difference Between Histogram and Bar Graph
Histogram refers to a graphical representation; that displays data by way of bars to show the frequency of numerical data. A bar graph...
Read more >
Histogram vs bar graph: Which should you use? - ThoughtSpot
Two of the most common charts used to visualize data are bar graphs and histograms. These two can look alike, but they aren't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found