how to get ticks every hour?
Consider this simple example
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import matplotlib.dates as mdates
pd.__version__
Out[147]: u'0.22.0'
idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.Series(np.random.randn(len(idx)), index = idx)
df.head()
Out[145]:
2017-01-01 05:03:00 0.4361
2017-01-01 05:04:00 0.9737
2017-01-01 05:05:00 0.8430
2017-01-01 05:06:00 0.4292
2017-01-01 05:07:00 0.5739
Freq: T, dtype: float64
I want to plot this, and have ticks every hour. I use:
fig, ax = plt.subplots()
hours = mdates.HourLocator(interval = 1) #
h_fmt = mdates.DateFormatter('%H:%M:%S')
df.plot(ax = ax, color = 'black', linewidth = 0.4)
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)
which gives
why dont the ticks appear every hour here? Thanks for your help!
python pandas matplotlib
|
show 2 more comments
Consider this simple example
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import matplotlib.dates as mdates
pd.__version__
Out[147]: u'0.22.0'
idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.Series(np.random.randn(len(idx)), index = idx)
df.head()
Out[145]:
2017-01-01 05:03:00 0.4361
2017-01-01 05:04:00 0.9737
2017-01-01 05:05:00 0.8430
2017-01-01 05:06:00 0.4292
2017-01-01 05:07:00 0.5739
Freq: T, dtype: float64
I want to plot this, and have ticks every hour. I use:
fig, ax = plt.subplots()
hours = mdates.HourLocator(interval = 1) #
h_fmt = mdates.DateFormatter('%H:%M:%S')
df.plot(ax = ax, color = 'black', linewidth = 0.4)
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)
which gives
why dont the ticks appear every hour here? Thanks for your help!
python pandas matplotlib
You should play withxtick
.
– user32185
Feb 14 '18 at 15:32
do you know how to do it? thanks!!
– ℕʘʘḆḽḘ
Feb 14 '18 at 15:34
1
@ℕʘʘḆḽḘ: so your desired output is just to see spikes at every full hour?
– Skandix
Feb 14 '18 at 15:36
1
possibly relevant: stackoverflow.com/questions/45704366/…
– tmdavison
Feb 14 '18 at 15:42
@Skandix, yes, I want to see ticks not only at 8, 10, 12 but every hour at 6,7,8,9,10 etc
– ℕʘʘḆḽḘ
Feb 14 '18 at 15:44
|
show 2 more comments
Consider this simple example
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import matplotlib.dates as mdates
pd.__version__
Out[147]: u'0.22.0'
idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.Series(np.random.randn(len(idx)), index = idx)
df.head()
Out[145]:
2017-01-01 05:03:00 0.4361
2017-01-01 05:04:00 0.9737
2017-01-01 05:05:00 0.8430
2017-01-01 05:06:00 0.4292
2017-01-01 05:07:00 0.5739
Freq: T, dtype: float64
I want to plot this, and have ticks every hour. I use:
fig, ax = plt.subplots()
hours = mdates.HourLocator(interval = 1) #
h_fmt = mdates.DateFormatter('%H:%M:%S')
df.plot(ax = ax, color = 'black', linewidth = 0.4)
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)
which gives
why dont the ticks appear every hour here? Thanks for your help!
python pandas matplotlib
Consider this simple example
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import matplotlib.dates as mdates
pd.__version__
Out[147]: u'0.22.0'
idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.Series(np.random.randn(len(idx)), index = idx)
df.head()
Out[145]:
2017-01-01 05:03:00 0.4361
2017-01-01 05:04:00 0.9737
2017-01-01 05:05:00 0.8430
2017-01-01 05:06:00 0.4292
2017-01-01 05:07:00 0.5739
Freq: T, dtype: float64
I want to plot this, and have ticks every hour. I use:
fig, ax = plt.subplots()
hours = mdates.HourLocator(interval = 1) #
h_fmt = mdates.DateFormatter('%H:%M:%S')
df.plot(ax = ax, color = 'black', linewidth = 0.4)
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)
which gives
why dont the ticks appear every hour here? Thanks for your help!
python pandas matplotlib
python pandas matplotlib
edited Feb 14 '18 at 15:26
ℕʘʘḆḽḘ
asked Feb 14 '18 at 15:10
ℕʘʘḆḽḘℕʘʘḆḽḘ
6,940945102
6,940945102
You should play withxtick
.
– user32185
Feb 14 '18 at 15:32
do you know how to do it? thanks!!
– ℕʘʘḆḽḘ
Feb 14 '18 at 15:34
1
@ℕʘʘḆḽḘ: so your desired output is just to see spikes at every full hour?
– Skandix
Feb 14 '18 at 15:36
1
possibly relevant: stackoverflow.com/questions/45704366/…
– tmdavison
Feb 14 '18 at 15:42
@Skandix, yes, I want to see ticks not only at 8, 10, 12 but every hour at 6,7,8,9,10 etc
– ℕʘʘḆḽḘ
Feb 14 '18 at 15:44
|
show 2 more comments
You should play withxtick
.
– user32185
Feb 14 '18 at 15:32
do you know how to do it? thanks!!
– ℕʘʘḆḽḘ
Feb 14 '18 at 15:34
1
@ℕʘʘḆḽḘ: so your desired output is just to see spikes at every full hour?
– Skandix
Feb 14 '18 at 15:36
1
possibly relevant: stackoverflow.com/questions/45704366/…
– tmdavison
Feb 14 '18 at 15:42
@Skandix, yes, I want to see ticks not only at 8, 10, 12 but every hour at 6,7,8,9,10 etc
– ℕʘʘḆḽḘ
Feb 14 '18 at 15:44
You should play with
xtick
.– user32185
Feb 14 '18 at 15:32
You should play with
xtick
.– user32185
Feb 14 '18 at 15:32
do you know how to do it? thanks!!
– ℕʘʘḆḽḘ
Feb 14 '18 at 15:34
do you know how to do it? thanks!!
– ℕʘʘḆḽḘ
Feb 14 '18 at 15:34
1
1
@ℕʘʘḆḽḘ: so your desired output is just to see spikes at every full hour?
– Skandix
Feb 14 '18 at 15:36
@ℕʘʘḆḽḘ: so your desired output is just to see spikes at every full hour?
– Skandix
Feb 14 '18 at 15:36
1
1
possibly relevant: stackoverflow.com/questions/45704366/…
– tmdavison
Feb 14 '18 at 15:42
possibly relevant: stackoverflow.com/questions/45704366/…
– tmdavison
Feb 14 '18 at 15:42
@Skandix, yes, I want to see ticks not only at 8, 10, 12 but every hour at 6,7,8,9,10 etc
– ℕʘʘḆḽḘ
Feb 14 '18 at 15:44
@Skandix, yes, I want to see ticks not only at 8, 10, 12 but every hour at 6,7,8,9,10 etc
– ℕʘʘḆḽḘ
Feb 14 '18 at 15:44
|
show 2 more comments
1 Answer
1
active
oldest
votes
The problem is that while pandas in general directly wraps the matplotlib plotting methods, this is not the case for plots with dates. As soon as dates are involved, pandas uses a totally different numerical representation of dates and hence also uses its own locators for the ticks.
In case you want to use matplotlib.dates
formatters or locators on plots created with pandas you may use the x_compat=True
option in pandas plots.
df.plot(ax = ax, color = 'black', linewidth = 0.4, x_compat=True)
This allows to use the matplotlib.dates
formatters or locators as shown below.
Else you may replace df.plot(ax = ax, color = 'black', linewidth = 0.4)
by
ax.plot(df.index, df.values, color = 'black', linewidth = 0.4)
Complete example:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.Series(np.random.randn(len(idx)), index = idx)
fig, ax = plt.subplots()
hours = mdates.HourLocator(interval = 1)
h_fmt = mdates.DateFormatter('%H:%M:%S')
ax.plot(df.index, df.values, color = 'black', linewidth = 0.4)
#or use
df.plot(ax = ax, color = 'black', linewidth = 0.4, x_compat=True)
#Then tick and format with matplotlib:
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)
fig.autofmt_xdate()
plt.show()
If the motivation to use pandas here is (as stated in the comments below) to be able to use
secondary_y
, the equivalent for matplotlib plots would be a twin axes twinx
.import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.DataFrame(np.cumsum(np.random.randn(len(idx), 2),0),
index = idx, columns=list("AB"))
fig, ax = plt.subplots()
ax.plot(df.index, df["A"], color = 'black')
ax2 = ax.twinx()
ax2.plot(df.index, df["B"], color = 'indigo')
hours = mdates.HourLocator(interval = 1)
h_fmt = mdates.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)
fig.autofmt_xdate()
plt.show()
thanks. maybe a workaround to keep Pandas doing the plots would be to get a regular Timestamp index instead of a pandas datetime?
– ℕʘʘḆḽḘ
Feb 14 '18 at 16:22
I would like to keep the Pandas constructor because I use extensively thesecondary_y
argument of plot to show multiple times series...
– ℕʘʘḆḽḘ
Feb 14 '18 at 16:27
2
I can't imagine any workaround for this.matplotlib.dates
assumes the numeric axes to represent days since 0001-01-01 UTC, plus 1. Only if the axes uses this datetime format, it will correctly tick and label the axes. On the other hand pandas will create its axes units depending on the data and use an appropriate locator for those units. The only alternative I could think of would be to manipulate the pandas formatters in use. I am not aware of any coherent way of doing this though.
– ImportanceOfBeingErnest
Feb 14 '18 at 16:28
2
The equivalent of secondary_y in matplotlib is to create atwinx
and plot the second plot to that axes. This will in general cause 3 codelines compared to one, so it's not that bad.
– ImportanceOfBeingErnest
Feb 14 '18 at 16:30
1
Not sure what the problem is. Here the easiest would befig.autofmt_xdate(rotation=90)
.
– ImportanceOfBeingErnest
Feb 19 '18 at 2:19
|
show 5 more comments
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%2f48790378%2fhow-to-get-ticks-every-hour%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is that while pandas in general directly wraps the matplotlib plotting methods, this is not the case for plots with dates. As soon as dates are involved, pandas uses a totally different numerical representation of dates and hence also uses its own locators for the ticks.
In case you want to use matplotlib.dates
formatters or locators on plots created with pandas you may use the x_compat=True
option in pandas plots.
df.plot(ax = ax, color = 'black', linewidth = 0.4, x_compat=True)
This allows to use the matplotlib.dates
formatters or locators as shown below.
Else you may replace df.plot(ax = ax, color = 'black', linewidth = 0.4)
by
ax.plot(df.index, df.values, color = 'black', linewidth = 0.4)
Complete example:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.Series(np.random.randn(len(idx)), index = idx)
fig, ax = plt.subplots()
hours = mdates.HourLocator(interval = 1)
h_fmt = mdates.DateFormatter('%H:%M:%S')
ax.plot(df.index, df.values, color = 'black', linewidth = 0.4)
#or use
df.plot(ax = ax, color = 'black', linewidth = 0.4, x_compat=True)
#Then tick and format with matplotlib:
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)
fig.autofmt_xdate()
plt.show()
If the motivation to use pandas here is (as stated in the comments below) to be able to use
secondary_y
, the equivalent for matplotlib plots would be a twin axes twinx
.import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.DataFrame(np.cumsum(np.random.randn(len(idx), 2),0),
index = idx, columns=list("AB"))
fig, ax = plt.subplots()
ax.plot(df.index, df["A"], color = 'black')
ax2 = ax.twinx()
ax2.plot(df.index, df["B"], color = 'indigo')
hours = mdates.HourLocator(interval = 1)
h_fmt = mdates.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)
fig.autofmt_xdate()
plt.show()
thanks. maybe a workaround to keep Pandas doing the plots would be to get a regular Timestamp index instead of a pandas datetime?
– ℕʘʘḆḽḘ
Feb 14 '18 at 16:22
I would like to keep the Pandas constructor because I use extensively thesecondary_y
argument of plot to show multiple times series...
– ℕʘʘḆḽḘ
Feb 14 '18 at 16:27
2
I can't imagine any workaround for this.matplotlib.dates
assumes the numeric axes to represent days since 0001-01-01 UTC, plus 1. Only if the axes uses this datetime format, it will correctly tick and label the axes. On the other hand pandas will create its axes units depending on the data and use an appropriate locator for those units. The only alternative I could think of would be to manipulate the pandas formatters in use. I am not aware of any coherent way of doing this though.
– ImportanceOfBeingErnest
Feb 14 '18 at 16:28
2
The equivalent of secondary_y in matplotlib is to create atwinx
and plot the second plot to that axes. This will in general cause 3 codelines compared to one, so it's not that bad.
– ImportanceOfBeingErnest
Feb 14 '18 at 16:30
1
Not sure what the problem is. Here the easiest would befig.autofmt_xdate(rotation=90)
.
– ImportanceOfBeingErnest
Feb 19 '18 at 2:19
|
show 5 more comments
The problem is that while pandas in general directly wraps the matplotlib plotting methods, this is not the case for plots with dates. As soon as dates are involved, pandas uses a totally different numerical representation of dates and hence also uses its own locators for the ticks.
In case you want to use matplotlib.dates
formatters or locators on plots created with pandas you may use the x_compat=True
option in pandas plots.
df.plot(ax = ax, color = 'black', linewidth = 0.4, x_compat=True)
This allows to use the matplotlib.dates
formatters or locators as shown below.
Else you may replace df.plot(ax = ax, color = 'black', linewidth = 0.4)
by
ax.plot(df.index, df.values, color = 'black', linewidth = 0.4)
Complete example:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.Series(np.random.randn(len(idx)), index = idx)
fig, ax = plt.subplots()
hours = mdates.HourLocator(interval = 1)
h_fmt = mdates.DateFormatter('%H:%M:%S')
ax.plot(df.index, df.values, color = 'black', linewidth = 0.4)
#or use
df.plot(ax = ax, color = 'black', linewidth = 0.4, x_compat=True)
#Then tick and format with matplotlib:
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)
fig.autofmt_xdate()
plt.show()
If the motivation to use pandas here is (as stated in the comments below) to be able to use
secondary_y
, the equivalent for matplotlib plots would be a twin axes twinx
.import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.DataFrame(np.cumsum(np.random.randn(len(idx), 2),0),
index = idx, columns=list("AB"))
fig, ax = plt.subplots()
ax.plot(df.index, df["A"], color = 'black')
ax2 = ax.twinx()
ax2.plot(df.index, df["B"], color = 'indigo')
hours = mdates.HourLocator(interval = 1)
h_fmt = mdates.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)
fig.autofmt_xdate()
plt.show()
thanks. maybe a workaround to keep Pandas doing the plots would be to get a regular Timestamp index instead of a pandas datetime?
– ℕʘʘḆḽḘ
Feb 14 '18 at 16:22
I would like to keep the Pandas constructor because I use extensively thesecondary_y
argument of plot to show multiple times series...
– ℕʘʘḆḽḘ
Feb 14 '18 at 16:27
2
I can't imagine any workaround for this.matplotlib.dates
assumes the numeric axes to represent days since 0001-01-01 UTC, plus 1. Only if the axes uses this datetime format, it will correctly tick and label the axes. On the other hand pandas will create its axes units depending on the data and use an appropriate locator for those units. The only alternative I could think of would be to manipulate the pandas formatters in use. I am not aware of any coherent way of doing this though.
– ImportanceOfBeingErnest
Feb 14 '18 at 16:28
2
The equivalent of secondary_y in matplotlib is to create atwinx
and plot the second plot to that axes. This will in general cause 3 codelines compared to one, so it's not that bad.
– ImportanceOfBeingErnest
Feb 14 '18 at 16:30
1
Not sure what the problem is. Here the easiest would befig.autofmt_xdate(rotation=90)
.
– ImportanceOfBeingErnest
Feb 19 '18 at 2:19
|
show 5 more comments
The problem is that while pandas in general directly wraps the matplotlib plotting methods, this is not the case for plots with dates. As soon as dates are involved, pandas uses a totally different numerical representation of dates and hence also uses its own locators for the ticks.
In case you want to use matplotlib.dates
formatters or locators on plots created with pandas you may use the x_compat=True
option in pandas plots.
df.plot(ax = ax, color = 'black', linewidth = 0.4, x_compat=True)
This allows to use the matplotlib.dates
formatters or locators as shown below.
Else you may replace df.plot(ax = ax, color = 'black', linewidth = 0.4)
by
ax.plot(df.index, df.values, color = 'black', linewidth = 0.4)
Complete example:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.Series(np.random.randn(len(idx)), index = idx)
fig, ax = plt.subplots()
hours = mdates.HourLocator(interval = 1)
h_fmt = mdates.DateFormatter('%H:%M:%S')
ax.plot(df.index, df.values, color = 'black', linewidth = 0.4)
#or use
df.plot(ax = ax, color = 'black', linewidth = 0.4, x_compat=True)
#Then tick and format with matplotlib:
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)
fig.autofmt_xdate()
plt.show()
If the motivation to use pandas here is (as stated in the comments below) to be able to use
secondary_y
, the equivalent for matplotlib plots would be a twin axes twinx
.import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.DataFrame(np.cumsum(np.random.randn(len(idx), 2),0),
index = idx, columns=list("AB"))
fig, ax = plt.subplots()
ax.plot(df.index, df["A"], color = 'black')
ax2 = ax.twinx()
ax2.plot(df.index, df["B"], color = 'indigo')
hours = mdates.HourLocator(interval = 1)
h_fmt = mdates.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)
fig.autofmt_xdate()
plt.show()
The problem is that while pandas in general directly wraps the matplotlib plotting methods, this is not the case for plots with dates. As soon as dates are involved, pandas uses a totally different numerical representation of dates and hence also uses its own locators for the ticks.
In case you want to use matplotlib.dates
formatters or locators on plots created with pandas you may use the x_compat=True
option in pandas plots.
df.plot(ax = ax, color = 'black', linewidth = 0.4, x_compat=True)
This allows to use the matplotlib.dates
formatters or locators as shown below.
Else you may replace df.plot(ax = ax, color = 'black', linewidth = 0.4)
by
ax.plot(df.index, df.values, color = 'black', linewidth = 0.4)
Complete example:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.Series(np.random.randn(len(idx)), index = idx)
fig, ax = plt.subplots()
hours = mdates.HourLocator(interval = 1)
h_fmt = mdates.DateFormatter('%H:%M:%S')
ax.plot(df.index, df.values, color = 'black', linewidth = 0.4)
#or use
df.plot(ax = ax, color = 'black', linewidth = 0.4, x_compat=True)
#Then tick and format with matplotlib:
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)
fig.autofmt_xdate()
plt.show()
If the motivation to use pandas here is (as stated in the comments below) to be able to use
secondary_y
, the equivalent for matplotlib plots would be a twin axes twinx
.import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.DataFrame(np.cumsum(np.random.randn(len(idx), 2),0),
index = idx, columns=list("AB"))
fig, ax = plt.subplots()
ax.plot(df.index, df["A"], color = 'black')
ax2 = ax.twinx()
ax2.plot(df.index, df["B"], color = 'indigo')
hours = mdates.HourLocator(interval = 1)
h_fmt = mdates.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)
fig.autofmt_xdate()
plt.show()
edited Jun 6 '18 at 13:16
answered Feb 14 '18 at 16:14
ImportanceOfBeingErnestImportanceOfBeingErnest
130k13139215
130k13139215
thanks. maybe a workaround to keep Pandas doing the plots would be to get a regular Timestamp index instead of a pandas datetime?
– ℕʘʘḆḽḘ
Feb 14 '18 at 16:22
I would like to keep the Pandas constructor because I use extensively thesecondary_y
argument of plot to show multiple times series...
– ℕʘʘḆḽḘ
Feb 14 '18 at 16:27
2
I can't imagine any workaround for this.matplotlib.dates
assumes the numeric axes to represent days since 0001-01-01 UTC, plus 1. Only if the axes uses this datetime format, it will correctly tick and label the axes. On the other hand pandas will create its axes units depending on the data and use an appropriate locator for those units. The only alternative I could think of would be to manipulate the pandas formatters in use. I am not aware of any coherent way of doing this though.
– ImportanceOfBeingErnest
Feb 14 '18 at 16:28
2
The equivalent of secondary_y in matplotlib is to create atwinx
and plot the second plot to that axes. This will in general cause 3 codelines compared to one, so it's not that bad.
– ImportanceOfBeingErnest
Feb 14 '18 at 16:30
1
Not sure what the problem is. Here the easiest would befig.autofmt_xdate(rotation=90)
.
– ImportanceOfBeingErnest
Feb 19 '18 at 2:19
|
show 5 more comments
thanks. maybe a workaround to keep Pandas doing the plots would be to get a regular Timestamp index instead of a pandas datetime?
– ℕʘʘḆḽḘ
Feb 14 '18 at 16:22
I would like to keep the Pandas constructor because I use extensively thesecondary_y
argument of plot to show multiple times series...
– ℕʘʘḆḽḘ
Feb 14 '18 at 16:27
2
I can't imagine any workaround for this.matplotlib.dates
assumes the numeric axes to represent days since 0001-01-01 UTC, plus 1. Only if the axes uses this datetime format, it will correctly tick and label the axes. On the other hand pandas will create its axes units depending on the data and use an appropriate locator for those units. The only alternative I could think of would be to manipulate the pandas formatters in use. I am not aware of any coherent way of doing this though.
– ImportanceOfBeingErnest
Feb 14 '18 at 16:28
2
The equivalent of secondary_y in matplotlib is to create atwinx
and plot the second plot to that axes. This will in general cause 3 codelines compared to one, so it's not that bad.
– ImportanceOfBeingErnest
Feb 14 '18 at 16:30
1
Not sure what the problem is. Here the easiest would befig.autofmt_xdate(rotation=90)
.
– ImportanceOfBeingErnest
Feb 19 '18 at 2:19
thanks. maybe a workaround to keep Pandas doing the plots would be to get a regular Timestamp index instead of a pandas datetime?
– ℕʘʘḆḽḘ
Feb 14 '18 at 16:22
thanks. maybe a workaround to keep Pandas doing the plots would be to get a regular Timestamp index instead of a pandas datetime?
– ℕʘʘḆḽḘ
Feb 14 '18 at 16:22
I would like to keep the Pandas constructor because I use extensively the
secondary_y
argument of plot to show multiple times series...– ℕʘʘḆḽḘ
Feb 14 '18 at 16:27
I would like to keep the Pandas constructor because I use extensively the
secondary_y
argument of plot to show multiple times series...– ℕʘʘḆḽḘ
Feb 14 '18 at 16:27
2
2
I can't imagine any workaround for this.
matplotlib.dates
assumes the numeric axes to represent days since 0001-01-01 UTC, plus 1. Only if the axes uses this datetime format, it will correctly tick and label the axes. On the other hand pandas will create its axes units depending on the data and use an appropriate locator for those units. The only alternative I could think of would be to manipulate the pandas formatters in use. I am not aware of any coherent way of doing this though.– ImportanceOfBeingErnest
Feb 14 '18 at 16:28
I can't imagine any workaround for this.
matplotlib.dates
assumes the numeric axes to represent days since 0001-01-01 UTC, plus 1. Only if the axes uses this datetime format, it will correctly tick and label the axes. On the other hand pandas will create its axes units depending on the data and use an appropriate locator for those units. The only alternative I could think of would be to manipulate the pandas formatters in use. I am not aware of any coherent way of doing this though.– ImportanceOfBeingErnest
Feb 14 '18 at 16:28
2
2
The equivalent of secondary_y in matplotlib is to create a
twinx
and plot the second plot to that axes. This will in general cause 3 codelines compared to one, so it's not that bad.– ImportanceOfBeingErnest
Feb 14 '18 at 16:30
The equivalent of secondary_y in matplotlib is to create a
twinx
and plot the second plot to that axes. This will in general cause 3 codelines compared to one, so it's not that bad.– ImportanceOfBeingErnest
Feb 14 '18 at 16:30
1
1
Not sure what the problem is. Here the easiest would be
fig.autofmt_xdate(rotation=90)
.– ImportanceOfBeingErnest
Feb 19 '18 at 2:19
Not sure what the problem is. Here the easiest would be
fig.autofmt_xdate(rotation=90)
.– ImportanceOfBeingErnest
Feb 19 '18 at 2:19
|
show 5 more comments
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%2f48790378%2fhow-to-get-ticks-every-hour%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
You should play with
xtick
.– user32185
Feb 14 '18 at 15:32
do you know how to do it? thanks!!
– ℕʘʘḆḽḘ
Feb 14 '18 at 15:34
1
@ℕʘʘḆḽḘ: so your desired output is just to see spikes at every full hour?
– Skandix
Feb 14 '18 at 15:36
1
possibly relevant: stackoverflow.com/questions/45704366/…
– tmdavison
Feb 14 '18 at 15:42
@Skandix, yes, I want to see ticks not only at 8, 10, 12 but every hour at 6,7,8,9,10 etc
– ℕʘʘḆḽḘ
Feb 14 '18 at 15:44