How can I change my yticks in matplotlib? I Want to be like "0, 5, 10..." (currently getting a "0.5, 1.0..." [duplicate] - pandas

This question already has answers here:
set y-axis in millions [duplicate]
(3 answers)
How do I make the numbers on the y-axis show values in millions instead of in scientific notation in matplotlib?
(1 answer)
Closed 3 months ago.
enter image description here
import matplotlib.pyplot as plt
cidades = [city for city, df in mv_cidade.groupby('Loja')]
plt.bar(mv_cidade['Loja'], mv_cidade['Faturamento'])
plt.ylabel('Faturamento em milhões')
plt.xlabel('Loja (por Estado)')
plt.xticks(cidades, rotation= 'vertical', size= 9)
plt.show()
It's kinda simple. I just wanna know how to change the yticks (0.5, 1.0, 1.5) to something like (5, 10, 15, 20)
Can anyone help?
I've already tried to put the "original" values
plt.yticks([6480300, 6868600, 7060500, 7303000, 7441800, 13111300, 14087900, 14867800, 22098300])
that was the result

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

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.

increasing x y values in matpplot [duplicate]

This question already has answers here:
How to set ticks on Fixed Position , matplotlib
(2 answers)
Closed 2 years ago.
I have made a plot in Jupyter and I got output as shown in picture but I want to increase my x values like it has 1995,2000,2005,2010,2015 on x axis and I want more x values like say 1995,1997,1999,2001,2003...so on.
I have enter this code but I am unable to produce more x values and y values as mentioned before.
fig=plt.figure(figsize=(9, 7), dpi= 100, facecolor='w', edgecolor='k')
plt.plot(df_3)
You can use xticks such as
plt.xticks(min_x, max_x+1, 1.0)
You can also set your interval 2 or 3 to have more number on your x-axis.

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

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