Search code examples
csvmatplotlibgraphing

Changing x values


I am plotting values from a CSV and i was just wondering if i could show the x values as something else.

for example:

My code is:

from cycler import cycler 
import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker


fig = plt.figure()

df = pd.read_csv('CSV_GM_NB_1_0_Functional_Initial_5_pt.csv', skiprows=8)

data1 = df.ix[:,19:49].T
data2 = df.ix[:,50:80].T
data3 = df.ix[:,81:115].T
data1.columns=df['SN']
data2.columns=df['SN']
data3.columns=df['SN']


ax1 = plt.subplot2grid((6,6), (0,0), rowspan=1, colspan=5)
plt.title('GM_NB')
plt.ylabel('PV')
ax2 = plt.subplot2grid((6,6), (1,0), rowspan=1, colspan=5)
plt.ylabel('PV')
ax3 = plt.subplot2grid((6,6), (2,0), rowspan=4, colspan=5)
plt.ylabel('Point Values')
plt.xlabel('DID')

ax1.plot(df.ix[:,19:49].T)
ax2.plot(df.ix[:,50:80].T)
ax3.plot(df.ix[:,81:115].T)

plt.subplots_adjust(hspace=1.0)
plt.show()

Here is the output: Output

Question

As you can see the x-values of the subplots are linear and increasing from 35 to 135. i was wondering i could simply show these values starting at 0 and going to 100. (i cannot change the values inside the CSV and i cannot change the code because the values of 35-135 have a corrisponding y value.

for specifically, i need the same y value, but visually i was wondering if i could change the x values to start at 0 without pulling different y values from the code.

was maybe thinking if there was a function such as

plt.xvalues(subtract 35)

does this make sense? thanks.

Just some way to show different values that are being read from the graph


Solution

  • Let's consider the following example where x data goes from 35 to 135.

    import matplotlib.pyplot as plt
    import numpy as np
    plt.rcParams["figure.figsize"] = 4,2
    
    #Real x data goes from 35 to 135
    x = np.linspace(35,135, num=151)
    y = np.sinc(np.linspace(-3*np.pi,3*np.pi, num=151))
    plt.plot(x,y)
    

    enter image description here

    Now you want to change the scale.

    Method 1: Change the data

    It's as easy as it sounds, if you want to change the scale by 35, just subtract 35 from x:

    #Method 1: change the x data
    x2 = x - 35
    plt.plot(x2,y)
    

    enter image description here

    Method 2: Change the ticklabels

    We can also change the ticklabels. Therefore we would first need to set the tick locatation to fixed values, and set the ticklabels to some other invented values

    #Method2:
    # plot original data
    plt.plot(x,y)
    # set original ticks
    ticks = np.arange(35,140,20) # [ 35  55  75  95 115 135]
    # set ticks to original ticks, 
    # but tickabels (second argument) to something different
    plt.xticks(ticks, ticks-35)
    

    The output is exactly the same as in the first case

    enter image description here