Python - MatPlotLib - How can I set two ticks on my x axis, one at the far left and one at the far right?How can I represent an 'Enum' in Python?How can I safely create a nested directory?How can I remove a trailing newline?How can I make a time delay in Python?How can you profile a Python script?How do I concatenate two lists in Python?Python, Matplotlib, subplot: How to set the axis range?setting y-axis limit in matplotlibHow can I reverse a list in Python?Changing the “tick frequency” on x or y axis in matplotlib?

A+ rating still unsecure by Google Chrome's opinion

Weird resistor with dots around it on the schematic

Is the Microsoft recommendation to use C# properties applicable to game development?

How would armour (and combat) change if the fighter didn't need to actually wear it?

Understanding a part of the proof that sequence that converges to square root of two is decreasing.

What modifiers are added to the attack and damage rolls of this unique longbow from Waterdeep: Dragon Heist?

What if a restaurant suddenly cannot accept credit cards, and the customer has no cash?

What is the opposite of "hunger level"?

100 Years of GCHQ - A quick afternoon puzzle!

Good way to stop electrolyte tabs from turning into powder?

Has the speed of light ever been measured in vacuum?

Are there liquid fueled rocket boosters having coaxial fuel/oxidizer tanks?

Will some rockets really collapse under their own weight?

What is a "soap"?

Duplicate and slide edge (rip from boundary)

Typesetting "hollow slash"

When does The Truman Show take place?

What should we do with manuals from the 80s?

Does the Haste spell's hasted action allow you to make multiple unarmed strikes? Or none at all?

How to prevent criminal gangs from making/buying guns?

Is this bar slide trick shown on Cheers real or a visual effect?

What should I do if actually I found a serious flaw in someone's PhD thesis and an article derived from that PhD thesis?

How do I answer an interview question about not meeting deadlines?

What are some tips and tricks for finding the cheapest flight when luggage and other fees are not revealed until far into the booking process?



Python - MatPlotLib - How can I set two ticks on my x axis, one at the far left and one at the far right?


How can I represent an 'Enum' in Python?How can I safely create a nested directory?How can I remove a trailing newline?How can I make a time delay in Python?How can you profile a Python script?How do I concatenate two lists in Python?Python, Matplotlib, subplot: How to set the axis range?setting y-axis limit in matplotlibHow can I reverse a list in Python?Changing the “tick frequency” on x or y axis in matplotlib?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















So I am trying to display some analytics in my python web-app. I want to display the date-time for the first and last entries on the x axis.



I have attempted to set the ticks as a two entry list but it gave a bad result.



plt.scatter(x_data, y_data)
plt.grid(True)
plt.xticks(x_data, [x_data[0], x_data[-1]])
fig = plt.gcf()


Where the x_data is:



['10:59:44 27-03-2019', '11:00:07 27-03-2019', '11:00:09 27-03-2019', '11:00:12 27-03-2019', '11:30:23 27-03-2019', '11:30:29 27-03-2019', '11:30:30 27-03-2019', '11:30:31 27-03-2019', '11:30:32 27-03-2019', '11:32:44 27-03-2019', '11:33:21 27-03-2019', '11:35:50 27-03-2019', '12:16:12 27-03-2019', '12:16:39 27-03-2019', '12:17:36 27-03-2019', '12:19:40 27-03-2019', '12:23:50 27-03-2019']


I expected the output to place one on the far left and the other on the far right. Instead the first two ticks simply showed the times overlapping.



I am unsure what the proper way of doing it is.










share|improve this question
























  • have you tried plt.xticks without the second argument? for setting axis limits there is plt.xlim funtion

    – alexey
    Mar 27 at 12:38











  • If I understand correctly you want all the ticks to show but only display the label for the first and last?

    – Gio
    Mar 27 at 12:39












  • Yes, I do want the other ticks to show but for the first and last to be labelled.

    – Thomas Coll
    Mar 27 at 12:41

















2















So I am trying to display some analytics in my python web-app. I want to display the date-time for the first and last entries on the x axis.



I have attempted to set the ticks as a two entry list but it gave a bad result.



plt.scatter(x_data, y_data)
plt.grid(True)
plt.xticks(x_data, [x_data[0], x_data[-1]])
fig = plt.gcf()


Where the x_data is:



['10:59:44 27-03-2019', '11:00:07 27-03-2019', '11:00:09 27-03-2019', '11:00:12 27-03-2019', '11:30:23 27-03-2019', '11:30:29 27-03-2019', '11:30:30 27-03-2019', '11:30:31 27-03-2019', '11:30:32 27-03-2019', '11:32:44 27-03-2019', '11:33:21 27-03-2019', '11:35:50 27-03-2019', '12:16:12 27-03-2019', '12:16:39 27-03-2019', '12:17:36 27-03-2019', '12:19:40 27-03-2019', '12:23:50 27-03-2019']


