python - change x axis using data frame column
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.)
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:
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.
Related videos on Youtube
jvalenti
Updated on June 04, 2022Comments
-
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:
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 followingplt.xticks(df['year'])
But the following happened:
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 ofdf['year']
along the x-axis.-
ImportanceOfBeingErnest about 6 yearsSure, there is. But its not clear how the dataframe looks like and what the desired output should be. See minimal reproducible example.
-
jvalenti about 6 yearsJust added example. Does this work?
-
-
jvalenti about 6 yearsthis works if i want to plot all data series in a dataframe right? I only want column 1 and column 2
-
romulomadu about 6 yearsyou can filt and plot only these columns by doing
df[['column1','column'2]].plot(...)
. -
jvalenti about 6 yearsdoes it matter which version of python? I am using 2.7.12
-
romulomadu about 6 yearsIt should work in both python 2 and 3, you got in an error?
-
jvalenti about 6 yearsThanks 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 likeplt.xticks(df.index,df["year"].unique.values)
you think? -
ImportanceOfBeingErnest about 6 yearsNo, the ticks and ticklabels need to have the same size. I updated the answer with how to use
unique
here.