How would I make the figure larger to clearly display all my values along the y axis.
df = pd.read_csv(io.BytesIO(uploaded['mass_shootings.csv']))
df.State.value_counts().plot(kind='barh')
Try using the figsize argument. E.g.
df = pd.read_csv(io.BytesIO(uploaded['mass_shootings.csv']))
df.State.value_counts().plot(kind='barh', figsize=(20, 6))
Read more on https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.html
Related
Consider the following snippet
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
data = np.random.rand(10,5)
cols = ["a","b","c","d","e"]
df = pd.DataFrame(data=data, columns = cols)
df.index.name="Time (s)"
fig,axes = plt.subplots(3,2,sharex=True, squeeze=False)
axes = axes.T.flat
axes[5].remove()
df.plot(subplots=True,grid=True,legend=True,ax = axes[0:5])
that produces the following plot
I wish to show the xticks in the subplots where they are missing as I wrote in red with reference to the above picture.
I wish to show only the xticks where I marked in red, not the labels. The labels are fine where they currently are and shall be kept there.
After some search, I tried with
for ax in axes:
ax.tick_params(axis="x")
and
for ax in axes:
ax.spines.set(visible=True)
but with no success.
Any hints?
EDIT: As someone kindly suggested, if I set sharex=False, then when I horizontally zoom on one axes I will not have the same zoom effect on the other axes and this is not what I want.
What I want is to: a) show the xticks in all axes, b) when I horizontally zoom on one axes all the other axes are horizontally zoomed of the same amount.
You need to turn off sharing x properties by setting sharex=False (which is the default value by the way in matplotlib.pyplot.subplots):
Replace this:
fig,axes = plt.subplots(3,2,sharex=True, squeeze=False)
By this:
fig,axes = plt.subplots(3,2, squeeze=False)
# Output:
This might be a very simple question, but I just could not get the trick for this problem .
I want to plot multiple subplots, but when I have done that and use my defined axis limits, I find there is overlapping of axis. Y axis should be same in each column. Any tips to remove this:
My simplified script is here:
column = 2
num=len(sta_files)
fig, axes = plt.subplots(nrows=num, ncols=column,figsize=(15,15))
n=0
for event_file in sta_files:
axes[n,0].plot(np.arange(0,len(st[0].data))*1/sampling_rate,
st[0].data+i,color='k',linewidth=0.7)
axes[n,0].set_xlim((0, 35))
spl2 = st[0]
fig = spl2.spectrogram(show=False, axes=axes[n,1])
mappable = axes[n,1].images[0]
Here is my output:
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 list of many aggregated data frames with identical structure.
I would like to plot two columns from each dataframe on the same graph.
I used this code snippet but it gives me a separate plot for each dataframe:
# iterate through a list
for df in frames:
df.plot(x='Time', y='G1', figsize=(16, 10))
plt.hold(True)
plt.show()
If you have each set indexed, you can just concatenate all of them and plot them at once without having to iterate.
# If not indexed:
# frames = [df.assign(sample=i) for i, df in enumerate(frames)]
df = pd.concat(frames).pivot(index='Time', columns='sample', values='G1')
df.plot(figsize=(16, 10));
This helps make sure that your data is aligned and plt.hold is deprecated in matplotlib 2.0.
As you noticed, pandas.DataFrame.plot is not affected by matplotlib's hold parameter because it creates a new figure every time. The way to get around this is to pass in the ax parameter explicitly. If ax is not None, it tells the DataFrame to plot on a specific set of axes instead of making a new figure on its own.
You can prepare a set of axes ahead of time, or use the return value of the first call to df.plot. I show the latter approach here:
ax = None
for df in frames:
ax = df.plot(x='Time', y='G1', figsize=(16, 10), ax=ax)
plt.hold(True)
plt.show()
I am using the pandas plotting function for histogram subplots.
df.plot.hist(subplots = True)
I would like to add a vertical line to each of the plots. I know you can do this in matplotlib plot using
plt.axvlines
But how do I access the individual subplots generated by pandas?
I looked at :Is there a way to pass different vertical lines to each subplot when using pandas histogram with "by=somevar"?
But can someone please give me an actual example of how to do this?
Thank you!
DataFrame.hist() returns a list of Axes object. You just have to iterate over each axis and add you vertical line
df = pd.DataFrame(np.random.random((100,5)))
axs = df.plot.hist(subplots = True)
for ax in axs:
ax.axvline(0.5)
I was facing the same issue you mentioned:
ax = daily_returns.hist(bins=20) # changing no. of bins to 20
print(ax)
And by printing the result I got:
[[<matplotlib.axes._subplots.AxesSubplot object at 0x0000021F1BEBEF28>
<matplotlib.axes._subplots.AxesSubplot object at 0x0000021F1C0F8208>]]
So in my case I woulnd need to do something like ax[0][0] and ax[0][1] to get to the axes