I have a pandas data frame where x axis has continuous values and y axis has ones and zeros. How should I make a bar plot so that the x axis has bins like 5-10 10-15 ,.... and y axis represents the no of ones and zeros =, i.e there are 100 ones when x is between 5-10
I tried the following method
ax = df.plot.bar(x='PAY',Y='ACCEPTED')
Here Y is categorical
I got the error
No numeric data to plot
I am drawing some graphs and I wanna import them in LaTex in 2 by 2 format. One of the problems is that values on the y-axis for one graph range from 1 to 6, but for another graph, those range from 1 to 200. Because of that, when I import graphs into my document, they do not look good. Is there any way to set the same width for value on the y-axis?
You can set the y axis limits using ax.set_ylim or plt.ylim:
# Set axis from 1 to 200
ax.set_ylim((1,200))
# Or just set it directly - this will also act on the current axis
plt.ylim((1,200))
Edit: The question is about widths rather than limits.
I think making the subplots together on one figure should solve this problem.
plt.figure()
plt.subplot(2,2,1)
plt.plot(x1,y1)
.
.
plt.subplot(2,2,4)
plt.plot(x4,y4)
So i'm trying to plot a (125 x 1000) grid with specified values. I'm using matplotlib with pcolormesh. This is how my code looks, enzyme array just symbolic.
enzyme = np.array([125 x 1000])
plt.pcolormesh(enzyme, cmap='Reds')
plt.colorbar()
plt.show()
The x-axis is my spatial resolution and my y-axis is time. My x-axis just runs from 0 to 125 and y-axis runs from 0 to 1000. But my actual problem is in hours, so I want the y-axis to show like 0hours -> 24hours per 2hours step. Something similar for the x-axis. So the grid index is not the right scale and number for my plot. How do I fix this.
I tried already including like
pcolormesh(x, y, enzyme)
with x and y a 1D array, but these have to match the length of my enzyme grid and i have way too many datapoints to put on the x- and y- axis.
I would suggest creating a new x and y array that fits the size of your enzyme array. You would assign each x and y value the time that corresponds to each of your indices. For example, if your 0-1000 y-axis is supposed to represent 24 hours, you could do something like this:
increase=24/1000.
yvals=np.arange(0,24,increase)
Dividing 24/1000 will give you the increment needed such that you have 1000 values going from 0,24 hours.
You then can change the xtick increments with something like this:
ax.set_xticks(np.arange(0,24,2))
I've got a simple plot in matplotlib. Every time that I plot a data, the graph render an exact Y axis to my plot. What I want is to add some space or allowance on my Y-axis. My maximum value in plot is 5
I want my graph to show at least up to 6 or 10 on it's Y-axis.
How ?
Thanks
I am struggling to set xlim for each histogram and create 1 column of graphs so the x-axis ticks are aligned. Being new pandas, I am unsure of how to apply answer applies: Overlaying multiple histograms using pandas.
>import from pandas import DataFrame, read_csv
>import matplotlib.pyplot as plt
>import pandas as pd
>df=DataFrame({'score0':[0.047771,0.044174,0.044169,0.042892,0.036862,0.036684,0.036451,0.035530,0.034657,0.033666],
'score1':[0.061010,0.054999,0.048395,0.048327,0.047784,0.047387,0.045950,0.045707,0.043294,0.042243]})
>print df
score0 score1
0 0.047771 0.061010
1 0.044174 0.054999
2 0.044169 0.048395
3 0.042892 0.048327
4 0.036862 0.047784
5 0.036684 0.047387
6 0.036451 0.045950
7 0.035530 0.045707
8 0.034657 0.043294
9 0.033666 0.042243
>df.hist()
>plt.xlim(-1.0,1.0)
The result sets only one of the bounds on the x-axis to be [-1,1].
I'm very familiar ggplot in R and just trying out pandas/matplotlib in python. I'm open to suggestions for better plotting ideas. Any help would be greatly appreciated.
update #1 (#ct-zhu):
I have tried the following, but the xlim edit on the subplot does not seem to translate the bin widths across the new x-axis values. As a result, the graph now has odd bin widths and still has more than one column of graphs:
for array in df.hist(bins=10):
for subplot in array:
subplot.set_xlim((-1,1))
update #2:
Getting closer with the use of layout, but the width of bins does not equal the interval length divided by bin count. In the example below, I set bins=10. Hence, the width of each bin over the interval from [-1,1] should be 2/10=0.20; however, the graph does not have any bins with a width of 0.20.
for array in df.hist(layout=(2,1),bins=10):
for subplot in array:
subplot.set_xlim((-1,1))
There are two subplots, and you can access each of them and modify them seperately:
ax_list=df.hist()
ax_list[0][0].set_xlim((0,1))
ax_list[0][1].set_xlim((0.01, 0.07))
What you are doing, by plt.xlim, changes the limit of the current working axis only. In this case, it is the second plot which is the most recently generated.
Edit:
To make the plots into 2 rows 1 column, use layout argument. To make the bin edges aligns, use bins argument. Set the x limit to (-1, 1) is probably not a good idea, you numbers are all smallish.
ax_list=df.hist(layout=(2,1),bins=np.histogram(df.values.ravel())[1])
ax_list[0][0].set_xlim((0.01, 0.07))
ax_list[1][0].set_xlim((0.01, 0.07))
Or specify exactly 10 bins between (-1,1):
ax_list=df.hist(layout=(2,1),bins=np.linspace(-1,1,10))
ax_list[0][0].set_xlim((-1,1))
ax_list[1][0].set_xlim((-1,1))