-1

I'm trying to add text labels to a simple matplotlib chart and nothing I've tried has worked.

plt.plot(SpringLikes)
plt.xlabel("Event")
labels=("SpringThemes")
plt.ylabel("No. of Likes")
plt.show()

enter image description here

It's a simple plot of the # of FB Likes per Event. I want the tick marks on the X axis to be the names of those events, which have been saved from a pandas Dataframe into the variable SpringThemes.

Any idea why this is not displaying the themes as the tickmark labels on the x axis?

martianwars
  • 6,380
  • 5
  • 35
  • 44
Andrew Smith
  • 539
  • 4
  • 8
  • 15
  • 1
    FYI, simply defining a local variable 'labels' will not affect your plot. It is functionally the same for you to write `any_variable_name_here=("SpringThemes")` – Leo Nov 23 '16 at 19:00
  • I see. So how could I take the text objects in my variable "SpringThemes" and use that to label the tick marks on the x axis? – Andrew Smith Nov 23 '16 at 19:07
  • Possible duplicate of [Matplotlib: How to put individual tags for a scatter plot](http://stackoverflow.com/questions/5147112/matplotlib-how-to-put-individual-tags-for-a-scatter-plot) – Leo Nov 23 '16 at 19:15

1 Answers1

1

Let's say for example you have a list for your x axis:

x = [1,2,3,4]

You can add ticks on it like this:

plt.xticks([Game 1, Game 2, Game 3,Game 4])

Thus changing the x axis label.

Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44
  • I don't think that helps me. I'm not looking to create a title for the chart, I'm looking to re-label the ticks on the x axis as the events themselves (i.e., "Game 1, Game 2, Game 3, etc..). I have the names of the events stored in a variable called "SpringThemes," but I can't figure out how to replace the generic numbers on the x axis with the text in that variable. – Andrew Smith Nov 23 '16 at 19:06
  • You should have told that on your question also put up the whole code. – Taufiq Rahman Nov 23 '16 at 19:11
  • Check my answer.Hope it helps you :-) – Taufiq Rahman Nov 23 '16 at 19:15
  • Very helpful! One more thing: I'd like to be able to generate those labels using my SpringThemes variable. But when I add the line plt.xtics[(SpringThemes)] I get an error saying "tuple object not callable." I know this is a dumb/newb question, but how can I get the plot to label the x ticks using the text in that SpringThemes variable? – Andrew Smith Nov 23 '16 at 19:21
  • That's not possible since you need to put ticks on each number,say you got 4 numbers then you need to add four ticks as you can see. – Taufiq Rahman Nov 23 '16 at 19:26
  • It sounds like I need to revise my entire plotting script. I was just doing a simple plt.plot(SpringLikes) rather than identifying x and y variables. When I added your code to mine, I still got the 'tuple object not callable' error – Andrew Smith Nov 23 '16 at 19:35
  • your x variable is something like this right? : x = [2,4,6,8] .It's a good idea to store them in a list like that. – Taufiq Rahman Nov 23 '16 at 19:38
  • I'm using two variables, "SpringLikes" and "SpringThemes" The SpringLikes is a list of the total number of likes per event. The SpringThemes is a list of the NAMES of the events (which I would like to display as the tick marks). – Andrew Smith Nov 23 '16 at 19:57
  • @AndrewSmith have you tried `plt.xticks(SpringThemes)` i.e. without brackets – Leo Nov 23 '16 at 20:29
  • @Leo Yes. When I do, I get the following: TypeError: 'tuple' object is not callable – Andrew Smith Nov 23 '16 at 23:16