1

I have a dataframe (df) and two series i've sliced from that dataframe.

X is for example 2014-10 | 2014-11 Y is for example numbers like 123, 345, 678, and I want to graph like this:

700
                                 *
500

300
                     *
100     *
      2014-10      2014-11    2014-12

My code:

xlist = df.month #series
x_string = str(xlist) #string
ylist = df.numbers #series
y_string = str(ylist) #string
plt.xticks(x,x_string) #set to use my series as the x axis values
plt.plot(x, ylist, 'bo') #plot x(x_string) and y(ylist) axis on the graph.
plt.show() #show my graph

I've tried plotting both the series and the strings but neither are working.

for now It's been suggested I ust don't use xticks, and I can get:

df.plot(x='month',
            y='numbers',
            title='Time dependency of ...')

1 Answer 1

2

You can do this directly in pandas:

df.plot(x='month', y='numbers', title='Time dependency of ...')

or:

df[['month', 'numbers']].set_index('month').plot(title='Time dependency of ...')

This is assuming that df is a data frame with a 'month' and a 'numbers' column.

Sign up to request clarification or add additional context in comments.

8 Comments

do I set index on xlist (the series) or x_string (the string) or something else?
X would be the name(string) of the column in your data frame that contain the dates. I assume df is a data frame with an X and a Y column. If there are more columns use `df[['X', 'Y']]
ah so the set_index is basically saying use the month_date as the x axis. If I wanted to do other things such as set a Y axis label, change the color of the line before plotting how I one write that? would I call plt.set_axis('X"), then plt.setcolor, then plt.ylabel = 'text' as an example?
Kind of, you can print df.set_index to see what happens. Per default pandas uses the index as the x axis. Take a look at the docs, but you can also customize the plot as you normally do it in matplotlib.
thanks, sorry I've never used matplotlib, so to customise the plot you have to preface all the variables with plt. Like plt.SomeDataframe then plt.yaxis label = something?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.