bars not proportional to value - matplotlib bar chart [duplicate] - matplotlib

This question already has an answer here:
Difference in plotting with different matplotlib versions
(1 answer)
Closed 4 years ago.
I am new to matplotlib and am trying to plot a bar chart using pyplot. Instead of getting a plot where the height of bar represents the value, I am getting bars that are linearly increasing in height while their values are displayed on the y-axis as labels.
payment_modes = ['Q', 'NO', 'A', 'C', 'P', 'E', 'D']
l1=[]
l2=[]
for i in payment_modes:
l.append(str(len(df[df['PMODE_FEB18']==i])))
# here l = ['33906', '37997', '815', '4350', '893', '98', '6']
plt.figure()
plt.bar(range(7),l)
This is what I am getting:

The problem is that you seem to be feeding bar with strings, not with numerical quantities. If you instead use the actual numerical quantities, bar will behave as you would expect:
import matplotlib.pyplot as plt
l = [33906, 37997, 815, 4350, 893, 98, 6]
plt.figure()
plt.bar(range(7),l)
plt.show()
gives

Related

How can I create a bar chart in the image attached to the question? [duplicate]

This question already has answers here:
Create a grouped bar plot using seaborn
(2 answers)
Plot bar chart in multiple subplot rows with Pandas
(1 answer)
Plotting a bar chart with seaborn
(1 answer)
Seaborn Catplot set values over the bars
(3 answers)
How to make multiple plots with seaborn from a wide dataframe
(2 answers)
Closed 8 months ago.
I would like to create a subplot of bar chart where '% of total' is the y-axis and 'plants' is the x-axis. Also 'brand' will be legend, so in this case 3 different charts for the 3 different 'brands'. Each groups % adds up to 100%. I started with the code below, but got stuck. Please see a sample of the data below and image below;
import pandas as pd
import numpy as np
df = pd.DataFrame({
'brand':['A','A', 'A', 'B','B', 'B' ,'C','C', 'C'],
'plants':[0, 1, 2, 0,1,2,0,1,2],
'% of total':[80, 12, 8, 67, 18, 5,35, 40,25],
})
plt.figure(figsize=(10, 10))
for i, brand in enumerate(['A', 'B', 'C']):
You can use seaborn and catplot:
# Python env: pip install seaborn
# Anaconda env: conda install seaborn
import seaborn as sns
import matplotlib.pyplot as plt
sns.catplot(x='plants', y='% of total', col='brand', data=df, kind='bar')
plt.show()
Output:
Does this need to be in a for loop? You could simply grab the relevant rows using pandas.
For example:
my_A_df = df[df['brand'] == A]
plt.hist(my_A_df)
plt.bar(my_A_df['plants'], my_A_df['% of total'])
This will work for generating a barplot for each. Not sure if this is within the bounds of your problem but happy to edit if necessary.

How to pick a new color for each plotted line in matplotlib? [duplicate]

This question already has answers here:
How to pick a new color for each plotted line within a figure in matplotlib?
(7 answers)
Closed 7 months ago.
I have a small problem with the colors when I make a loop for to create my lines
Can you help me please?
I would like to change the color of each line
data = {'Note':['3','3','3','4','4','4','5','5','5','1','2','2','1'],
'Score':['10','10.1','17','17.5','16.5','14.3','10','10.1','17','17.5','16.5','16.5','16.5']}
Create DataFrame
df = pd.DataFrame(data)
Create Plot by loop for
groups=df.groupby("Note")["Score"].min()
for color in ['r', 'b', 'g', 'k', 'm']:
for i in groups.values:
plt.axhline(y=i, color=color)
plt.show()
The reason you are not seeing the colors is because you are running 2 for loops and there are several values which are the same number (like 16.5 and 10). So, the lines get written one on top of the other. You can see the different colors by changing you code like this. But do note that only the last color for a particular y value will be seen.
data = {'Note':['3','3','3','4','4','4','5','5','5','1','2','2','1'],
'Score':['10','10.1','17','17.5','16.5','14.3','10','10.1','17','17.5','16.5','16.5','16.5']}
df = pd.DataFrame(data)
groups=df.groupby("Note")["Score"].min()
print(group.values)
color = ['r', 'b', 'g', 'k', 'm']
j=0
for i in groups.values:
plt.axhline(y=i, color=color[j%5])
j=j+1
plt.show()
OUTPUT

Pandas histogram plot with Y axis or colorbar

