I am trying to do a plot that uses sub plots in Python, but it doesn't seem to be working.
SO I have a dataframe and there is an Index. This index holds datetime. I have two columns that I would like to plot, and I am trying to use sub plots. Data1 and Data2 are my column names. My code is below
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(x=df[index], y=df['Data1'])
ax2.plot(x=df[index], y=df['Data2'])
plt.show()
When I run this I get the following error.
TypeError: 'DataFrame' object cannot be interpreted as an integer
Related
I am new to python
I have a few Pandas Data Frames having different columns
I am trying to plot different column using subplot plot as subplots, but I'm unfortunately failing to come up with a solution to how and would highly appreciate some help.
I also tried few things pasting the code below but I am getting the error -'AxesSubplot object is not subscriptable'
enter code here
df = pd.read_csv('D:\data_ana\\file_r.csv')
figure , axis = plt.subplots(1,1)
axis[0,0].df.plot(x = "Age", y = ["K_DistX","K_DistY"],
kind="line", figsize=(5, 5))
axis[0,1].df.plot(x="Age", y=["K_VabsX","K_VabsY"],
kind="line", figsize=(5, 5))
# display plot
plt.show()
Well since you are a newbee, here is an explanation of your given error
AxesSubplot, is the object you create in this line of code, to be exact your axis variable
figure , axis = plt.subplots(1,1)
axis[0,0].df.plot(x = "Age", y = ["K_DistX","K_DistY"],
When you do this line of code, python be like wtf. Since the AxesSubplot is not an mutable interator he gives you that error
So basically what you need to do is to stop slicin the axis object this will stop giving you that specific error, but unfortunately for you there are other issues in your code but they seen to be pretty basic stuff like what is a df object and so on i would recommend you study a little more.
axis.plot(x_axis_data,y_axis_data)
I have multiple .dat file (30) and I load them as Dataframes and add more columns:
A_files = glob.glob("*.A*.dat") #load .dat file which contains "A" in their name
for files in A_files:
df=pd.read_fwf(files,header=None,infer_nrows=300,names=["Time","Result",'Error']) #loading the files as dataframes
df['error_plus']=df["Result"]+df['Error'] #defining first curve for error bars
df['error_minus']=df["Result"]-df['Error'] #defining second curve for error bars
Now I'd like to make subplots from the dataframes, where x='Time', y='Result', and 'error_plus' with 'error_minus' will serve for function ax.fill_between. I tried to extend the code above with this:
A_files = glob.glob("*.A*.dat")
for files in A_files:
df=pd.read_fwf(files,header=None,infer_nrows=300,names = ["Time","Result",'Error'])
df['error_plus']=df["Result"]+df['Error']
df['error_minus']=df["Result"]-df['Error']
ax=df.plot(subplots=True,x='Time', y="Result",sharey=True, sharex=True)
ax.fill_between(df["Time"], df["error_plus"],df["error_minus"],color="r")
However, it didn't make subplots as I expected and this error was raised: 'numpy.ndarray' object has no attribute 'fill_between' (but when I plot just one dataframe without looping, then this error doesn't occurr).
Is there some easy/elegant approach how to make subplots from a loop of dataframes? And also containing fill_between function to highlight error and shared axis?
Thanks.
The problem is in the use of ax=df.plot(), you need to give a previously created "matplotlib axes object" as a parameter to df.plot().
You also don't need the subplots parameter, he has a different meaning, he says whether to plot each of df's columns in a different subplot, or all together in one subplot.
See: pandas.DataFrame.plot documentation.
Then you also have to tell sharex, sharey to the pyplot.subplots() function instead, because this is where you create one plot with subplots.
Here is a mockup of all the changes:
fig, axes = plt.subplots(nrows=5, ncols=6, sharex=True, sharey=True)
axes_flat = axes.flatten()
for file, ax in zip(A_files, axes_flat):
df = pd.read_fwf(file, ...)
...
df.plot(x='Time', y="Result", ax=ax)
plt.show()
I am new to Pyplot and simply trying to read data from a .csv file and produce a line plot using ax.plot(x,y):
filepath ='Monthly_Raw_Financials.csv'
raw_data = pd.`read_csv`(filepath, index_col='Month', parse_dates=True)
fig, ax = plt.subplots()
ax.plot(raw_data.index, raw_data['Profit'])
plt.show()
I get only an empty axis with no data plotted and and error message "'Series' object has no attribute 'find'". I am following the example of a number of tutorials. What am I doing wrong?
In pandas, a column is a Series object, which isn't quite the same as a numpy array. It holds a numpy array in its .values attribute, but it also holds an index (.index attribute). I don't understand where your error comes from, but you could try plotting the values instead, i.e.
fig, ax = plt.subplots()
ax.plot(raw_data.index, raw_data['Profit'].values)
plt.show()
Note that you could use the plot method on your dataframe as:
ax = raw_data.plot('Profit')
plt.show()
Example Plot that needs to format date
I am trying to plot stock prices against time (see above). The code below does plot the "OPEN" prices but as I try to format the X-axis dates from ordinal to ISO dates, it throws AttributeError.
The same code worked while plotting the OHLC graph, but somehow this doesn't work now.
AttributeError: 'list' object has no attribute 'xaxis'
df_copy = read_stock('EBAY')
fig = plt.figure(figsize= (12,10), dpi = 80)
ax1 = plt.subplot(111)
ax1 = plt.plot(df_copy['Date'], df_copy['Open'], label = 'Open values' )
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
This line:
ax1 = plt.plot(df_copy['Date'], df_copy['Open'], label='Open values')
Refines your Axes object to be the list of artists returned by the plot command.
Instead of relying on the state machine to put artists on the Axes, you should use your objects directly:
df_copy = read_stock('EBAY')
fig = plt.figure(figsize=(12, 10), dpi=80)
ax1 = fig.add_subplot(111)
lines = ax1.plot(df_copy['Date'], df_copy['Open'], label='Open values')
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
The problem comes from you writing
ax1 = plt.plot(df_copy['Date'], df_copy['Open'], label = 'Open values' )
Since you are changing the type of ax1 from being the handle returned by plt.subplot(). After said line, it is a list of lines that were added to the plot, which explains your error message. See the documentary on the plot command:
Return value is a list of lines that were added.
matplotlib.org
I have two dataframes, with unique x and y coordinates, and I want to plot them in the same figure.
I am now plotting two dataframes in same figure as such:
plt.plot(df1['x'],df1['y'])
plt.plot(df2['x'],df2['y'])
plt.show
However, pandas also has plotting functionality.
df.plot()
How could I achieve the same as my first example but use the pandas functionality?
To plot all columns against the index as line plots.
ax = df1.plot()
df2.plot(ax=ax)
A single pandas.DataFrame.plot (not subplots=True) returns a matplotlib.axes.Axes, which you can then pass to the second dataframe.
To plot specific columns as x and y. Specifying x and y is required for scatter plots (kind='scatter').
ax = df1.plot(x='Lat', y='Lon', figsize=(8, 8))
df2.plot(ax=ax, x='Lat', y='Lon')