I have the above dataset. I need to plot a graph where the starting point of y-axis needs to be 0, irrespective of the values in the dataset. The x-axis is the index(Time) and y-axis is Jolt 1, Jolt 2,...
I have a graph showing 2 different plots (the blue curve starts at y=0). I want the orange plot to start from 0 too to compare the trends visually.
Here is the code
import pandas as pd
import matplotlib.pyplot as plt
df.set_index('Time').plot()
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
How do I modify the code to get the desired output?
You can set the 'Time'
column as the index of your dataframe:
df = df.set_index('Time')
then you can loop over the remaining columns and plot them with respect to the dataframe index. In order to make all lines start from 0, you have to subtract to each column the respective starting point at the index 0:
for column in df.columns:
ax.plot(df.index, df[column] - df.loc[0, column], label = column)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame()
df['Time'] = np.arange(0, 0.6, 0.1)
df['Jolt 1'] = np.random.rand(len(df['Time']))
df['Jolt 2'] = np.random.rand(len(df['Time'])) + np.random.randint(low = -5, high = 5)
df['Jolt 3'] = np.random.rand(len(df['Time'])) + np.random.randint(low = -5, high = 5)
df = df.set_index('Time')
fig, ax = plt.subplots()
for column in df.columns:
ax.plot(df.index, df[column] - df.loc[0, column], label = column)
ax.legend(frameon = True)
plt.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.