In Pandas, I am trying to generate a Ridgeline plot for which the density values are shown (either as Y axis or color-ramp). I am using the Joyplot but any other alternative ways are fine.
So, first I created the Ridge plot to show the different distribution plot for each condition (you can reproduce it using this code):
import pandas as pd
import joypy
import matplotlib
import matplotlib.pyplot as plt
df1 = pd.DataFrame({'Category1':np.random.choice(['C1','C2','C3'],1000),'Category2':np.random.choice(['B1','B2','B3','B4','B5'],1000),
'year':np.arange(start=1900, stop=2900, step=1),
'Data':np.random.uniform(0,1,1000),"Period":np.random.choice(['AA','CC','BB','DD'],1000)})
data_pivot=df1.pivot_table('Data', ['Category1', 'Category2','year'], 'Period')
fig, axes = joypy.joyplot(data_pivot, column=['AA', 'BB', 'CC', 'DD'], by="Category1", ylim='own', figsize=(14,10), legend=True, alpha=0.4)
so it generates the figure but without my desired Y axis. So, based on this post, I could add a colorramp, which neither makes sense nor show the differences between the distribution plot of the different categories on each line :) ...
ar=df1['Data'].plot.kde().get_lines()[0].get_ydata() ## a workaround to get the probability values to set the colorramp max and min
norm = plt.Normalize(ar.min(), ar.max())
original_cmap = plt.cm.viridis
cmap = matplotlib.colors.ListedColormap(original_cmap(norm(ar)))
sm = matplotlib.cm.ScalarMappable(cmap=original_cmap, norm=norm)
sm.set_array([])
# plotting ....
fig, axes = joypy.joyplot(data_pivot,colormap = cmap , column=['AA', 'BB', 'CC', 'DD'], by="Category1", ylim='own', figsize=(14,10), legend=True, alpha=0.4)
fig.colorbar(sm, ax=axes, label="density")
But what I want is some thing like either of these figures (preferably with colorramp) :

Matplotlib - Value label on bar graph [duplicate]

This question already has answers here:
How to add value labels on a bar chart
(7 answers)
Closed 6 months ago.
Hi I would like to put value labels on the bar graph below:
df = pd.DataFrame({'Percentile':[25, 50, 75] , "Price in GBP":[
10.000000 ,
50.000000 ,
453.750000
]})
df.plot.bar(x='Percentile', y="Price in GBP", rot=0,grid=True)
plt.ylabel("Price in GBP")
plt.title("Business Coach - Price Distribution")
plt.show()
The graph should look like this:
I have searched a lot, but sadly can't find a relevant solution that works. Thanks
To add text to a chart, you'll need to add each label one at a time by looping through the data.
The bars are located at 0, 1, and 2, which is why range(len(df)) is used rather than df["Percentile"]. I also added in an offset (-.1 and +5) to x and y so that the text appears centered over the bar. Experiment with removing/adjusting those offsets to see how the output changes.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'Percentile':[25, 50, 75] , "Price in GBP":[
10.000000 ,
50.000000 ,
453.750000
]})
df.plot.bar(x='Percentile', y="Price in GBP", rot=0,grid=True)
# add the labels one at a time
for x, price in zip(range(len(df)), df["Price in GBP"]):
plt.text(x - .1, price + 5, price)
plt.show()
You can spend a ton of time adjusting the formatting, but this should get you started.

How to set axis to start from corner in Matplotlib [duplicate]

This question already has answers here:
Matplotlib: cancelling the offset of axis introduced in matplotlib 2.0 [duplicate]
(2 answers)
How can I change the x axis in matplotlib so there is no white space?
(2 answers)
Closed 5 years ago.
this is a graph that I plotted:
# MatPlotlib
import matplotlib.pyplot as plt
# Scientific libraries
import numpy as np
plt.figure(1)
points = np.array([(100, 6.09),
(111, 8.42),
(119, 10.6),
(129, 12.5),
(139, 14.9),
(149, 17.2),
(200, 28.9),
(250, 40.9),
(299, 52.4),
(349, 64.7),
(400, 76.9)])
# get x and y vectors
x = points[:,0]
y = points[:,1]
# calculate polynomial
z = np.polyfit(x, y, 3)
f = np.poly1d(z)
# calculate new x's and y's
x_new = np.linspace(x[0], x[-1], 50)
y_new = f(x_new)
plt.plot(x,y,'bo', x_new, y_new)
plt.show()
I find that all the graphs I plot do not have their axes starting from the corner of the box, could anyone tell me how I can correct this? Aside from setting limits in the graph
By default, matplotlib adds a 5% margin on all sides of the axes.
To get rid of that margin, you can use plt.margins(0).
import matplotlib.pyplot as plt
plt.plot([1,2,3],[1,2,3], marker="o")
plt.margins(0)
plt.show()
To change the margins for the complete script, you may use
plt.rcParams['axes.xmargin'] = 0
plt.rcParams['axes.ymargin'] = 0
Or you may change your rc file to include those settings.