increasing x y values in matpplot [duplicate] - pandas

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.

Related

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

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

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

What are the parameters of pyplot.subplot? [duplicate]

This question already has answers here:
What does the argument mean in fig.add_subplot(111)?
(8 answers)
What is the behaviour of subplot given single argument? [duplicate]
(1 answer)
Why do many examples use `fig, ax = plt.subplots()` in Matplotlib/pyplot/python
(6 answers)
Closed 1 year ago.
Can't understand pyplot.subplot(330+1+i)
# plot first few images
for i in range(9):
#define subplot
pyplot.subplot(330 + 1 + i)
# plot raw pixel data
pyplot.imshow(trainX[i], cmap=pyplot.get_cmap('gray'))
# show the figure
pyplot.show()
You can view the arguments in the documentation for subplot. The basic arguments are subplot(nrows, ncols, index) where nrows is the number of rows of plots, ncols is the number of columns of plots, and index is the plot number when counting across the grid of plots from left to right, top to bottom.
Another way to specify subplots is with a three-digit integer as in your example.
A 3-digit integer. The digits are interpreted as if given separately as three single-digit integers, i.e. fig.add_subplot(235) is the same as fig.add_subplot(2, 3, 5). Note that this can only be used if there are no more than 9 subplots.
In your example where i ranges from 0 to 8, inclusive, the argument for subplot will range from 331 to 339. Following to the documentation, your subplots will be over 3 rows and 3 columns with in indices 1 to 9.

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.

Generate legend with matplotlib based on the values in column [duplicate]

This question already has answers here:
Scatter plots in Pandas/Pyplot: How to plot by category [duplicate]
(8 answers)
Closed 3 years ago.
Assume we have a dataframe of 4 individuals' scores in 2 different tests and the 3rd column tells us if they passed or failed overall
df:
[10,20,failed
10,40,passed
20,40,passed
30,10,failed]
I would like to generate a scatter plot with the scores of the 1st column on the x axis, the scores of the 2nd test on the y axis, and indicate with color (or marker) if they passed or failed. I have achieved this with:
plt.scatter(x=df[column1], y=df[column2], c=df[column3])
The question is, how can I have a legend based on the color (or marker) and column3?
[red: failed
blue: passed]
Here's my suggestion: Plot the failed an passed separately to get their handles, which can then be used for the legend.
fig = plt.figure()
ax1 = fig.add_subplot(111)
passed = ax1.scatter(x=df.loc[df[column3].eq('passed'), column1], y=df.loc[df[column3].eq('passed'), column2], c='green')
failed = ax1.scatter(x=df.loc[df[column3].eq('failed'), column1], y=df.loc[df[column3].eq('failed'), column2], c='red')
ax1.legend(handles=[passed, failed], labels=['Passed', 'Failed'])