I expected the output to place one on the far left and the other on the far right. Instead the first two ticks simply showed the times overlapping.



I am unsure what the proper way of doing it is.










share|improve this question
























  • have you tried plt.xticks without the second argument? for setting axis limits there is plt.xlim funtion

    – alexey
    Mar 27 at 12:38











  • If I understand correctly you want all the ticks to show but only display the label for the first and last?

    – Gio
    Mar 27 at 12:39












  • Yes, I do want the other ticks to show but for the first and last to be labelled.

    – Thomas Coll
    Mar 27 at 12:41













2












2








2








So I am trying to display some analytics in my python web-app. I want to display the date-time for the first and last entries on the x axis.



I have attempted to set the ticks as a two entry list but it gave a bad result.



plt.scatter(x_data, y_data)
plt.grid(True)
plt.xticks(x_data, [x_data[0], x_data[-1]])
fig = plt.gcf()


Where the x_data is:



['10:59:44 27-03-2019', '11:00:07 27-03-2019', '11:00:09 27-03-2019', '11:00:12 27-03-2019', '11:30:23 27-03-2019', '11:30:29 27-03-2019', '11:30:30 27-03-2019', '11:30:31 27-03-2019', '11:30:32 27-03-2019', '11:32:44 27-03-2019', '11:33:21 27-03-2019', '11:35:50 27-03-2019', '12:16:12 27-03-2019', '12:16:39 27-03-2019', '12:17:36 27-03-2019', '12:19:40 27-03-2019', '12:23:50 27-03-2019']


I expected the output to place one on the far left and the other on the far right. Instead the first two ticks simply showed the times overlapping.



I am unsure what the proper way of doing it is.










share|improve this question














So I am trying to display some analytics in my python web-app. I want to display the date-time for the first and last entries on the x axis.



I have attempted to set the ticks as a two entry list but it gave a bad result.



plt.scatter(x_data, y_data)
plt.grid(True)
plt.xticks(x_data, [x_data[0], x_data[-1]])
fig = plt.gcf()


Where the x_data is:



['10:59:44 27-03-2019', '11:00:07 27-03-2019', '11:00:09 27-03-2019', '11:00:12 27-03-2019', '11:30:23 27-03-2019', '11:30:29 27-03-2019', '11:30:30 27-03-2019', '11:30:31 27-03-2019', '11:30:32 27-03-2019', '11:32:44 27-03-2019', '11:33:21 27-03-2019', '11:35:50 27-03-2019', '12:16:12 27-03-2019', '12:16:39 27-03-2019', '12:17:36 27-03-2019', '12:19:40 27-03-2019', '12:23:50 27-03-2019']


I expected the output to place one on the far left and the other on the far right. Instead the first two ticks simply showed the times overlapping.



I am unsure what the proper way of doing it is.







python matplotlib






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 12:34









Thomas CollThomas Coll

154 bronze badges




154 bronze badges















  • have you tried plt.xticks without the second argument? for setting axis limits there is plt.xlim funtion

    – alexey
    Mar 27 at 12:38











  • If I understand correctly you want all the ticks to show but only display the label for the first and last?

    – Gio
    Mar 27 at 12:39












  • Yes, I do want the other ticks to show but for the first and last to be labelled.

    – Thomas Coll
    Mar 27 at 12:41

















  • have you tried plt.xticks without the second argument? for setting axis limits there is plt.xlim funtion

    – alexey
    Mar 27 at 12:38











  • If I understand correctly you want all the ticks to show but only display the label for the first and last?

    – Gio
    Mar 27 at 12:39












  • Yes, I do want the other ticks to show but for the first and last to be labelled.

    – Thomas Coll
    Mar 27 at 12:41
















have you tried plt.xticks without the second argument? for setting axis limits there is plt.xlim funtion

– alexey
Mar 27 at 12:38





have you tried plt.xticks without the second argument? for setting axis limits there is plt.xlim funtion

– alexey
Mar 27 at 12:38













If I understand correctly you want all the ticks to show but only display the label for the first and last?

– Gio
Mar 27 at 12:39






If I understand correctly you want all the ticks to show but only display the label for the first and last?

– Gio
Mar 27 at 12:39














Yes, I do want the other ticks to show but for the first and last to be labelled.

– Thomas Coll
Mar 27 at 12:41





Yes, I do want the other ticks to show but for the first and last to be labelled.

