Setting x axys with matplotlib - python-3.x

I have this chart but its not showing the data as I would like.
I need it to show all labels (hour) in the x plot from 1 to 24 instead its showing (0, 5, 10, 15, 25) and I would like it not to show the 0 and 25 of the x plot.
import tkinter
import matplotlib
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import numpy as np
root = tkinter.Tk()
root.wm_title("Embedding in Tk")
xlst=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
ylst=[26, 25, 21, 43, 68, 107, 221, 569, 575, 599, 586, 432, 486, 525, 410, 328, 251, 166, 153, 105, 123, 93, 54, 69]
matplotlib.use("TkAgg")
figure = Figure(figsize=(8,6), dpi=80)
plot = figure.add_subplot(1, 1, 1)
plot.bar(xlst, ylst)
canvas = FigureCanvasTkAgg(figure, root)
canvas.get_tk_widget().grid(row=0, column=0)
tkinter.mainloop()
The 0 and 25 marked with the red arrow I want it to start with 1 and end with 24 also with the green arrow I would like to show all hours.
Can anyone help?

You are missing just one line, so here you go:
plot.set_xticks(np.arange(1,25))

Related

Why the output of ax.hist and physt.hist are not identical?

I have an array and I would like to place it into 7 bins and then calculate the mean and standard deviation (standard in the error) corresponding to each bin so that I can plot both the histogram as well as the errorbars. While the numpy histogram readily outputs the mean values of bins, it is not meant to produce the errorbars (unless I am wrong). This is why I want to use the physt python package to directly extract the mean and errors corresponding to each bin for the purpose of errorbars. But, I just noticed that the two methodology are not agreeing with each other in the first place; they don't even produce the same mean values (heights) as expected. Now, I am kind of confused. I would truly appreciate your help.
import numpy as np
from physt import h1
import matplotlib.pyplot as plt
x_arr = np.array([
0, 32, 28, 15, 19, 22, 18, 16, 13, 35, 21, 32, 23, 11, 17, 3, 17, 3, 21, 43, 32, 15, 16, 18,
28, 9, 33, 16, 20, 19, 35, 37, 32, 26, 30, 30, 28, 30, 22, 25, 21, 26, 41, 41, 12, 3, 5, 6, 5,
17, 16, 16, 16, 7, 2, 15, 16, 15, 15, 15, 7, 5
])
bins = np.array([0, 2, 3, 5, 9, 17, 33, 65])
ax = plt.axes()
heights, bins, patches = ax.hist(x_arr, bins, density=True)
print('numpy: \n', heights)
hist = h1(x_arr, bins, density=True)
print('physt: \n', hist.frequencies / sum(hist.frequencies))
And here are the outputs which are interestingly different:
numpy:
[0.00806452 0.01612903 0.02419355 0.02419355 0.03427419 0.02721774
0.00352823]
physt:
[0.01612903 0.01612903 0.0483871 0.09677419 0.27419355 0.43548387
0.11290323]

Plot Network statistics using matplotlib

I try to use matplotlib to print network statistics. I want to look it like line graphs created with excel.
Excel:
Matplotlib
[
My very simple code:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59])
y = np.array(['0.00', '0.00', '0.00', '0.12', '0.00', '0.00', '0.00', '14.75', '108.56', '78.91', '508.15', '79.66', '147.84', '199.87', '14.02', '10.05', '3411.12', '19735.23', '19929.51', '18428.82', '21727.14', '19716.41', '20295.20', '20283.08', '20088.10', '20155.81', '20108.67', '19954.45', '20316.46', '20045.77', '20233.71', '19981.40', '20230.02', '20099.69', '20000.23', '20234.06', '19763.92', '20458.40', '19626.22', '20542.25', '19821.72', '20443.78', '20109.41', '19918.96', '20223.37', '19933.64', '20023.73', '19655.67', '19890.94', '20590.04', '20158.37', '20001.59', '20011.48', '19785.95', '20550.63', '19687.02', '20025.00', '20478.25', '20124.66', '20148.08'])
plt.plot(x, y)
plt.xticks(x)
plt.show()
Your y is string type. Try y=y.astype(float) before plot, then you get the expected:

