3

The code below takes a dataframe filters by a string in a column and then plot the values of another column

I plot the values of the using histogram and than worked fine until I added Mean, Median and standard deviation but now I am just getting an empty graph where instead the all of the variables mentioned below should be plotted in one graph together with their labels

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import pyplot as plt
from matplotlib import pyplot as plt
import numpy as np

df = pd.read_csv(r'C:/Users/output.csv', delimiter=";", encoding='unicode_escape')
df['Plot_column'] = df['Plot_column'].str.split(',').str[0]
df['Plot_column'] = df['Plot_column'].astype('int64', copy=False)

X=df[df['goal_colum']=='start running']['Plot_column'].values


dev_x= X
mean_=np.mean(dev_x)
median_=np.median(dev_x)
standard_=np.std(dev_x)

plt.hist(dev_x, bins=5)
plt.plot(mean_, label='Mean')
plt.plot(median_, label='Median')
plt.plot(standard_, label='Std Deviation')



plt.title('Data')

2
  • I think it's because you go from hist to plot. Not the end of the world, but a bit tedious- hist returns the bin positions, and you can write a loop to iterate through and manually place them on plot (so do plt.hist, then close it, then do your other plots, then manually add the hist stuff). EDIT: also, you'll have to use the ax technique Commented Feb 12, 2020 at 17:51
  • Since you seem to be new to Stack Overflow, you should read How to create a Minimal, Complete, and Verifiable example Commented Feb 12, 2020 at 18:19

1 Answer 1

2

https://matplotlib.org/3.1.1/gallery/statistics/histogram_features.html

There are two major ways to plot in matplotlib, pyplot (the easy way) and ax (the hard way). Ax lets you customize your plot more and you should work to move towards that. Try something like the following

num_bins = 50

fig, ax = plt.subplots()

# the histogram of the data
n, bins, patches = ax.hist(dev_x, num_bins, density=1)

ax.plot(np.mean(dev_x))
ax.plot(np.median(dev_x))
ax.plot(np.std(dev_x))

# Tweak spacing to prevent clipping of ylabel
fig.tight_layout()
plt.show()
Sign up to request clarification or add additional context in comments.

3 Comments

any input . Really stuck here and need some help
can you change your input to an MCVE by changing the df to a hard-coded set of inputs?
i have a csv file , I want to read it and only plot the values of one specific column using K-Means, find the mean, median and standard deviation and plot all these into one graph

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.