– Thomas Coll
Mar 27 at 12:41












2 Answers
2






active

oldest

votes


















2














Here is a workaround solution using some random data because you did not specify what the y_data is. The idea is to loop through the existing default tick labels and then replace all the tick labels by empty strings except the first and the last one.



fig, ax = plt.subplots()

plt.scatter(x_data, range(len(x_data)))
plt.grid(True)

fig.canvas.draw()
xticks = ax.get_xticklabels()
labels_new = [lab.get_text() if i in [0, len(xticks)-1] else ""
for i, lab in enumerate(xticks) ]

ax.set_xticklabels(labels_new)

plt.show()


enter image description here






share|improve this answer



























  • It's a good workaround. If there is no official way then this will do. Thank you!

    – Thomas Coll
    Mar 27 at 14:44


















1














I suspect your problem is that you are defining a vector of ticks and then assigning two labels (in position 0 and 1).



import numpy as np
import matplotlib.pyplot as plt

x_data=np.array([1,2,3,4,5,6,7,8])
y_data=np.random.normal(0, 1, (8, 1))

# create an array for your labels
new_labels=[''] * (len(x_data)-2)
new_labels.insert(0,x_data[0])
new_labels.append(x_data[-1])

plt.scatter(x_data, y_data)
plt.grid(True)
plt.xticks(x_data, new_labels) #use the new tick labels
fig = plt.gcf()





share|improve this answer



























  • No this leads to an messed up output (oi65.tinypic.com/2j5qm9.jpg)

    – Thomas Coll
    Mar 27 at 14:17












  • yes, of course. I added a list as second label =). Now it should work

    – Gio
    Mar 27 at 16:54













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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55377331%2fpython-matplotlib-how-can-i-set-two-ticks-on-my-x-axis-one-at-the-far-left%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Here is a workaround solution using some random data because you did not specify what the y_data is. The idea is to loop through the existing default tick labels and then replace all the tick labels by empty strings except the first and the last one.



fig, ax = plt.subplots()

plt.scatter(x_data, range(len(x_data)))
plt.grid(True)

fig.canvas.draw()
xticks = ax.get_xticklabels()
labels_new = [lab.get_text() if i in [0, len(xticks)-1] else ""
for i, lab in enumerate(xticks) ]

ax.set_xticklabels(labels_new)

plt.show()


enter image description here






share|improve this answer



























  • It's a good workaround. If there is no official way then this will do. Thank you!

    – Thomas Coll
    Mar 27 at 14:44















2














Here is a workaround solution using some random data because you did not specify what the y_data is. The idea is to loop through the existing default tick labels and then replace all the tick labels by empty strings except the first and the last one.



fig, ax = plt.subplots()

plt.scatter(x_data, range(len(x_data)))
plt.grid(True)

fig.canvas.draw()
xticks = ax.get_xticklabels()
labels_new = [lab.get_text() if i in [0, len(xticks)-1] else ""
for i, lab in enumerate(xticks) ]

ax.set_xticklabels(labels_new)

plt.show()


enter image description here






share|improve this answer



























  • It's a good workaround. If there is no official way then this will do. Thank you!

    – Thomas Coll
    Mar 27 at 14:44













2












2








2







Here is a workaround solution using some random data because you did not specify what the y_data is. The idea is to loop through the existing default tick labels and then replace all the tick labels by empty strings except the first and the last one.



fig, ax = plt.subplots()

plt.scatter(x_data, range(len(x_data)))
plt.grid(True)

fig.canvas.draw()
xticks = ax.get_xticklabels()
labels_new = [lab.get_text() if i in [0, len(xticks)-1] else ""
for i, lab in enumerate(xticks) ]

ax.set_xticklabels(labels_new)

plt.show()


enter image description here






share|improve this answer















Here is a workaround solution using some random data because you did not specify what the y_data is. The idea is to loop through the existing default tick labels and then replace all the tick labels by empty strings except the first and the last one.



fig, ax = plt.subplots()

plt.scatter(x_data, range(len(x_data)))
plt.grid(True)

fig.canvas.draw()
xticks = ax.get_xticklabels()
labels_new = [lab.get_text() if i in [0, len(xticks)-1] else ""
for i, lab in enumerate(xticks) ]

ax.set_xticklabels(labels_new)

plt.show()


enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 27 at 14:27

























answered Mar 27 at 14:18









SheldoreSheldore

21.9k5 gold badges15 silver badges37 bronze badges




21.9k5 gold badges15 silver badges37 bronze badges















  • It's a good workaround. If there is no official way then this will do. Thank you!

    – Thomas Coll
    Mar 27 at 14:44

















  • It's a good workaround. If there is no official way then this will do. Thank you!

    – Thomas Coll
    Mar 27 at 14:44
















