edachan
edachan

Reputation: 33

Pandas: Plot X-axis values from dataframe columns and Y-axis values from dataframe rows

I have this dataset, where columns are values across the years and rows values in Sterling Pounds:

         total_2013  total2014  total_2015  total_2016
0         1569000    1614000     1644000     1659000
1          330000     423000      474000      540000
2         2100000    2080000     2093000     2135000
3         2161000    2238000     2221000     2200000
4         1865000    1975000     2046000     2152000
5         1903000    1972000     1970000     2034000
6         2087000    2091000     1963000     1956000
7         1237000    1231000     1199000     1188000
8         1043000    1072000     1076000     1059000
9          569000     610000      564000      592000
10        2207000    2287000     2191000     2274000
11        1908000    1908000     1917000     1908000

I need to plot in the X-axis the columns total_2013, total_2014, total_2015, total_2016.

In the Y-axis, I need to draw a point for the single values of each row and draw a line between these values along the years 2013-2016.

I couldn't make work xticks to put the values in the columns as different time points in the X-axis. Not sure if that's the right thing to do neither.

I have plotted so far this:

import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
matplotlib.style.use('ggplot')
ax = data_merged_years.plot(kind='line', title ="UK Schools budget per year 2013-2016", figsize=(15, 10), legend=True, fontsize=12)
ax.set_xlabel("Year", fontsize=12)
ax.set_ylabel("Pounds", fontsize=12)
plt.show()

My attempt: Wrong plot with incorrect X and Y:
My attempt: Wrong plot with incorrect X and Y

Upvotes: 1

Views: 6354

Answers (1)

Sebastian Wozny
Sebastian Wozny

Reputation: 17526

With your script the output I can produce is this: enter image description here

If you want the columns as x-axis rather than different lines you could transpose the DataFrame before plotting:

In [13]: import matplotlib.pyplot as plt
    ...: import pandas as pd
    ...: matplotlib.style.use('ggplot')
    ...: ax = data_merged_years.transpose().plot(kind='line', title ="UK Schools budget per year 2013-2016", figsize=(15, 10), legend=True, fontsize=12)
    ...: ax.set_xlabel("Year", fontsize=12)
    ...: ax.set_ylabel("Pounds", fontsize=12)
    ...: plt.show()
    ...: 

enter image description here

From your original plot it seems that you would have over ten thousand lines in one plot. Are you sure that is what you want?

Upvotes: 2

Related Questions