Image Output not properly displayed in Seaborn Bar graph

The below code snippet is displaying the plot image perfectly in Pycharm window, but the same image isn't appearing properly when it's saved in an image.
How I can save the image properly?
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
sns.set_context('paper')
report_id = ['Report_1', 'Report_2', 'Report_3', 'Report_4', 'Report_5', 'Report_6', 'Report_7', 'Report_8', 'Report_9',
'Report_10', 'Report_11', 'Report_12', 'Report_13', 'Report_14', 'Report_15', 'Report_16', 'Report_17',
'Report_18', 'Report_19', 'Report_20', 'Report_21', 'Report_22', 'Report_23', 'Report_24', 'Report_25',
'Report_26', 'Report_27', 'Report_28', 'Report_29', 'Report_30', 'Report_31', 'Report_32', 'Report_33',
'Report_34', 'Report_35', 'Report_36', 'Report_37', 'Report_38', 'Report_39', 'Report_40', 'Report_41',
'Report_42', 'Report_43', 'Report_44', 'Report_45', 'Report_46', 'Report_47', 'Report_48', 'Report_49',
'Report_50', 'Report_51', 'Report_52', 'Report_53', 'Report_54', 'Report_55', 'Report_56', 'Report_57',
'Report_58', 'Report_59', 'Report_60']
report_value = [1300, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
55, 56, 57, 58, 59, 60]
df = pd.DataFrame({'report_id': report_id, 'report_value': report_value})
sns.set(rc={'figure.figsize': (15, 100)})
ax = sns.barplot(y="report_id", x="report_value", data=df, palette="GnBu_d")
ax.tick_params(labelsize=3)
initialx = 0
for p in ax.patches:
ax.text(p.get_width(), initialx + p.get_height() / 10, "{:1.0f}".format(p.get_width()),fontsize=5)
initialx += 1
plt.savefig(r"C:\Program\Anaconda3\venvs\PlotGraph\Bar_Graph.png")
plt.show()
Pycharm Image:
Saved Image of Same plot:

Matplotlib scatter plot how to give same color for same size

I am having two arrays and a difference arrays of first two arrays
X = [1, 5, 63, 77, 103, 148, 156, 177, 183]
Y = [3, 46, 65, 87, 129, 150, 166, 181, 186]
Diff = [ 2 41 2 10 26 2 10 4 3 3]
How to plot a scatter plot for this data with x,y,diff where the same difference value show same color using matplotlib in python?
You should use 'c' parameter to color the differences.
Please see the below code:
X = [1, 5, 63, 77, 103, 148, 156, 177, 183]
Y = [3, 46, 65, 87, 129, 150, 166, 181, 186]
Diff = [ 2, 41, 2, 10, 26, 2, 10, 4, 3]
import matplotlib.pyplot as plt
plt.scatter(X,Y,c=Diff)

how can i find the highest even and odd number in 2D matrix using python3

how can i do this using simple code in python3
matrix = [[98, 19, 1, 46, 51, 33, 3, 33, 80, 40], [26, 88, 79, 10, 63, 76, 18, 49, 47, 44], [18, 53, 8, 96, 40, 53, 73, 8, 31, 43], [8, 40, 31, 98, 19, 39, 15, 9, 58, 32], [76, 45, 1, 5, 15, 14, 20, 88, 51, 48]
You can flatten your list via itertools.chain.from_iterable, then get the largest even and odd using %==0 for evens and %!=0 for odds:
import itertools
flat = list(itertools.chain.from_iterable(matrix))
even_max = max(i for i in flat if i%2==0)
odd_max = max(i for i in flat if i%2!=0)
>>> even_max
98
>>> odd_max
79
If you prefer to avoid itertools, you can flatten your 2d matrix by list comprehension:
flat = [v for i in matrix for v in i]
even_max = max(i for i in flat if i%2==0)
odd_max = max(i for i in flat if i%2!=0)
More options, with numpy (might be overkill for your matrix, but could be beneficial if your matrix were huge):
import numpy as np
m = np.array(matrix)
even_max = max(m[m%2==0])
odd_max = max(m[m%2!=0])

Resources