how to plot histogram in matplotlib when data is in tuples?
I need to plot a histogram of the 5 most frequently occurring words in a list. I've used the collections module's c.counter().most_common() to give me the following tuples:
[('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
How can I plot a histogram when the data is in the format ('word', frequency)?
The format that I am familiar with is: ['you', 'you', 'you', ... , 'i', 'i', 'i', ... , etc.]
I know that I could multiply the string times the integer in each element to build a new list in the format I am familiar with to plot on the histogram but I feel like there has to be a more efficient way to do this.
python matplotlib
add a comment |
I need to plot a histogram of the 5 most frequently occurring words in a list. I've used the collections module's c.counter().most_common() to give me the following tuples:
[('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
How can I plot a histogram when the data is in the format ('word', frequency)?
The format that I am familiar with is: ['you', 'you', 'you', ... , 'i', 'i', 'i', ... , etc.]
I know that I could multiply the string times the integer in each element to build a new list in the format I am familiar with to plot on the histogram but I feel like there has to be a more efficient way to do this.
python matplotlib
add a comment |
I need to plot a histogram of the 5 most frequently occurring words in a list. I've used the collections module's c.counter().most_common() to give me the following tuples:
[('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
How can I plot a histogram when the data is in the format ('word', frequency)?
The format that I am familiar with is: ['you', 'you', 'you', ... , 'i', 'i', 'i', ... , etc.]
I know that I could multiply the string times the integer in each element to build a new list in the format I am familiar with to plot on the histogram but I feel like there has to be a more efficient way to do this.
python matplotlib
I need to plot a histogram of the 5 most frequently occurring words in a list. I've used the collections module's c.counter().most_common() to give me the following tuples:
[('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
How can I plot a histogram when the data is in the format ('word', frequency)?
The format that I am familiar with is: ['you', 'you', 'you', ... , 'i', 'i', 'i', ... , etc.]
I know that I could multiply the string times the integer in each element to build a new list in the format I am familiar with to plot on the histogram but I feel like there has to be a more efficient way to do this.
python matplotlib
python matplotlib
asked Nov 25 '18 at 7:12
Jacob MyerJacob Myer
496
496
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Unzip your list of tuples:
from matplotlib import pyplot as plt
a = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
plt.bar(*zip(*a))
plt.show()
Sample output:
add a comment |
You can use matplotlib bar chart:
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
items = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
y_pos = np.arange(len(items))
plt.bar(y_pos, [x[1] for x in items], align='center', alpha=0.5)
plt.xticks(y_pos, [x[0] for x in items])
plt.show()
With the result:
add a comment |
I prefer pandas for easy manipulation of data and plotting:
import pandas
freqs = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
# Create a DataFrame for the data, with names for the columns
freqdf = pandas.DataFrame(freqs, columns=['Word', 'Count']).set_index('Word')
freqdf.plot.barh()
Resulting plot:
Thank you but this was for an exercise in matplotlib unfortunately
– Jacob Myer
Nov 25 '18 at 7:49
add a comment |
Here's an extension of above solution using Matplotlib
as well as Seaborn
:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
lst = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
val, cnt = (zip(*lst))
val, cnt = list(val), list(cnt)
val, cnt
# (['you', 'i', 'we', 'my', 'he'], [7706, 6570, 2733, 2718, 2369])
# using Matplotlib
length = len(cnt)
plt.bar(np.arange(length), cnt, label=True)
plt.xticks(np.arange(len(cnt)), val)
plt.show()
# using seaborn
sns.barplot( val, cnt )
1
Thank you, I would prefer seaborn but I had to use matplotlib for this particular exercise.
– Jacob Myer
Nov 25 '18 at 7:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465421%2fhow-to-plot-histogram-in-matplotlib-when-data-is-in-tuples%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Unzip your list of tuples:
from matplotlib import pyplot as plt
a = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
plt.bar(*zip(*a))
plt.show()
Sample output:
add a comment |
Unzip your list of tuples:
from matplotlib import pyplot as plt
a = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
plt.bar(*zip(*a))
plt.show()
Sample output:
add a comment |
Unzip your list of tuples:
from matplotlib import pyplot as plt
a = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
plt.bar(*zip(*a))
plt.show()
Sample output:
Unzip your list of tuples:
from matplotlib import pyplot as plt
a = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
plt.bar(*zip(*a))
plt.show()
Sample output:
edited Nov 25 '18 at 7:37
answered Nov 25 '18 at 7:28
Mr. TMr. T
4,18791535
4,18791535
add a comment |
add a comment |
You can use matplotlib bar chart:
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
items = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
y_pos = np.arange(len(items))
plt.bar(y_pos, [x[1] for x in items], align='center', alpha=0.5)
plt.xticks(y_pos, [x[0] for x in items])
plt.show()
With the result:
add a comment |
You can use matplotlib bar chart:
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
items = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
y_pos = np.arange(len(items))
plt.bar(y_pos, [x[1] for x in items], align='center', alpha=0.5)
plt.xticks(y_pos, [x[0] for x in items])
plt.show()
With the result:
add a comment |
You can use matplotlib bar chart:
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
items = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
y_pos = np.arange(len(items))
plt.bar(y_pos, [x[1] for x in items], align='center', alpha=0.5)
plt.xticks(y_pos, [x[0] for x in items])
plt.show()
With the result:
You can use matplotlib bar chart:
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
items = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
y_pos = np.arange(len(items))
plt.bar(y_pos, [x[1] for x in items], align='center', alpha=0.5)
plt.xticks(y_pos, [x[0] for x in items])
plt.show()
With the result:
answered Nov 25 '18 at 7:17
DinariDinari
1,619522
1,619522
add a comment |
add a comment |
I prefer pandas for easy manipulation of data and plotting:
import pandas
freqs = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
# Create a DataFrame for the data, with names for the columns
freqdf = pandas.DataFrame(freqs, columns=['Word', 'Count']).set_index('Word')
freqdf.plot.barh()
Resulting plot:
Thank you but this was for an exercise in matplotlib unfortunately
– Jacob Myer
Nov 25 '18 at 7:49
add a comment |
I prefer pandas for easy manipulation of data and plotting:
import pandas
freqs = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
# Create a DataFrame for the data, with names for the columns
freqdf = pandas.DataFrame(freqs, columns=['Word', 'Count']).set_index('Word')
freqdf.plot.barh()
Resulting plot:
Thank you but this was for an exercise in matplotlib unfortunately
– Jacob Myer
Nov 25 '18 at 7:49
add a comment |
I prefer pandas for easy manipulation of data and plotting:
import pandas
freqs = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
# Create a DataFrame for the data, with names for the columns
freqdf = pandas.DataFrame(freqs, columns=['Word', 'Count']).set_index('Word')
freqdf.plot.barh()
Resulting plot:
I prefer pandas for easy manipulation of data and plotting:
import pandas
freqs = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
# Create a DataFrame for the data, with names for the columns
freqdf = pandas.DataFrame(freqs, columns=['Word', 'Count']).set_index('Word')
freqdf.plot.barh()
Resulting plot:
answered Nov 25 '18 at 7:29
chthonicdaemonchthonicdaemon
12.3k3147
12.3k3147
Thank you but this was for an exercise in matplotlib unfortunately
– Jacob Myer
Nov 25 '18 at 7:49
add a comment |
Thank you but this was for an exercise in matplotlib unfortunately
– Jacob Myer
Nov 25 '18 at 7:49
Thank you but this was for an exercise in matplotlib unfortunately
– Jacob Myer
Nov 25 '18 at 7:49
Thank you but this was for an exercise in matplotlib unfortunately
– Jacob Myer
Nov 25 '18 at 7:49
add a comment |
Here's an extension of above solution using Matplotlib
as well as Seaborn
:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
lst = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
val, cnt = (zip(*lst))
val, cnt = list(val), list(cnt)
val, cnt
# (['you', 'i', 'we', 'my', 'he'], [7706, 6570, 2733, 2718, 2369])
# using Matplotlib
length = len(cnt)
plt.bar(np.arange(length), cnt, label=True)
plt.xticks(np.arange(len(cnt)), val)
plt.show()
# using seaborn
sns.barplot( val, cnt )
1
Thank you, I would prefer seaborn but I had to use matplotlib for this particular exercise.
– Jacob Myer
Nov 25 '18 at 7:50
add a comment |
Here's an extension of above solution using Matplotlib
as well as Seaborn
:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
lst = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
val, cnt = (zip(*lst))
val, cnt = list(val), list(cnt)
val, cnt
# (['you', 'i', 'we', 'my', 'he'], [7706, 6570, 2733, 2718, 2369])
# using Matplotlib
length = len(cnt)
plt.bar(np.arange(length), cnt, label=True)
plt.xticks(np.arange(len(cnt)), val)
plt.show()
# using seaborn
sns.barplot( val, cnt )
1
Thank you, I would prefer seaborn but I had to use matplotlib for this particular exercise.
– Jacob Myer
Nov 25 '18 at 7:50
add a comment |
Here's an extension of above solution using Matplotlib
as well as Seaborn
:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
lst = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
val, cnt = (zip(*lst))
val, cnt = list(val), list(cnt)
val, cnt
# (['you', 'i', 'we', 'my', 'he'], [7706, 6570, 2733, 2718, 2369])
# using Matplotlib
length = len(cnt)
plt.bar(np.arange(length), cnt, label=True)
plt.xticks(np.arange(len(cnt)), val)
plt.show()
# using seaborn
sns.barplot( val, cnt )
Here's an extension of above solution using Matplotlib
as well as Seaborn
:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
lst = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
val, cnt = (zip(*lst))
val, cnt = list(val), list(cnt)
val, cnt
# (['you', 'i', 'we', 'my', 'he'], [7706, 6570, 2733, 2718, 2369])
# using Matplotlib
length = len(cnt)
plt.bar(np.arange(length), cnt, label=True)
plt.xticks(np.arange(len(cnt)), val)
plt.show()
# using seaborn
sns.barplot( val, cnt )
answered Nov 25 '18 at 7:37
dataLeodataLeo
6031419
6031419
1
Thank you, I would prefer seaborn but I had to use matplotlib for this particular exercise.
– Jacob Myer
Nov 25 '18 at 7:50
add a comment |
1
Thank you, I would prefer seaborn but I had to use matplotlib for this particular exercise.
– Jacob Myer
Nov 25 '18 at 7:50
1
1
Thank you, I would prefer seaborn but I had to use matplotlib for this particular exercise.
– Jacob Myer
Nov 25 '18 at 7:50
Thank you, I would prefer seaborn but I had to use matplotlib for this particular exercise.
– Jacob Myer
Nov 25 '18 at 7:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465421%2fhow-to-plot-histogram-in-matplotlib-when-data-is-in-tuples%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown