Issue
I'm a newcomer to Pandas and Matplotlib, trying to plot a relation between the mean value of my array's rows and columns. The result I'm looking for is something like this: "linhas" refers to the rows and "colunas" refers to the columns. The Y label refers to the means and the X label refers to the number of columns in my array
I came up with some solutions, as shown below:
print(arr)
df = pd.DataFrame(arr)
display(df)
num_cols = [df.shape[1]]
print(type(num_cols))
print(num_cols)
cols = df.count(axis=1)
lcols = cols.tolist()
print(type(lcols))
col_mean = df[:].mean(axis=0)
print(type(col_mean))
col_mean.tolist()
row_mean = df[:].mean(axis=1)
print(type(row_mean))
row_mean.tolist()
print(type(row_mean))
print(row_mean)
dados = pd.DataFrame({
'Colunas': col_mean,
'Linhas': row_mean
}, index=lcols)
lines = dados.plot.line()
What I was looking after is something like this: "linhas" refers to the rows and "colunas" refers to the columns. The Y label refers to the means and the X label refers to the number of columns in my array
Unfortunately, my output is totally wrong, as follows: My output
Any help would be deeply appreciated, as I'm a bit lost right now. Thanks in advance!
Solution
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# just a dummy array
arr = np.array([[37, 68, 1, 19, 6],
[ 0, 14, 32, 73, 53],
[37, 85, 67, 30, 91],
[42, 52, 6, 42, 85],
[82, 26, 44, 38, 48],
[54, 55, 23, 46, 78]])
n_rows, n_cols = arr.shape
df = pd.DataFrame(arr)
col_mean = df.mean(axis=0)
row_mean = df.mean(axis=1)
plt.plot(range(1, n_rows+1), row_mean, marker='^', c='orange', label='rows')
plt.plot(range(1, n_cols+1), col_mean, marker='o', c='blue', label='cols')
plt.xlabel('Label x axis')
plt.ylabel('Label y axis')
plt.title('Title plotting')
plt.legend()
Answered By - Joao_PS
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.