It's a good workaround. If there is no official way then this will do. Thank you!

– Thomas Coll
Mar 27 at 14:44





It's a good workaround. If there is no official way then this will do. Thank you!

– Thomas Coll
Mar 27 at 14:44













1














I suspect your problem is that you are defining a vector of ticks and then assigning two labels (in position 0 and 1).



import numpy as np
import matplotlib.pyplot as plt

x_data=np.array([1,2,3,4,5,6,7,8])
y_data=np.random.normal(0, 1, (8, 1))

# create an array for your labels
new_labels=[''] * (len(x_data)-2)
new_labels.insert(0,x_data[0])
new_labels.append(x_data[-1])

plt.scatter(x_data, y_data)
plt.grid(True)
plt.xticks(x_data, new_labels) #use the new tick labels
fig = plt.gcf()





share|improve this answer



























  • No this leads to an messed up output (oi65.tinypic.com/2j5qm9.jpg)

    – Thomas Coll
    Mar 27 at 14:17












  • yes, of course. I added a list as second label =). Now it should work

    – Gio
    Mar 27 at 16:54















1














I suspect your problem is that you are defining a vector of ticks and then assigning two labels (in position 0 and 1).



import numpy as np
import matplotlib.pyplot as plt

x_data=np.array([1,2,3,4,5,6,7,8])
y_data=np.random.normal(0, 1, (8, 1))

# create an array for your labels
new_labels=[''] * (len(x_data)-2)
new_labels.insert(0,x_data[0])
new_labels.append(x_data[-1])

plt.scatter(x_data, y_data)
plt.grid(True)
plt.xticks(x_data, new_labels) #use the new tick labels
fig = plt.gcf()





share|improve this answer



























  • No this leads to an messed up output (oi65.tinypic.com/2j5qm9.jpg)

    – Thomas Coll
    Mar 27 at 14:17












  • yes, of course. I added a list as second label =). Now it should work

    – Gio
    Mar 27 at 16:54













1












1








1







I suspect your problem is that you are defining a vector of ticks and then assigning two labels (in position 0 and 1).



import numpy as np
import matplotlib.pyplot as plt

x_data=np.array([1,2,3,4,5,6,7,8])
y_data=np.random.normal(0, 1, (8, 1))

# create an array for your labels
new_labels=[''] * (len(x_data)-2)
new_labels.insert(0,x_data[0])
new_labels.append(x_data[-1])

plt.scatter(x_data, y_data)
plt.grid(True)
plt.xticks(x_data, new_labels) #use the new tick labels
fig = plt.gcf()





share|improve this answer















I suspect your problem is that you are defining a vector of ticks and then assigning two labels (in position 0 and 1).



import numpy as np
import matplotlib.pyplot as plt

x_data=np.array([1,2,3,4,5,6,7,8])
y_data=np.random.normal(0, 1, (8, 1))

# create an array for your labels
new_labels=[''] * (len(x_data)-2)
new_labels.insert(0,x_data[0])
new_labels.append(x_data[-1])

plt.scatter(x_data, y_data)
plt.grid(True)
plt.xticks(x_data, new_labels) #use the new tick labels
fig = plt.gcf()






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 27 at 16:54

























answered Mar 27 at 12:52









GioGio

1,9181 gold badge13 silver badges24 bronze badges




1,9181 gold badge13 silver badges24 bronze badges















  • No this leads to an messed up output (oi65.tinypic.com/2j5qm9.jpg)

    – Thomas Coll
    Mar 27 at 14:17












  • yes, of course. I added a list as second label =). Now it should work

    – Gio
    Mar 27 at 16:54

















  • No this leads to an messed up output (oi65.tinypic.com/2j5qm9.jpg)

    – Thomas Coll
    Mar 27 at 14:17












  • yes, of course. I added a list as second label =). Now it should work

    – Gio
    Mar 27 at 16:54
















No this leads to an messed up output (oi65.tinypic.com/2j5qm9.jpg)

– Thomas Coll
Mar 27 at 14:17






No this leads to an messed up output (oi65.tinypic.com/2j5qm9.jpg)

– Thomas Coll
Mar 27 at 14:17














yes, of course. I added a list as second label =). Now it should work

– Gio
Mar 27 at 16:54





yes, of course. I added a list as second label =). Now it should work

– Gio
Mar 27 at 16:54

















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55377331%2fpython-matplotlib-how-can-i-set-two-ticks-on-my-x-axis-one-at-the-far-left%23new-answer', 'question_page');

);

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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript