python - change x axis using data frame column

12,783

Solution 1

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

Solution 2

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.

Share:
12,783

Related videos on Youtube

jvalenti
Author by

jvalenti

Updated on June 04, 2022

Comments

  • jvalenti
    jvalenti almost 2 years

    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.

    • ImportanceOfBeingErnest
      ImportanceOfBeingErnest about 6 years
      Sure, there is. But its not clear how the dataframe looks like and what the desired output should be. See minimal reproducible example.
    • jvalenti
      jvalenti about 6 years
      Just added example. Does this work?
  • jvalenti
    jvalenti about 6 years
    this works if i want to plot all data series in a dataframe right? I only want column 1 and column 2
  • romulomadu
    romulomadu about 6 years
    you can filt and plot only these columns by doing df[['column1','column'2]].plot(...).
  • jvalenti
    jvalenti about 6 years
    does it matter which version of python? I am using 2.7.12
  • romulomadu
    romulomadu about 6 years
    It should work in both python 2 and 3, you got in an error?
  • jvalenti
    jvalenti about 6 years
    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?
  • ImportanceOfBeingErnest
    ImportanceOfBeingErnest about 6 years
    No, the ticks and ticklabels need to have the same size. I updated the answer with how to use unique here.