Adding y values to a plot using matplotlib [duplicate] - python
This question already has answers here:
How to plot and annotate a grouped bar chart
(1 answer)
Python matplotlib multiple bars
(7 answers)
How to annotate grouped bar plot with percent by hue/legend group
(1 answer)
How to plot grouped bars in the correct order
(1 answer)
How to get a grouped bar plot of categorical data
(1 answer)
Closed 1 year ago.
I would like to add the y values to my plot for each year to make the graph easily readable but not sure how to do it. I have tried using the enumerate function but it does not return the desired output. Any guidance on this would be helpful.
import numpy as np
import pandas as pd
from matplotlib import pyplot
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
laikipia = pd.DataFrame({
"2020":[5.21, 20.91, 17.05],
"2021":[20.91, 19.91, 19.76],
},
index=["Cropland", "Forestland", "Shrubland"]
)
c = ['#FFDB5C', '#358221', '#EECFA8']
laikipia.plot(kind="bar", color=c)
plt.title("")
plt.xlabel("Laikipia LULC")
plt.ylabel("Area Loss (ha)")
plt.legend(laikipia, bbox_to_anchor=(1, 1))
plt.xticks(rotation=0)
#plt.yticks(round [plotdata], 0)
x = laikipia
y = [70, 60, 50, 40, 30, 20, 10, 0]
max_y_lim = max(y)
min_y_lim = min(y)
plt.ylim(min_y_lim, max_y_lim)
for i, v in enumerate(y):
plt.text(0, i, y[i], str(v), ha="center", va = "bottom")
plt.show()
plt.tight_layout()
plot output
Related
matplotlib - change number display and remove the mini lines [duplicate]
This question already has answers here: Prevent scientific notation (1 answer) Python hide ticks but show tick labels (9 answers) How to format axis number format to thousands with a comma (10 answers) Closed last month. I would like to have two changes: Instead of 1.2 I want to have 1.200.000 (also for the others) as x axis tick labels - red colour Get rid of the mini-lines (y axis ticks) - blue colour Here is my code: import pandas as pd #import numpy as np import matplotlib.pyplot as plt df = pd.read_excel(r"C:\Users\...\3_Python.xlsx", sheet_name=2) a = [_*200000 for _ in range(12)] b = [_*200000 for _ in range(12)] df.plot(kind="scatter" ,color="dimgrey" ,marker="D" ,y="Vorher" ,x="Nachher" ,title="Title" ,xticks=[_*200000 for _ in range(12)] ,yticks=[_*200000 for _ in range(12)] ) plt.plot(a, b, label="xxx", linestyle="-", color="black") plt.text(1000000, 1, "xxxx") plt.text(1, 1000000, "yyyyy") plt.ylim(ymin=0, ymax=1200000) plt.xlim(xmin=0, xmax=1200000) plt.xlabel("past") plt.ylabel("future") plt.grid(True, linewidth=2, color="grey") plt.show()
draw multiple box plots on a single graph [duplicate]
This question already has answers here: Import multiple CSV files into pandas and concatenate into one DataFrame (20 answers) dataframe to long format (2 answers) seaborn boxplot and stripplot points aren't aligned over the x-axis by hue (1 answer) Closed 6 months ago. For a given dataset I am plotting a box plot of size of object at 10 different points as below: import matplotlib.pyplot as plt import matplotlib.font_manager as font_manager import matplotlib.pyplot as plt import numpy as np import pandas as pd import matplotlib as mpl font_prop = font_manager.FontProperties( size=18) def plot (path, name=""): df = pd.read_csv(path, index_col=0) df = df.dropna() Position = [1 + i // df.shape[0] for i in range(df.size)] df_n = [df[col] for col in df.columns] df_t = pd.concat(df_n).tolist() groups = [[] for i in range(max(Position))] [groups[Position[i] - 1].append(df_t[i]) for i in range(len(df_t))] plt.figure(figsize=(12, 5)) plt.scatter(Position, df_t, color='g') b = plt.boxplot(groups, patch_artist=False) for median in b['medians']: median.set(color='r', linewidth=2) A typical graph would be like this: I have 4 different datasets and I would like to present a graph where on the position axis (x axis) there will be 4 bar plots above each position. How would I modify my code to do that? Here is the sample dataset: https://github.com/aebk2015/multipleboxplot.git ,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,Class 1,7.6,1.0,1.0,1.0,1.0,6.0,49.0,1.0,1.0,40.0,L 2,9.7,2.7,5.6,1.0,1.0,1.0,34.0,1.0,1.0,1.0,L 3,1.0,6.0,1.0,1.0,1.0,3.0,39.0,1.0,28.0,1.0,L 4,8.0,25.5,1.0,1.0,1.0,1.0,24.0,1.0,1.0,1.0,L 5,1.0,29.0,1.0,1.0,1.0,1.0,38.0,29.0,20.0,1.0,L 6,4.0,34.0,1.0,1.0,1.0,39.0,14.0,1.0,12.0,1.0,L 7,1.0,17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.0,1.0,48.0,1.0,1.0,1.0,W 1,1.0,9.7,6.8,53.0,1.0,57.0,1.0,9.5,1.0,1.0,B 2,5.8,16.3,1.0,10.8,1.0,58.0,1.0,1.0,1.0,1.0,B 3,1.0,38.0,17.0,34.0,1.0,55.0,1.0,8.0,1.0,1.0,B 4,1.0,42.0,1.0,26.0,1.0,1.0,65.0,44.0,1.0,1.0,B 5,41.0,43.0,16.0,9.7,1.0,36.0,61.0,1.0,1.0,1.0,B 6,47.0,20.0,1.0,1.0,1.0,1.0,28.0,7.7,1.0,1.0,B 7,22.0,92.0,1.0,1.0,1.0,20.0,15.0,1.0,1.0,1.0,B 8,31.0,72.0,1.0,1.0,1.0,1.0,20.0,1.0,1.0,1.0,B
Plotting dataframe using matplot lib [duplicate]
This question already has answers here: Line plot with data points in pandas (2 answers) Closed 1 year ago. Hi I am trying to get a line plot for a dataframe: i = [0.01,0.02,0.03,....,0.98,0.99,1.00] values= [76,98,22,.....,32,98,100] but there is index from 0,1,...99 as well and when I plot the index line also gets plotted. How do I ignore the plotting of index? I used the following code: plt.plot(df,color= 'blue', label= 'values') plt.title('values for corresponding i') plt.legend(loc= 'upper right') plt.xlabel("i") plt.ylabel("values") plt.show()
You could use plot.line directly on pandas dataframe, it's a wrapper around matplotlib and it makes stuff easier. Example: import pandas as pd import numpy as np import matplotlib.pyplot as plt # Generate random DataFrame i = np.arange(0, 1, 0.01) values = np.random.randint(1, 100, 100) df = pd.DataFrame({"i": i, "values": values}) # Plot df.plot.line(x="i", y="values", color="blue", label="values") plt.title("values for corresponding i") plt.legend(loc="upper right") plt.xlabel("i") plt.ylabel("values") Result:
How to adjust space between Matplotlib/Seaborn subplots for multi-plot layouts [duplicate]
This question already has answers here: Improve subplot size/spacing with many subplots (8 answers) Closed 5 months ago. The following figure shows the standard Seaborn/Matplotlib Boxplots in a 2 X 2 grid layout: It is pretty much what I want except that I would like to put some more space between the first row of the of the plots and the second row. The distance between the X-axis labels of the first row plots and the title of the second row plots is almost non-existent. I have been playing with the parameters as explained in this thread: StackOverflow Thread Here is my relevant code: import math import pandas as pd import numpy as np import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages from PyPDF2 import PdfFileMerger import seaborn as sns num_cols = 2 num_rows = int(math.ceil(tot_plots / float(num_cols))) fig, axes = plt.subplots(nrows=num_rows, ncols=num_cols, figsize=(16, 16)) x_var = df_orig['hra'] for idx, ax in enumerate(axes.flat): data_var = current_cols[idx] y_var = df_orig[data_var] title_str = '' sns.boxplot(x=x_var, y=y_var, ax=ax, order=order, palette=color, showfliers=False) ax.set_title(data_var + title_str) ax.xaxis.label.set_visible(False) ax.yaxis.label.set_visible(False) ax.xaxis.set_tick_params(labelsize=8) ax.yaxis.set_tick_params(labelsize=8) plt.setp(ax.xaxis.get_majorticklabels(), rotation=90) fig.suptitle("Sampling BoxPlots", x=0.5, y=0.93, fontsize=14, fontweight="bold") plt.tight_layout() plt.subplots_adjust(top=0.8) pdf_pages = PdfPages(file_name) pdf_pages.savefig() pdf_pages.close()
Have you tried adjusting hspace = 0.8 instead? According to matplotlib's reference that's the argument for changing the height between subplots, and not top. plt.subplots_adjust(hspace = 0.8)
How can I rotate the auto-generated x-axis labels of a matplotlib plot? [duplicate]
This question already has answers here: Date ticks and rotation in matplotlib (6 answers) Closed 7 years ago. I want to rotate the automatically-generated x-axis labels of my plot in order to prevent the labels from overlapping: I have been advised that matplotlib.pyplot.setp is worth a look, but I don't know how to use it in conjunction with labels that are generated automatically. How can I rotate these labels? x = [element[0] for element in eventDuplicates] y = [element[1] for element in eventDuplicates] figure = matplotlib.pyplot.figure() figure.suptitle("event duplicates", fontsize = 20) matplotlib.pyplot.scatter(x, y, s = 1, alpha = 1) axes = matplotlib.pyplot.gca() axes.yaxis.set_major_formatter(FormatStrFormatter("%.0f")) axes.xaxis.set_major_formatter(FormatStrFormatter("%.0f")) axes.set_xlim(left = 0) #axes.set_ylim(bottom = 0) #matplotlib.pyplot.setp(rotation = 90) # <--- insert magic here matplotlib.pyplot.xlabel("run numbers") matplotlib.pyplot.ylabel("event numbers") matplotlib.pyplot.savefig("diagnostic_event_duplicates_scatter.png")
Something like this? import numpy as np import matplotlib.pylab as pl import matplotlib as mpl mpl.rcParams['axes.formatter.useoffset'] = False x = np.arange(10000000, 10000010, 1) y = np.cos(x) pl.figure() pl.subplot(121) pl.plot(x,y) pl.subplot(122) pl.plot(x,y) locs, labels = pl.xticks() pl.setp(labels, rotation=90)