Matplotlib pyplot savefig output has different data to show
I've got a loop for making a large number of charts by filtering a PANDAS dataframe:
def makeChart(name):
plt.clf()
plt.cla()
filename = "plots/" + name.replace(" ","_")
series = df[df['name'] == name]
series_wk = series[['name','start_date','num_nights']].groupby([pd.Grouper(key='start_date', freq='W')]).sum().reset_index()
series_wk['period'] = series_wk.apply(checkperiod, axis=1)
series_wk.set_index('start_date',inplace=True)
series_wk.to_csv(filename + ".csv")
ne = series_wk[series_wk['period'] == "Non-election"]
e = series_wk[series_wk['period'] == "Election"]
fig, ax = plt.subplots(figsize=(15,7))
ax.bar(ne.index, ne['num_nights'], color='b')
ax.bar(e.index, e['num_nights'], color='r')
ax.xaxis.set_major_locator(ticker.MultipleLocator(50))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
ax.set_title(name)
plt.xticks(rotation=90)
fig.savefig(filename)
plt.show()
makeChart("CARR Kim")
The output of plt.show() has the correct data, and looks like this
However the saved file has incorrect data, and looks like this.
I'm new to using matplotlib, and really can't figure out why the actual data points would differ here. Any help would be greatly appreciated!
python python-3.x pandas matplotlib
add a comment |
I've got a loop for making a large number of charts by filtering a PANDAS dataframe:
def makeChart(name):
plt.clf()
plt.cla()
filename = "plots/" + name.replace(" ","_")
series = df[df['name'] == name]
series_wk = series[['name','start_date','num_nights']].groupby([pd.Grouper(key='start_date', freq='W')]).sum().reset_index()
series_wk['period'] = series_wk.apply(checkperiod, axis=1)
series_wk.set_index('start_date',inplace=True)
series_wk.to_csv(filename + ".csv")
ne = series_wk[series_wk['period'] == "Non-election"]
e = series_wk[series_wk['period'] == "Election"]
fig, ax = plt.subplots(figsize=(15,7))
ax.bar(ne.index, ne['num_nights'], color='b')
ax.bar(e.index, e['num_nights'], color='r')
ax.xaxis.set_major_locator(ticker.MultipleLocator(50))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
ax.set_title(name)
plt.xticks(rotation=90)
fig.savefig(filename)
plt.show()
makeChart("CARR Kim")
The output of plt.show() has the correct data, and looks like this
However the saved file has incorrect data, and looks like this.
I'm new to using matplotlib, and really can't figure out why the actual data points would differ here. Any help would be greatly appreciated!
python python-3.x pandas matplotlib
ps - assume the function is properly indented, it broke when I pasted it in!
– Niklaus
Nov 13 '18 at 0:39
Would you mind providing your data file ?
– Patol75
Nov 13 '18 at 3:38
Both are correct and incorrect somehow. Maybe you're more lucky in the first case to see those bars you want to see. The underlying problem is that bars are too small to be shown. Either use a largerwidth
or set anedgecolor
explicitely.
– ImportanceOfBeingErnest
Nov 13 '18 at 12:17
@ImportanceOfBeingErnest setting an explicit width worked, thank you!
– Niklaus
Nov 14 '18 at 10:30
add a comment |
I've got a loop for making a large number of charts by filtering a PANDAS dataframe:
def makeChart(name):
plt.clf()
plt.cla()
filename = "plots/" + name.replace(" ","_")
series = df[df['name'] == name]
series_wk = series[['name','start_date','num_nights']].groupby([pd.Grouper(key='start_date', freq='W')]).sum().reset_index()
series_wk['period'] = series_wk.apply(checkperiod, axis=1)
series_wk.set_index('start_date',inplace=True)
series_wk.to_csv(filename + ".csv")
ne = series_wk[series_wk['period'] == "Non-election"]
e = series_wk[series_wk['period'] == "Election"]
fig, ax = plt.subplots(figsize=(15,7))
ax.bar(ne.index, ne['num_nights'], color='b')
ax.bar(e.index, e['num_nights'], color='r')
ax.xaxis.set_major_locator(ticker.MultipleLocator(50))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
ax.set_title(name)
plt.xticks(rotation=90)
fig.savefig(filename)
plt.show()
makeChart("CARR Kim")
The output of plt.show() has the correct data, and looks like this
However the saved file has incorrect data, and looks like this.
I'm new to using matplotlib, and really can't figure out why the actual data points would differ here. Any help would be greatly appreciated!
python python-3.x pandas matplotlib
I've got a loop for making a large number of charts by filtering a PANDAS dataframe:
def makeChart(name):
plt.clf()
plt.cla()
filename = "plots/" + name.replace(" ","_")
series = df[df['name'] == name]
series_wk = series[['name','start_date','num_nights']].groupby([pd.Grouper(key='start_date', freq='W')]).sum().reset_index()
series_wk['period'] = series_wk.apply(checkperiod, axis=1)
series_wk.set_index('start_date',inplace=True)
series_wk.to_csv(filename + ".csv")
ne = series_wk[series_wk['period'] == "Non-election"]
e = series_wk[series_wk['period'] == "Election"]
fig, ax = plt.subplots(figsize=(15,7))
ax.bar(ne.index, ne['num_nights'], color='b')
ax.bar(e.index, e['num_nights'], color='r')
ax.xaxis.set_major_locator(ticker.MultipleLocator(50))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
ax.set_title(name)
plt.xticks(rotation=90)
fig.savefig(filename)
plt.show()
makeChart("CARR Kim")
The output of plt.show() has the correct data, and looks like this
However the saved file has incorrect data, and looks like this.
I'm new to using matplotlib, and really can't figure out why the actual data points would differ here. Any help would be greatly appreciated!
python python-3.x pandas matplotlib
python python-3.x pandas matplotlib
edited Nov 13 '18 at 0:56
Niklaus
asked Nov 13 '18 at 0:38
NiklausNiklaus
12
12
ps - assume the function is properly indented, it broke when I pasted it in!
– Niklaus
Nov 13 '18 at 0:39
Would you mind providing your data file ?
– Patol75
Nov 13 '18 at 3:38
Both are correct and incorrect somehow. Maybe you're more lucky in the first case to see those bars you want to see. The underlying problem is that bars are too small to be shown. Either use a largerwidth
or set anedgecolor
explicitely.
– ImportanceOfBeingErnest
Nov 13 '18 at 12:17
@ImportanceOfBeingErnest setting an explicit width worked, thank you!
– Niklaus
Nov 14 '18 at 10:30
add a comment |
ps - assume the function is properly indented, it broke when I pasted it in!
– Niklaus
Nov 13 '18 at 0:39
Would you mind providing your data file ?
– Patol75
Nov 13 '18 at 3:38
Both are correct and incorrect somehow. Maybe you're more lucky in the first case to see those bars you want to see. The underlying problem is that bars are too small to be shown. Either use a largerwidth
or set anedgecolor
explicitely.
– ImportanceOfBeingErnest
Nov 13 '18 at 12:17
@ImportanceOfBeingErnest setting an explicit width worked, thank you!
– Niklaus
Nov 14 '18 at 10:30
ps - assume the function is properly indented, it broke when I pasted it in!
– Niklaus
Nov 13 '18 at 0:39
ps - assume the function is properly indented, it broke when I pasted it in!
– Niklaus
Nov 13 '18 at 0:39
Would you mind providing your data file ?
– Patol75
Nov 13 '18 at 3:38
Would you mind providing your data file ?
– Patol75
Nov 13 '18 at 3:38
Both are correct and incorrect somehow. Maybe you're more lucky in the first case to see those bars you want to see. The underlying problem is that bars are too small to be shown. Either use a larger
width
or set an edgecolor
explicitely.– ImportanceOfBeingErnest
Nov 13 '18 at 12:17
Both are correct and incorrect somehow. Maybe you're more lucky in the first case to see those bars you want to see. The underlying problem is that bars are too small to be shown. Either use a larger
width
or set an edgecolor
explicitely.– ImportanceOfBeingErnest
Nov 13 '18 at 12:17
@ImportanceOfBeingErnest setting an explicit width worked, thank you!
– Niklaus
Nov 14 '18 at 10:30
@ImportanceOfBeingErnest setting an explicit width worked, thank you!
– Niklaus
Nov 14 '18 at 10:30
add a comment |
0
active
oldest
votes
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%2f53272125%2fmatplotlib-pyplot-savefig-output-has-different-data-to-show%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53272125%2fmatplotlib-pyplot-savefig-output-has-different-data-to-show%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
ps - assume the function is properly indented, it broke when I pasted it in!
– Niklaus
Nov 13 '18 at 0:39
Would you mind providing your data file ?
– Patol75
Nov 13 '18 at 3:38
Both are correct and incorrect somehow. Maybe you're more lucky in the first case to see those bars you want to see. The underlying problem is that bars are too small to be shown. Either use a larger
width
or set anedgecolor
explicitely.– ImportanceOfBeingErnest
Nov 13 '18 at 12:17
@ImportanceOfBeingErnest setting an explicit width worked, thank you!
– Niklaus
Nov 14 '18 at 10:30