Questions tagged [bar-chart]
A bar chart is graphical representation of data where the value is represented by the length of the bar.
8,392
questions
261
votes
6
answers
612k
views
Label axes on Seaborn Barplot
I'm trying to use my own labels for a Seaborn barplot with the following code:
import pandas as pd
import seaborn as sns
fake = pd.DataFrame({'cat': ['red', 'green', 'blue'], 'val': [1, 2, 3]})
...
233
votes
11
answers
578k
views
How to display the value on horizontal bars
I generated a bar plot, how can I display the value of the bar on each bar?
Current plot:
What I am trying to get:
My code:
import os
import numpy as np
import matplotlib.pyplot as plt
x = [...
182
votes
7
answers
491k
views
How to add value labels on a bar chart
I'm creating a bar chart, and I can't figure out how to add value labels on the bars (in the center of the bar, or just above it).
I believe the solution is either with 'text' or 'annotate', but I:
a) ...
172
votes
6
answers
732k
views
How to plot a histogram using Matplotlib in Python with a list of data?
If I have a list of y-values that correspond to bar height and a list of x-value strings, how do I plot a histogram using matplotlib.pyplot.hist?
Related: matplotlib.pyplot.bar.
163
votes
4
answers
255k
views
How can I change the Y-axis figures into percentages in a barplot?
How can we change y axis to percent like the figure? I can change y axis range but I can't make it to percent.
155
votes
4
answers
305k
views
Showing data values on stacked bar chart in ggplot2
I'd like to show data values on stacked bar chart in ggplot2. Here is my attempted code
library(ggplot2)
Data <- data.frame(
Year = c(rep(c("2006-07", "2007-08", "2008-...
138
votes
8
answers
378k
views
Rotating x axis labels in R for barplot
I am trying to get the x axis labels to be rotated 45 degrees on a barplot with no luck. This is the code I have below:
barplot(((data1[,1] - average)/average) * 100,
srt = 45,
...
136
votes
1
answer
110k
views
Position geom_text on dodged barplot
I tried to make the title self-explanatory, but here goes - data first:
dtf <- structure(list(variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L,
4L, 4L, 5L, 5L), .Label = c("vma", "vla", "ia", "fma", ...
126
votes
2
answers
186k
views
How to put labels over geom_bar for each bar in R with ggplot2
I've found this, How to put labels over geom_bar in R with ggplot2, but it just put labels(numbers) over only one bar.
Here is, let's say, two bars for each x-axis, how to do the same thing?
My ...
121
votes
3
answers
365k
views
How to change the color of a single bar in a bar plot
Supposely, I have the bar chart as below:
Any ideas on how to set different colors for each carrier? As for example, AK would be Red, GA would be Green, etc?
I am using Pandas and matplotlib in ...
114
votes
5
answers
163k
views
How do you plot bar charts in gnuplot?
How do you plot bar charts in gnuplot with text labels?
101
votes
4
answers
265k
views
R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph
I am getting this error while plotting a bar graph and I am not able to get rid of it, I have tried both qplot and ggplot but still the same error.
Following is my code:
library(dplyr)
library(...
96
votes
3
answers
126k
views
Pandas Plotting with Multi-Index
After performing a groupby.sum() on a DataFrame I'm having some trouble trying to create my intended plot.
import pandas as pd
import numpy as np
np.random.seed(365)
rows = 100
data = {'Month': np....
95
votes
4
answers
191k
views
Barchart with vertical ytick labels
I'm using matplotlib to generate a (vertical) barchart. The problem is my labels are rather long. Is there any way to display them vertically, either in the bar or above it or below it?
92
votes
8
answers
255k
views
How to display custom values on a bar plot
I'm looking to see how to do two things in Seaborn with using a bar chart to display values that are in the dataframe, but not in the graph.
I'm looking to display the values of one field in a ...
83
votes
5
answers
148k
views
Create stacked barplot where each stack is scaled to sum to 100%
I have a data.frame like this:
df <- read.csv(text = "ONE,TWO,THREE
23,234,324
34,534,12
56,324,124
34,...
82
votes
2
answers
392k
views
Plot multiple columns of pandas DataFrame on the bar chart
I am using the following code to plot a bar-chart:
import matplotlib.pyplot as pls
my_df.plot(x='my_timestampe', y='col_A', kind='bar')
plt.show()
The plot works fine. However, I want to improve ...
80
votes
3
answers
141k
views
Order categories by count in a countplot
I know that seaborn.countplot has the attribute order which can be set to determine the order of the categories. But what I would like to do is have the categories be in order of descending count. I ...
80
votes
6
answers
74k
views
Bar plot, no space between bottom of geom and x axis keep space above
When I plot a bar graph in ggplot2 I would like to reduce the space between the bottom of the bars and the x-axis to 0, yet keep the space above the bars and the plot box. I have a hack to do it ...
71
votes
4
answers
132k
views
How to prevent overlapping x-axis labels in sns.countplot
For the plot
sns.countplot(x="HostRamSize",data=df)
I got the following graph with x-axis label mixing together, how do I avoid this? Should I change the size of the graph to solve this problem?
66
votes
2
answers
75k
views
Reverse stacked bar order
I'm creating a stacked bar chart using ggplot like this:
plot_df <- df[!is.na(df$levels), ]
ggplot(plot_df, aes(group)) + geom_bar(aes(fill = levels), position = "fill")
Which gives me something ...
62
votes
5
answers
158k
views
How to plot bar graphs with same X coordinates side by side ('dodged')
import matplotlib.pyplot as plt
gridnumber = range(1,4)
b1 = plt.bar(gridnumber, [0.2, 0.3, 0.1], width=0.4,
label="Bar 1", align="center")
b2 = plt.bar(gridnumber, [0.3, 0.2, 0.2], ...
61
votes
6
answers
190k
views
Put stars on ggplot barplots and boxplots - to indicate the level of significance (p-value)
It's common to put stars on barplots or boxplots to show the level of significance (p-value) of one or between two groups, below are several examples:
The number of stars are defined by p-value, for ...
60
votes
4
answers
150k
views
How to prevent x-axis labels from overlapping
I'm generating a bar-chart with matplotlib. It all works well but I can't figure out how to prevent the labels of the x-axis from overlapping each other. Here an example:
Here is some sample SQL for ...
59
votes
1
answer
117k
views
Change bar color according to hue name
I'm using seaborn and pandas to create some bar plots from different (but related) data. The two datasets share a common category used as a hue, and as such I would like to ensure that in the two ...
58
votes
4
answers
72k
views
Edit the width of bars using pd.DataFrame.plot()
I am making a stacked bar plot using:
DataFrame.plot(kind='bar',stacked=True)
I want to control width of bars so that the bars are connected to each other like a histogram.
I've looked through the ...
58
votes
2
answers
225k
views
Grouped bar plot in ggplot
I have a survey file in which row are observation and column question.
Here are some fake data they look like:
People,Food,Music,People
P1,Very Bad,Bad,Good
P2,Good,Good,Very Bad
P3,Good,Bad,Good
P4,...
58
votes
5
answers
199k
views
How to increase the space between bar plot bars
How do I increase the space between each bar with matplotlib barcharts, as they keep cramming them self to the centre. (this is what it currently looks)
import matplotlib.pyplot as plt
import ...
55
votes
4
answers
102k
views
how to hide highchart x - axis data values
I am drawing a bar chart using highchart.js
I do not want to show the x - axis data values.
Can any one tell me which option does it?
full config:
var chart = new Highcharts.Chart({
...
54
votes
7
answers
42k
views
Multirow axis labels with nested grouping variables
I would like the levels of two different nested grouping variables to appear on separate lines below the plot, and not in the legend. What I have right now is this code:
data <- read.table(text = "...
52
votes
2
answers
35k
views
Ordering of bars in ggplot
I have looked through the answers in this forum but cannot seem to find an answer to this specific problem. I have the following data and want to create a bar chart where the bars are ordered from ...
51
votes
3
answers
21k
views
How to add group labels for bar charts
I want to plot data of the following form, using matplotlib bar plot:
data = {'Room A':
{'Shelf 1':
{'Milk': 10,
'Water': 20},
'Shelf 2':
...
50
votes
3
answers
87k
views
plotting value_counts() in seaborn barplot
I'm having trouble getting a barplot in seaborn. Here's my reproducible data:
people = ['Hannah', 'Bethany', 'Kris', 'Alex', 'Earl', 'Lori']
reputation = ['awesome', 'cool', 'brilliant', 'meh', '...
48
votes
2
answers
86k
views
matplotlib bar chart with dates
I know about plot_date() but is there a bar_date() out there?
The general method would be to use set_xticks and set_xticklabels, but I'd like something that can handle time scales from a few hours out ...
46
votes
2
answers
25k
views
How to rotate X-axis labels in bokeh figure?
I'm just starting to use Bokeh. Below I create some args I use for the rect figure.
x_length = var_results.index * 5.5
Multiplying the index by 5.5 gave me more room between labels.
names = ...
45
votes
4
answers
87k
views
Matplotlib, horizontal bar chart (barh) is upside-down
TL'DR, the vertical bar charts are shown in a conventional way -- things line up from left to right. However, when it is converted to horizontal bar chart (from bar to barh), everything is upside-down....
45
votes
4
answers
100k
views
How to scale Seaborn's y-axis with a bar plot
I'm using factorplot(kind="bar").
How do I scale the y-axis, for example with log-scale?
I tried tinkering with the plots' axes, but that always messed up the bar plot in one way or another, ...
43
votes
4
answers
126k
views
How to display all x labels in R barplot?
This is a basic question but I am unable to find an answer. I am generating about 9 barplots within one panel and each barplot has about 12 bars. I am providing all the 12 labels in my input but R is ...
43
votes
1
answer
56k
views
How can I add hatches, stripes or another pattern or texture to a barplot in ggplot?
Suppose I have data with both an ordinal variable and a categorical variable:
set.seed(35)
df <- data.frame(Class = factor(rep(c(1,2),times = 80), labels = c("Math","Science")),
...
42
votes
4
answers
58k
views
How to remove gaps between bars in a bar chart
I'm making a bar chart in Matplotlib with a call like this:
xs.bar(bar_lefts, bar_heights, facecolor='black', edgecolor='black')
I get a barchart that looks like this:
What I'd like is one with no ...
41
votes
3
answers
118k
views
ggplot2 : Plot mean with geom_bar
I have the following data frame:
test2 <- data.frame(groups = c(rep("group1",4), rep("group2",4)),
X2 = c(rnorm(4), rnorm(4)) ,
label = c(rep(1,2),rep(2,2),rep(1,2),rep(2,2)))
and I am ...
41
votes
3
answers
102k
views
Matplotlib bar graph x axis won't plot string values
I am using Python 2.7 and matplotlib. I am attempting to reach into my database of ambulance calls and count up the number of calls that happen on each weekday.
I will then use matplotlib to create a ...
40
votes
4
answers
242k
views
Simplest way to do grouped barplot
I have the following dataframe:
Catergory Reason Species
1 Decline Genuine 24
2 Improved Genuine 16
3 Improved Misclassified 85
4 Decline Misclassified ...
37
votes
3
answers
170k
views
How to get a barplot with several variables side by side grouped by a factor
I have a dataset which looks like this one below. I am trying to make a barplot with the grouping variable gender, with all the variables side by side on the x axis (grouped by gender as filler with ...
36
votes
2
answers
70k
views
Bar plot with groupby
My categorical variable case_status takes on four unique values. I have data from 2014 to 2016. I would like to plot the distribution of case_status grouped by year. I try the following:
df.groupby('...
34
votes
4
answers
106k
views
Plot the average values for each level
Using ggplot2 generate a plot which shows the following data.
df=data.frame(score=c(4,2,3,5,7,6,5,6,4,2,3,5,4,8),
age=c(18,18,23,50,19,39,19,23,22,22,40,35,22,16))
str(df)
df
Instead of ...
33
votes
3
answers
158k
views
Stacked Bar Plot in R
I've looked at the similar questions on here regarding stacked bar plots in R, but I'm still not having any luck.
I have created the following data frame:
A B C D E F G
1 ...
33
votes
3
answers
48k
views
Customizing the order of legends in plotly
I am trying to customize the order of legends while plotting stacked bar plots in plotly,python.
data = [
go.Bar(
y=df['sid'], # assign x as the dataframe column 'x'
...
33
votes
2
answers
21k
views
How to center stacked percent barchart labels
I am trying to plot nice stacked percent barchart using ggplot2. I've read some material and almost manage to plot, what I want. Also, I enclose the material, it might be useful in one place:
How do ...
31
votes
2
answers
53k
views
change color of only one bar in ggplot
I want to color only one bar in ggplot. This is my data frame:
area <- c("Północ", "Południe", "Wschód", "Zachód")
sale <- c(16.5, 13.5, 14, 13)
df.sale <- data.frame(area, sale)
colnames(df....