2

I have a data frame, df I used to produce a plot of two series like so:

year = [2002, 2002, 2002, 2002]
month = ['Jan', 'Feb', 'Mar', 'Apr']
column1 = [3.3, 3.0, 3.1, 3.2, 2.9]
column2 = [7.0, 7.1, 7.3, 6.9, 7.3]
Dataset = list(zip(year, month, column1, column2))
df = DataFrame(data = Dataset, columns = ['year', 'month', 'column1', 'column2'])
df['column1'].plot(legend = True, label = 'column1')
df['column2'].plot(legend = True, label = 'column2', title = \
"Figure 1", style = '--', linewidth = 2.5)

Which produces the following:

enter image description here

I also have a column in my dataframe, df['year'] that has values that I would like to go along the x-axis. I tried the following

plt.xticks(df['year'])

But the following happened:

enter image description here

Is there a way to use the column df['year'] and have its values as the x axis tick marks without manually listing them? I would like the final version to look like the first plot but with the unique values of df['year'] along the x-axis.

2
  • 2
    Sure, there is. But its not clear how the dataframe looks like and what the desired output should be. See minimal reproducible example. Commented Feb 6, 2018 at 0:33
  • Just added example. Does this work? Commented Feb 6, 2018 at 0:59

2 Answers 2

5

To set the ticklabels to the values of some dataframe column, you would need to set the tickpositions to the index of the dataframe and the labels as the values from said column.

plt.xticks(df.index,df["year"].values)

Complete example:

from pandas import DataFrame
import matplotlib.pyplot as plt

year = [2002, 2002, 2002, 2002]
month = ['Jan', 'Feb', 'Mar', 'Apr']
column1 = [3.3, 3.0, 3.1, 3.2, 2.9]
column2 = [7.0, 7.1, 7.3, 6.9, 7.3]
Dataset = list(zip(year, month, column1, column2))
df = DataFrame(data = Dataset, columns = ['year', 'month', 'column1', 'column2'])
df['column1'].plot(legend = True, label = 'column1')
df['column2'].plot(legend = True, label = 'column2', title = \
      "Figure 1", style = '--', linewidth = 2.5)

plt.xticks(df.index,df["year"].values)

plt.show()

This shows of course all labels as 2002, since all values from the year column are 2002. (Not sure if that makes sense though.)

enter image description here

If you wanted to only label the first occurance of each year, you could use the unique years as follows

unique_years, ind = np.unique(df["year"].values,return_index=True)
plt.xticks(df.index[ind], unique_years)

resulting in something like this:

enter image description here

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

2 Comments

Thanks this is great! The data used for the example is basically just df.head(). What I really want is to only show the year on the x-axis during the month of January, so it shows when a new year has begun. could I use something like plt.xticks(df.index,df["year"].unique.values) you think?
No, the ticks and ticklabels need to have the same size. I updated the answer with how to use unique here.
1

You can set 'year' columns as index first:

df.set_index('year')

than you can use pandas to plot:

df[['column1','column2']].plot(title = 'Figure 1', legend = True, style = ['-','--'], linewidth = 2.5)
plt.show()

Pandas will print both series in same graph with index 'year' as x axis, the columns names are automatically attributed as lines labels.

4 Comments

this works if i want to plot all data series in a dataframe right? I only want column 1 and column 2
you can filt and plot only these columns by doing df[['column1','column'2]].plot(...).
does it matter which version of python? I am using 2.7.12
It should work in both python 2 and 3, you got in an error?

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.