I have a csv file containing two columns. What I'd like to do is to plot a histogram based on these two columns.
My code is as follows:
data = pd.read_csv('data.csv')
My csv data is made like this:
Age Blood Pressure
51 120
.. ...
I tried with plt.hist(data['Age'], bins=10) which only gives me an histogram based on the first column and its frequency, the same goes for the second column.
Is there a way to plot an histogram which shows me "Ages" in the x-Axis and "Blood Pressure" in the y-Axis?
If it actually makes sense for you, you can change the orientation of the second plot:
plt.hist(data['Age'], bins=10, alpha=.5)
plt.hist(data['Blood Pressure'], bins=10, alpha=.5, orientation='horizontal')
plt.show()
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.bar(data['Age'], data['Blood Pressure'])
Related
I am trying to include 2 seaborn countplots with different scales on the same plot but the bars display as different widths and overlap as shown below. Any idea how to get around this?
Setting dodge=False, doesn't work as the bars appear on top of each other.
The main problem of the approach in the question, is that the first countplot doesn't take hue into account. The second countplot won't magically move the bars of the first. An additional categorical column could be added, only taking on the 'weekend' value. Note that the column should be explicitly made categorical with two values, even if only one value is really used.
Things can be simplified a lot, just starting from the original dataframe, which supposedly already has a column 'is_weeked'. Creating the twinx ax beforehand allows to write a loop (so writing the call to sns.countplot() only once, with parameters).
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
sns.set_style('dark')
# create some demo data
data = pd.DataFrame({'ride_hod': np.random.normal(13, 3, 1000).astype(int) % 24,
'is_weekend': np.random.choice(['weekday', 'weekend'], 1000, p=[5 / 7, 2 / 7])})
# now, make 'is_weekend' a categorical column (not just strings)
data['is_weekend'] = pd.Categorical(data['is_weekend'], ['weekday', 'weekend'])
fig, ax1 = plt.subplots(figsize=(16, 6))
ax2 = ax1.twinx()
for ax, category in zip((ax1, ax2), data['is_weekend'].cat.categories):
sns.countplot(data=data[data['is_weekend'] == category], x='ride_hod', hue='is_weekend', palette='Blues', ax=ax)
ax.set_ylabel(f'Count ({category})')
ax1.legend_.remove() # both axes got a legend, remove one
ax1.set_xlabel('Hour of Day')
plt.tight_layout()
plt.show()
use plt.xticks(['put the label by hand in your x label'])
I have a csv file containing two columns. What I'd like to do is to plot a histogram based on these two columns.
My code is as follows:
data = pd.read_csv('data.csv')
My csv data is made like this:
Age Blood Pressure
51 120
.. ...
I tried with plt.hist(data['Age'], bins=10) which only gives me an histogram based on the first column and its frequency, the same goes for the second column. Is there a way to plot an histogram which shows me "Ages" in the x-Axis and "Blood Pressure" in the y-Axis?
Maybe you could use a Bar chart
This code will do the job probably:
plt.bar(data['Age'], data['Blood Pressure'], align='center')
plt.xlabel('Age')
plt.ylabel('Blood Pressure')
plt.title('Bar Chart')
plt.show()
More about Bar charts: https://pythonspot.com/matplotlib-bar-chart/
I took data from excel and plotted it. The first column is date, while the next two columns are prices of different indexes.
I managed to plot them, but they are on separate graphs. I need them plotted against each other with one y-axis (date) and two x-axis.
Also, I can't figure out how to make the line dotted for one and a diamond marker for the other.
import matplotlib.pyplot as plt
import pandas as pd
excel_data = pd.read_excel('Python_assignment_InputData.xlsx', '^GSPTSE')
excel_data.plot(kind='line', x = 'Date', y = 'Bitcoin CAD (BTC-CAD)', color = 'green')
excel_data.plot(kind='line', x = 'Date', y = 'S&P/TSX Composite index (^GSPTSE)', color = 'blue')
plt.show()
I expect Bitcoin and S%P prices to be on one y axis, with dates being on the x axis.
I am providing a sample answer using the iris DataFrame from seaborn. You can modify it to your needs. What you need is a single x axis and two y-axes.
import seaborn as sns
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
iris = sns.load_dataset("iris")
iris.plot(x='sepal_length', y='sepal_width', linestyle=':', ax=ax)
iris.plot(x='petal_length', y='petal_width', marker='d',
linestyle='None', secondary_y=True, ax=ax)
I have a dataframe with date as index, floats as columns, filled with mostly NaN and a few floats.
I am plotting this dataframe using :
fig, ax = plt.subplots()
plot(df2[11][2:], linestyle='dashed',linewidth=2,label='xx')
ax.set(xlabel='xx', ylabel='xx', title='xx')
ax.grid()
ax.legend()
The plot window open but with no data appearing. But if I use markers instead of line, the data point will appears.
What should I correct to plot my graphs as lines?
edit Thanks, it worked like this :
s1 = np.isfinite(df2[11][2:])
fig, ax = plt.subplots()
plot(df2.index[2:][s1],df2[11][2:].values[s1], linestyle='-',linewidth=2,label='xx')
ax.set(xlabel='xx', ylabel='xx',title='xx')
ax.grid()
ax.legend()
Try
import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(df2[11][2:], linestyle='dashed',linewidth=2,label='xx')
plt.set(xlabel='xx', ylabel='xx', title='xx')
plt.grid()
plt.legend()
plt.show()
In your case matplotlib won't draw a line between points separated by NaNs. You can mask NaNs or get rid of them. Have a look at the link below, there are some solutions to draw lines skipping NaNs.
matplotlib: drawing lines between points ignoring missing data
I have two different pandas dataframes from which I obtained the following graphs:
ar_month_mean.plot(figsize=(15,5))
hist_month.plot(kind='bar', figsize=(15,5))
I'd like to combine them to obtain something similar to this:
you can pass an ax to the plotting methods, to have multiple plots in the same ax. Otherwise, each new plot will be in a new axis:
import matplotlib.pyplot as plt
f = plt.figure(figsize=(15,5))
ax = plt.gca()
ar_month_mean.plot(ax=ax, figsize=(15,5))
hist_month.plot(ax=ax, kind='bar', figsize=(15,5))
If you post the actually data, I will upload the resulting figure.