Python:Matplotlib update scatter graph in real time - python

I am creating a projectile motion simulator, using the scatter function in matplotlib. I have got a list of coordinates stored in an ordered dictionary but want to be able to add these coordinates one at a time with a given time interval.
PSEUDOCODE/Bad Python:
path = OrderedDict([(0.0707, 0.0702), (0.141, 0.139), (0.212, 0.208), (0.283, 0.275), (0.354, 0.341), (0.424, 0.407), (0.495, 0.471), (0.566, 0.534), (0.636, 0.597), (0.707, 0.658), (0.778, 0.719), (0.849, 0.778), (0.919, 0.836), (0.99, 0.894), (1.06, 0.95), (1.13, 1.01), (1.2, 1.06), (1.27, 1.11), (1.34, 1.17), (1.41, 1.22), (1.48, 1.27), (1.56, 1.32), (1.63, 1.37), (1.7, 1.41), (1.77, 1.46), (1.84, 1.51), (1.91, 1.55), (1.98, 1.6), (2.05, 1.64), (2.12, 1.68), (2.19, 1.72), (2.26, 1.76), (2.33, 1.8), (2.4, 1.84), (2.47, 1.87), (2.55, 1.91), (2.62, 1.95), (2.69, 1.98), (2.76, 2.01), (2.83, 2.04), (2.9, 2.08), (2.97, 2.11), (3.04, 2.13), (3.11, 2.16), (3.18, 2.19), (3.25, 2.22), (3.32, 2.24), (3.39, 2.27), (3.46, 2.29), (3.54, 2.31), (3.61, 2.33), (3.68, 2.35), (3.75, 2.37), (3.82, 2.39), (3.89, 2.41), (3.96, 2.42), (4.03, 2.44), (4.1, 2.45), (4.17, 2.47), (4.24, 2.48), (4.31, 2.49), (4.38, 2.5), (4.45, 2.51), (4.53, 2.52), (4.6, 2.53), (4.67, 2.53), (4.74, 2.54), (4.81, 2.54), (4.88, 2.55), (4.95, 2.55), (5.02, 2.55), (5.09, 2.55), (5.16, 2.55), (5.23, 2.55), (5.3, 2.55), (5.37, 2.54), (5.44, 2.54), (5.52, 2.53), (5.59, 2.53), (5.66, 2.52), (5.73, 2.51), (5.8, 2.5), (5.87, 2.49), (5.94, 2.48), (6.01, 2.47), (6.08, 2.46), (6.15, 2.44), (6.22, 2.43), (6.29, 2.41), (6.36, 2.39), (6.43, 2.38), (6.51, 2.36), (6.58, 2.34), (6.65, 2.32), (6.72, 2.3), (6.79, 2.27), (6.86, 2.25), (6.93, 2.22), (7.0, 2.2), (7.07, 2.17), (7.14, 2.14), (7.21, 2.11), (7.28, 2.08), (7.35, 2.05), (7.42, 2.02), (7.5, 1.99), (7.57, 1.96), (7.64, 1.92), (7.71, 1.89), (7.78, 1.85), (7.85, 1.81), (7.92, 1.77), (7.99, 1.73), (8.06, 1.69), (8.13, 1.65), (8.2, 1.61), (8.27, 1.57), (8.34, 1.52), (8.41, 1.48), (8.49, 1.43), (8.56, 1.38), (8.63, 1.33), (8.7, 1.28), (8.77, 1.23), (8.84, 1.18), (8.91, 1.13), (8.98, 1.08), (9.05, 1.02), (9.12, 0.968), (9.19, 0.911), (9.26, 0.854), (9.33, 0.796), (9.4, 0.737), (9.48, 0.677), (9.55, 0.616), (9.62, 0.554), (9.69, 0.491), (9.76, 0.427), (9.83, 0.361), (9.9, 0.295), (9.97, 0.229), (10.0, 0.161), (10.1, 0.0916), (10.2, 0.0217)])
for each in path:
graph.update(each)
time.sleep(0.1)
Any help would be much appreciated :)

This works for me: (at least for prototyping)
import matplotlib.pyplot as plt
path = [(0.0707, 0.0702), (0.141, 0.139), (0.212, 0.208), (0.283, 0.275), (0.354, 0.341), (0.424, 0.407), (0.495, 0.471), (0.566, 0.534), (0.636, 0.597), (0.707, 0.658), (0.778, 0.719), (0.849, 0.778), (0.919, 0.836), (0.99, 0.894), (1.06, 0.95), (1.13, 1.01), (1.2, 1.06), (1.27, 1.11), (1.34, 1.17), (1.41, 1.22), (1.48, 1.27), (1.56, 1.32), (1.63, 1.37), (1.7, 1.41), (1.77, 1.46), (1.84, 1.51), (1.91, 1.55), (1.98, 1.6), (2.05, 1.64), (2.12, 1.68), (2.19, 1.72), (2.26, 1.76), (2.33, 1.8), (2.4, 1.84), (2.47, 1.87), (2.55, 1.91), (2.62, 1.95), (2.69, 1.98), (2.76, 2.01), (2.83, 2.04), (2.9, 2.08), (2.97, 2.11), (3.04, 2.13), (3.11, 2.16), (3.18, 2.19), (3.25, 2.22), (3.32, 2.24), (3.39, 2.27), (3.46, 2.29), (3.54, 2.31), (3.61, 2.33), (3.68, 2.35), (3.75, 2.37), (3.82, 2.39), (3.89, 2.41), (3.96, 2.42), (4.03, 2.44), (4.1, 2.45), (4.17, 2.47), (4.24, 2.48), (4.31, 2.49), (4.38, 2.5), (4.45, 2.51), (4.53, 2.52), (4.6, 2.53), (4.67, 2.53), (4.74, 2.54), (4.81, 2.54), (4.88, 2.55), (4.95, 2.55), (5.02, 2.55), (5.09, 2.55), (5.16, 2.55), (5.23, 2.55), (5.3, 2.55), (5.37, 2.54), (5.44, 2.54), (5.52, 2.53), (5.59, 2.53), (5.66, 2.52), (5.73, 2.51), (5.8, 2.5), (5.87, 2.49), (5.94, 2.48), (6.01, 2.47), (6.08, 2.46), (6.15, 2.44), (6.22, 2.43), (6.29, 2.41), (6.36, 2.39), (6.43, 2.38), (6.51, 2.36), (6.58, 2.34), (6.65, 2.32), (6.72, 2.3), (6.79, 2.27), (6.86, 2.25), (6.93, 2.22), (7.0, 2.2), (7.07, 2.17), (7.14, 2.14), (7.21, 2.11), (7.28, 2.08), (7.35, 2.05), (7.42, 2.02), (7.5, 1.99), (7.57, 1.96), (7.64, 1.92), (7.71, 1.89), (7.78, 1.85), (7.85, 1.81), (7.92, 1.77), (7.99, 1.73), (8.06, 1.69), (8.13, 1.65), (8.2, 1.61), (8.27, 1.57), (8.34, 1.52), (8.41, 1.48), (8.49, 1.43), (8.56, 1.38), (8.63, 1.33), (8.7, 1.28), (8.77, 1.23), (8.84, 1.18), (8.91, 1.13), (8.98, 1.08), (9.05, 1.02), (9.12, 0.968), (9.19, 0.911), (9.26, 0.854), (9.33, 0.796), (9.4, 0.737), (9.48, 0.677), (9.55, 0.616), (9.62, 0.554), (9.69, 0.491), (9.76, 0.427), (9.83, 0.361), (9.9, 0.295), (9.97, 0.229), (10.0, 0.161), (10.1, 0.0916), (10.2, 0.0217)]
X = []
Y = []
for item in path:
X.append(item[0])
Y.append(item[1])
plt.ion() # turn interactive mode on
animated_plot = plt.plot(X, Y, 'ro')[0]
for i in range(len(X)):
animated_plot.set_xdata(X[0:i])
animated_plot.set_ydata(Y[0:i])
plt.draw()
plt.pause(0.1)

Related

Find local maxima in data from dataframe

Is there a way to find the local maxima from data I get from CSV file and put its value on the plot?
The x and y values that are in a pandas dataframe look something like this
x = 1598.78, 1596.85, 1594.92, 1592.99, 1591.07, 1589.14, 1587.21, 1585.28, 1583.35, 1581.42, 1579.49, 1577.57, 1575.64, 1573.71, 1571.78, 1569.85, 1567.92, 1565.99, 1564.07, 1562.14, 1560.21, 1558.28, 1556.35, 1554.42, 1552.49, 1550.57, 1548.64, 1546.71, 1544.78, 1542.85, 1540.92, 1538.99, 1537.07, 1535.14, 1533.21, 1531.28, 1529.35, 1527.42, 1525.49, 1523.57, 1521.64, 1519.71, 1517.78, 1515.85, 1513.92, 1511.99, 1510.07, 1508.14, 1506.21, 1504.28, 1502.35, 1500.42, 1498.49, 1496.57, 1494.64, 1492.71, 1490.78, 1488.85, 1486.92, 1484.99, 1483.07, 1481.14, 1479.21, 1477.28, 1475.35, 1473.42, 1471.49, 1469.57, 1467.64, 1465.71, 1463.78, 1461.85, 1459.92, 1457.99, 1456.07, 1454.14, 1452.21, 1450.28, 1448.35, 1446.42, 1444.49, 1442.57, 1440.64, 1438.71, 1436.78, 1434.85, 1432.92, 1430.99, 1429.07, 1427.14, 1425.21, 1423.28, 1421.35, 1419.42, 1417.49, 1415.57, 1413.64, 1411.71, 1409.78, 1407.85, 1405.92, 1403.99, 1402.07, 1400.14
y = 0.640, 0.624, 0.609, 0.594, 0.581, 0.569, 0.558, 0.547, 0.537, 0.530, 0.523, 0.516, 0.508, 0.502, 0.497, 0.491, 0.487, 0.484, 0.481, 0.480, 0.479, 0.482, 0.490, 0.503, 0.520, 0.542, 0.566, 0.586, 0.600, 0.606, 0.593, 0.569, 0.557, 0.548, 0.538, 0.531, 0.527, 0.524, 0.522, 0.522, 0.523, 0.525, 0.526, 0.527, 0.530, 0.534, 0.536, 0.539, 0.547, 0.553, 0.557, 0.563, 0.573, 0.599, 0.654, 0.738, 0.852, 0.891, 0.810, 0.744, 0.711, 0.694, 0.686, 0.683, 0.683, 0.690, 0.700, 0.706, 0.713, 0.723, 0.731, 0.732, 0.737, 0.756, 0.779, 0.786, 0.790, 0.794, 0.802, 0.815, 0.827, 0.832, 0.831, 0.826, 0.823, 0.828, 0.834, 0.834, 0.832, 0.832, 0.831, 0.825, 0.816, 0.804, 0.798, 0.794, 0.786, 0.775, 0.764, 0.752, 0.739, 0.722, 0.708, 0.697
and I'm trying to get something like this.
P.S. Note that numeric values were added with the plt.text function just to exemplify what I want.
x = [1598.78, 1596.85, 1594.92, 1592.99, 1591.07, 1589.14, 1587.21, 1585.28, 1583.35, 1581.42, 1579.49, 1577.57, 1575.64, 1573.71, 1571.78, 1569.85, 1567.92, 1565.99, 1564.07, 1562.14, 1560.21, 1558.28, 1556.35, 1554.42, 1552.49, 1550.57, 1548.64, 1546.71, 1544.78, 1542.85, 1540.92, 1538.99, 1537.07, 1535.14, 1533.21, 1531.28, 1529.35, 1527.42, 1525.49, 1523.57, 1521.64, 1519.71, 1517.78, 1515.85, 1513.92, 1511.99, 1510.07, 1508.14, 1506.21, 1504.28, 1502.35, 1500.42, 1498.49, 1496.57, 1494.64, 1492.71, 1490.78, 1488.85, 1486.92, 1484.99, 1483.07, 1481.14, 1479.21, 1477.28, 1475.35, 1473.42, 1471.49, 1469.57, 1467.64, 1465.71, 1463.78, 1461.85, 1459.92, 1457.99, 1456.07, 1454.14, 1452.21, 1450.28, 1448.35, 1446.42, 1444.49, 1442.57, 1440.64, 1438.71, 1436.78, 1434.85, 1432.92, 1430.99, 1429.07, 1427.14, 1425.21, 1423.28, 1421.35, 1419.42, 1417.49, 1415.57, 1413.64, 1411.71, 1409.78, 1407.85, 1405.92, 1403.99, 1402.07, 1400.14]
y = [0.640, 0.624, 0.609, 0.594, 0.581, 0.569, 0.558, 0.547, 0.537, 0.530, 0.523, 0.516, 0.508, 0.502, 0.497, 0.491, 0.487, 0.484, 0.481, 0.480, 0.479, 0.482, 0.490, 0.503, 0.520, 0.542, 0.566, 0.586, 0.600, 0.606, 0.593, 0.569, 0.557, 0.548, 0.538, 0.531, 0.527, 0.524, 0.522, 0.522, 0.523, 0.525, 0.526, 0.527, 0.530, 0.534, 0.536, 0.539, 0.547, 0.553, 0.557, 0.563, 0.573, 0.599, 0.654, 0.738, 0.852, 0.891, 0.810, 0.744, 0.711, 0.694, 0.686, 0.683, 0.683, 0.690, 0.700, 0.706, 0.713, 0.723, 0.731, 0.732, 0.737, 0.756, 0.779, 0.786, 0.790, 0.794, 0.802, 0.815, 0.827, 0.832, 0.831, 0.826, 0.823, 0.828, 0.834, 0.834, 0.832, 0.832, 0.831, 0.825, 0.816, 0.804, 0.798, 0.794, 0.786, 0.775, 0.764, 0.752, 0.739, 0.722, 0.708, 0.697]
import matplotlib.pyplot as plt
# The slope of a line is a measure of its steepness. Mathematically, slope is calculated as "rise over run" (change in y divided by change in x).
slope = [np.sign((y[i] - y[i-1]) / (x[i] - x[i-1])) for i in range(1, len(y))]
x_prev = slope[0]
optima_dic={'minima':[], 'maxima':[]}
for i in range(1, len(slope)):
if slope[i] * x_prev == -1: #slope changed
if x_prev == 1: # slope changed from 1 to -1
optima_dic['maxima'].append(i)
else: # slope changed from -1 to 1
optima_dic['minima'].append(i)
x_prev=-x_prev
from matplotlib.pyplot import text
plt.rcParams["figure.figsize"] = (20,10)
ix = 0
for x_, y_ in zip(x, y):
plt.plot(x_, y_, 'o--', color='grey')
if(ix in optima_dic['minima']):
plt.text(x_, y_, s = x_, fontsize=10)
ix += 1

Matplotlib: contour plot with data interpolation

I use scipy.interpolate.griddata to interpolate my data for contour plot. The data have different scale on x and y axes:
from scipy.interpolate import griddata
xx = [0.0493, 0.0458, 0.0425, 0.0394, 0.0365, 0.0337, 0.0311, 0.0286, 0.0262, 0.024, 0.0219, 0.0198, 0.0179, 0.016, 0.0143, 0.0126, 0.0109, 0.0094, 0.0079, 0.0064, 0.005, 0.0037, 0.0024, 0.0012, 0.0, 0.0663, 0.0637, 0.0613, 0.059, 0.0567, 0.0546, 0.0525, 0.0506, 0.0487, 0.0469, 0.0451, 0.0434, 0.0418, 0.0402, 0.0387, 0.0373, 0.0359, 0.0345, 0.0332, 0.0319, 0.0307, 0.0295, 0.0283, 0.0272, 0.0261, 0.0792, 0.0774, 0.0756, 0.0739, 0.0722, 0.0706, 0.0691, 0.0676, 0.0661, 0.0647, 0.0633, 0.062, 0.0607, 0.0594, 0.0582, 0.057, 0.0559, 0.0547, 0.0536, 0.0526, 0.0515, 0.0505, 0.0495, 0.0486, 0.0477, 0.0919, 0.0905, 0.0891, 0.0878, 0.0865, 0.0852, 0.084, 0.0828, 0.0816, 0.0805, 0.0794, 0.0783, 0.0772, 0.0762, 0.0752, 0.0742, 0.0732, 0.0723, 0.0714, 0.0705, 0.0696, 0.0688, 0.0679, 0.0671, 0.0663, 0.1044, 0.1033, 0.1022, 0.1011, 0.1, 0.099, 0.098, 0.097, 0.096, 0.0951, 0.0942, 0.0933, 0.0924, 0.0915, 0.0907, 0.0898, 0.089, 0.0882, 0.0874, 0.0867, 0.0859, 0.0852, 0.0845, 0.0837, 0.083, 0.1168, 0.1159, 0.1149, 0.114, 0.1132, 0.1123, 0.1114, 0.1106, 0.1098, 0.109, 0.1082, 0.1074, 0.1066, 0.1059, 0.1052, 0.1045, 0.1037, 0.1031, 0.1024, 0.1017, 0.1011, 0.1004, 0.0998, 0.0992, 0.0985, 0.1291, 0.1283, 0.1275, 0.1267, 0.126, 0.1252, 0.1245, 0.1238, 0.123, 0.1223, 0.1217, 0.121, 0.1203, 0.1197, 0.119, 0.1184, 0.1178, 0.1172, 0.1166, 0.116, 0.1154, 0.1148, 0.1143, 0.1137, 0.1132]
yy = [0.6137, 0.8211, 1.0277, 1.2338, 1.4393, 1.6444, 1.8489, 2.053, 2.2567, 2.4601, 2.6631, 2.8658, 3.0682, 3.2703, 3.4722, 3.6738, 3.8752, 4.0763, 4.2773, 4.4781, 4.6787, 4.8791, 5.0794, 5.2795, 5.4795, 0.3217, 0.5059, 0.694, 0.8859, 1.0812, 1.2799, 1.4816, 1.6861, 1.8934, 2.1033, 2.3155, 2.5301, 2.7467, 2.9655, 3.1861, 3.4086, 3.6328, 3.8586, 4.0861, 4.315, 4.5453, 4.777, 5.01, 5.2442, 5.4795, 0.2447, 0.4154, 0.5919, 0.7737, 0.9606, 1.1524, 1.3488, 1.5496, 1.7547, 1.9638, 2.1767, 2.3932, 2.6133, 2.8367, 3.0633, 3.293, 3.5257, 3.7611, 3.9993, 4.2401, 4.4833, 4.729, 4.977, 5.2272, 5.4795, 0.1814, 0.3467, 0.5184, 0.696, 0.8795, 1.0685, 1.2629, 1.4624, 1.6668, 1.876, 2.0897, 2.3079, 2.5302, 2.7567, 2.987, 3.2212, 3.4591, 3.7004, 3.9452, 4.1933, 4.4446, 4.6989, 4.9563, 5.2165, 5.4795, 0.1202, 0.2837, 0.4538, 0.6303, 0.8128, 1.0012, 1.1953, 1.3949, 1.5998, 1.8099, 2.0249, 2.2448, 2.4693, 2.6983, 2.9318, 3.1694, 3.4112, 3.657, 3.9066, 4.16, 4.417, 4.6776, 4.9416, 5.2089, 5.4795, 0.0598, 0.2232, 0.3932, 0.5697, 0.7525, 0.9413, 1.136, 1.3365, 1.5425, 1.754, 1.9706, 2.1924, 2.4191, 2.6506, 2.8867, 3.1275, 3.3726, 3.6221, 3.8757, 4.1334, 4.3951, 4.6606, 4.93, 5.203, 5.4795, 0.0, 0.1638, 0.3344, 0.5115, 0.695, 0.8848, 1.0806, 1.2823, 1.4897, 1.7028, 1.9212, 2.145, 2.3739, 2.6078, 2.8466, 3.0903, 3.3385, 3.5914, 3.8486, 4.1102, 4.376, 4.646, 4.9199, 5.1978, 5.4795]
vv = [0.4829, 0.5196, 0.5541, 0.5866, 0.6173, 0.6463, 0.6738, 0.6998, 0.7246, 0.7481, 0.7706, 0.7919, 0.8123, 0.8318, 0.8504, 0.8683, 0.8854, 0.9017, 0.9175, 0.9326, 0.9471, 0.9611, 0.9745, 0.9875, 1.0, 0.4229, 0.4512, 0.4782, 0.5041, 0.5288, 0.5525, 0.5752, 0.597, 0.618, 0.6381, 0.6575, 0.6761, 0.6941, 0.7114, 0.7282, 0.7443, 0.7599, 0.775, 0.7895, 0.8036, 0.8173, 0.8305, 0.8433, 0.8557, 0.8678, 0.4044, 0.4259, 0.4467, 0.4668, 0.4862, 0.505, 0.5231, 0.5407, 0.5578, 0.5743, 0.5903, 0.6059, 0.621, 0.6356, 0.6498, 0.6637, 0.6771, 0.6902, 0.703, 0.7154, 0.7274, 0.7392, 0.7507, 0.7618, 0.7727, 0.3883, 0.4056, 0.4225, 0.4388, 0.4548, 0.4703, 0.4854, 0.5001, 0.5144, 0.5283, 0.5419, 0.5552, 0.5681, 0.5808, 0.5931, 0.6051, 0.6169, 0.6284, 0.6396, 0.6506, 0.6613, 0.6718, 0.6821, 0.6921, 0.7019, 0.3725, 0.3871, 0.4014, 0.4153, 0.4289, 0.4422, 0.4551, 0.4678, 0.4802, 0.4924, 0.5042, 0.5159, 0.5272, 0.5384, 0.5493, 0.56, 0.5704, 0.5807, 0.5907, 0.6006, 0.6102, 0.6197, 0.629, 0.6381, 0.6471, 0.3569, 0.3697, 0.3821, 0.3943, 0.4063, 0.418, 0.4295, 0.4407, 0.4518, 0.4626, 0.4732, 0.4836, 0.4938, 0.5038, 0.5137, 0.5233, 0.5328, 0.5421, 0.5513, 0.5603, 0.5691, 0.5778, 0.5863, 0.5947, 0.6029, 0.3415, 0.3529, 0.3641, 0.375, 0.3858, 0.3964, 0.4068, 0.417, 0.427, 0.4368, 0.4465, 0.456, 0.4654, 0.4745, 0.4836, 0.4925, 0.5012, 0.5098, 0.5182, 0.5265, 0.5347, 0.5428, 0.5507, 0.5585, 0.5662]
N=500
data_points = (xx, yy)
grid_points = (np.linspace(min(xx), max(xx), N), np.linspace(min(yy), max(yy), N))
vi = griddata(data_points, vv,
(grid_points[0][None, :], grid_points[1][:, None]), method='cubic')
plot(xx,yy, '.k')
contour(grid_points[0], grid_points[1], vi)
But I got ugly sharped contours:
However, if I scale for example the y axis, like this yy = [v/50. for v in yy], I got the smoothed plot:
How to get the smoothed contours with original axes scales?

hbar bokeh 1.3.4 with categorical data

I am trying to build a categorical plot with hbar in bokeh, though it seems a bit odd that it does not follow the same concept as the vbar. I have tried few variations and I still have not been able to plot the data, I am only getting an empty canvas. If someone could help me out, it would be much appreciated.
I am using bokeh 1.3.4 in my system and in an webapp I am building in Flask, so it has to be either this version or below(feels a bit demanding, but it is software requirements).
I have done it with pandas_bokeh which makes it very simple, though I am adding interactivity to the plots to allow the viewer to play around and pandas_bokeh does the job and you ended up not learning it properly.
webapp draft so far
rX = df3.index.values
xL = ['Dublin New', 'Ireland New','Dublin Existing','Ireland Existing']
labelDict = {'2010': xL, '2011': xL, '2012': xL, '2013': xL, '2014': xL,'2015': xL,'2016': xL,'2017': xL, '2018': xL}
sourceT = ColumnDataSource(data=dict(x=df3.index.values,
y=df3['Dublin New'],
y1=df3['Ireland New'],
y2=df3['Dublin Existing'],
y3=df3['Ireland Existing']))
pT = figure(y_range=FactorRange(*labelDict), plot_height=350, plot_width=550, title='Properties Transactions in Ireland', tools='pan, wheel_zoom, box_zoom, reset')
pT.hbar(y=dodge('x', -0.5, range=pT.y_range), height=0.3, right='y', fill_color="#FDE724", source=sourceT)
#pT.hbar(y=dodge('x', -0.25, range=pT.y_range), height=0.3, right='y1', fill_color='#35B778', source=sourceT)
show(pT)
Here is the plot I would like to reproduce using bokeh, instead pandas_bokeh.
Thanks in advance for the help.
The value for the range parameter of the dodge should be the actual range:
range=pT.y_range # GOOD
You are passing the range end property, which is a number:
range=pT.y_range.end # BAD
EDIT: Without a complete, minimal reproducer, it is not possible to directly fix your code. The best that can be offered is a complete working example that demonstrates that hbar does work exactly equivalently to vbar, that hopefully will be useful to you by comparison, to figure out where your full code strays:
from bokeh.io import show
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.transform import dodge
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ['2015', '2016', '2017']
data = {'fruits' : fruits,
'2015' : [2, 1, 4, 3, 2, 4],
'2016' : [5, 3, 3, 2, 4, 6],
'2017' : [3, 2, 4, 4, 5, 3]}
source = ColumnDataSource(data=data)
p = figure(y_range=fruits, x_range=(0, 10), plot_width=250, title="Fruit Counts by Year",
toolbar_location=None, tools="")
p.hbar(y=dodge('fruits', -0.25, range=p.y_range), right='2015', height=0.2, source=source,
color="#c9d9d3")
p.hbar(y=dodge('fruits', 0.0, range=p.y_range), right='2016', height=0.2, source=source,
color="#718dbf")
p.hbar(y=dodge('fruits', 0.25, range=p.y_range), right='2017', height=0.2, source=source,
color="#e84d60")
p.y_range.range_padding = 0.1
p.ygrid.grid_line_color = None
show(p)
#bigreddot Thanks a lot. I followed your approach and it works smoothly. I will try to work reserve the axis as I would rather to have the years in the yaxis.
Anyhow I really appreciated your help.
varT = ['Dublin New', 'Ireland New', 'Dublin Existing','Ireland Existing']
yearsT = df3.index.values.tolist()
dataT = {'var': var,
'2010': df3.iloc[0].values, '2011': df3.iloc[1].values,
'2012': df3.iloc[2].values, '2013': df3.iloc[3].values,
'2014': df3.iloc[4].values, '2015': df3.iloc[5].values,
'2016': df3.iloc[6].values, '2017': df3.iloc[7].values,
'2018': df3.iloc[8].values,
}
sourceTs = ColumnDataSource(data=dataT)
pT1 = figure(y_range=var, x_range=(0, df3.values.max()), plot_width=450, title='Properties Transactions in Ireland', tools='pan, wheel_zoom, box_zoom, reset')
pT1.hbar(y=dodge('var', -0.4, range=pT1.y_range), right='2010', height=0.1, source=sourceTs, color='#440154', legend=value('2010'))
pT1.hbar(y=dodge('var', -0.3, range=pT1.y_range), right='2011', height=0.1, source=sourceTs, color='#46317E', legend=value('2011'))
pT1.hbar(y=dodge('var', -0.2, range=pT1.y_range), right='2012', height=0.1, source=sourceTs, color='#365A8C', legend=value('2012'))
pT1.hbar(y=dodge('var', -0.1, range=pT1.y_range), right='2013', height=0.1, source=sourceTs, color='#277E8E', legend=value('2013'))
pT1.hbar(y=dodge('var', 0, range=pT1.y_range), right='2014', height=0.1, source=sourceTs, color='#1EA087', legend=value('2014'))
pT1.hbar(y=dodge('var', 0.1, range=pT1.y_range), right='2015', height=0.1, source=sourceTs, color='#49C16D', legend=value('2015'))
pT1.hbar(y=dodge('var', 0.2, range=pT1.y_range), right='2016', height=0.1, source=sourceTs, color='#9DD93A', legend=value('2016'))
pT1.hbar(y=dodge('var', 0.3, range=pT1.y_range), right='2017', height=0.1, source=sourceTs, color='#FDE724', legend=value('2017'))
pT1.hbar(y=dodge('var', 0.4, range=pT1.y_range), right='2018', height=0.1, source=sourceTs, color='#AADB32', legend=value('2018'))
pT1.legend.location='bottom_right'
#pT1.y_range.range_padding = 0.1
pT1.grid.grid_line_color = None
tick_labels_pt = {'10000':'10K','20000':'20K','30000':'30K','40000':'40K','50000':'50K'}
pT1.xaxis.major_label_overrides = tick_labels_pt
pT1.legend.background_fill_alpha=None
pT1.legend.border_line_alpha=0
pT1.legend.label_text_font_size = "11px"
pT1.legend.click_policy="hide"
pT1.title.text_font_size = '15px'
pT1.axis.major_label_text_font_style = 'bold'
#pT1.xaxis.major_label_text_font_style = 'bold'
pT1.toolbar.autohide = True
show(pT1)
Here is the outcome:
with the axis reversed:
varpti = ['Dublin New', 'Ireland New', 'Dublin Existing','Ireland Existing']
#the values of the y axis has to be in str format
yearspti = '2010','2011','2012', '2013', '2014', '2015', '2016', '2017', '2018' #df3.index.values.tolist()
datapti = {'years': yearspti,
'Dublin New': df3['Dublin New'].values,
'Ireland New': df3['Ireland New'].values,
'Dublin Existing': df3['Dublin Existing'].values,
'Ireland Existing': df3['Ireland Existing'].values
}
sourcepti = ColumnDataSource(data=datapti)
pti = figure(y_range=yearspti, x_range=(0, df3.values.max()), plot_height=500, plot_width=450, title='Properties Transactions in Ireland', tools='pan, wheel_zoom, box_zoom, reset')
pti.hbar(y=dodge('years', -0.2, range=pti.y_range), right='Dublin New', height=0.1, source=sourcepti, color='#440154', legend=value('Dublin New'))
pti.hbar(y=dodge('years', 0, range=pti.y_range), right='Ireland New', height=0.1, source=sourcepti, color='#30678D', legend=value('Ireland New'))
pti.hbar(y=dodge('years', 0.2, range=pti.y_range), right='Dublin Existing', height=0.1, source=sourcepti, color='#35B778', legend=value('Dublin Exsiting'))
pti.hbar(y=dodge('years', 0.4, range=pti.y_range), right='Ireland Existing', height=0.1, source=sourcepti, color='#FDE724', legend=value('Ireland Exsiting'))

How to plot graph using two lists where first list is normal list and second list contains list of lists

I want to plot graph using two lists x and y where-
x=['18ww25', '18ww27', '18ww28', '18ww28.1', '18ww29', '18ww29.1', '18ww29.2']
y=[['27%', '27%', '27%', '27%', '27%', '27%', '27%'], ['18%', '18%', '20%', '23%', '30%', '30%', '30%'], ['24%', '25%', '28%', '32%', '39%', '39%', '43%'], ['43%', '43%', '44%', '44%', '43%', '45%', '45%'], ['0%', '0%', '0%', '0%', '0%', '0%', '0%'], ['14%', '14%', '16%', '16%', '16%', '17%', '17%'], ['14%', '14%', '14%', '14%', '14%', '14%', '14%'], ['0%', '0%', '0%', '0%', '0%', '0%', '0%'], ['8%', '8%', '8%', '8%', '8%', '8%', '8%']]
I have to plot graph with each x and y pair . I have to plot list x with every list of y in the same graph, such as: (x,y1) (x,y2) ... (x,y9) (as total 9 elements are there in y).
The code that I am using right now is:
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("A test graph")
for i in range(len(y)):
plt.plot(x,[pt[i] for pt in y],label = 'id %s'%i)
plt.legend()
plt.show()
but this code is showing error as
x and y must have same first dimension, but have shapes (7,) and (9,)
How can I plot this?
It looks like you want to plot
for i in range(len(y)):
plt.plot(x, y[i], label = 'id %s'%i)
However you would probably change the y values to numbers first, else they would not be to scale. In total that may look like
import matplotlib.pyplot as plt
x=['18ww25', '18ww27', '18ww28', '18ww28.1', '18ww29', '18ww29.1', '18ww29.2']
y=[['27%', '27%', '27%', '27%', '27%', '27%', '27%'],
['18%', '18%', '20%', '23%', '30%', '30%', '30%'],
['24%', '25%', '28%', '32%', '39%', '39%', '43%'],
['43%', '43%', '44%', '44%', '43%', '45%', '45%'],
['0%', '0%', '0%', '0%', '0%', '0%', '0%'],
['14%', '14%', '16%', '16%', '16%', '17%', '17%'],
['14%', '14%', '14%', '14%', '14%', '14%', '14%'],
['0%', '0%', '0%', '0%', '0%', '0%', '0%'],
['8%', '8%', '8%', '8%', '8%', '8%', '8%']]
z = [list(map(lambda s: float(s.split("%")[0]),yi)) for yi in y]
plt.xlabel("X-axis")
plt.ylabel("Y-axis [%]")
plt.title("A test graph")
for i in range(len(z)):
plt.plot(x,z[i],label = 'id %s'%i)
plt.legend()
plt.show()

Writing numbers into file python

I was trying to extract all the elements of the my data points (x,y) tuples, and put them into list of x values and y list, and transfer them to two columns in excel spreadsheet. It seems writing numbers into file is quite difficult. Can anyone shed a light on this problem?
Current state:
xlist=[list[i][0] for i in range(len(list))]
ylist=[list[i][1] for i in range(len(list))]
fob=open('c:/test/a.txt','w')
fob.write(xlist[i] for i in range(len(xlist))
i want to write down a column of numbers in notepad so that I can highlight and copy into spread sheet directly .
Below are my data.
list = [(0.496, 12.49), (0.531, 12.40), (0.578, 12.18), (0.615,
11.96), (0.657, 11.75), (0.731, 11.28), (0.785, 10.85), (0.812,
10.61), (0.883, 9.92), (0.930, 9.40), (0.979, 8.77), (1.026,
8.10), (1.081, 7.23), (1.134, 6.33), (1.189, 5.39), (1.220,
4.85), (1.273, 3.92), (1.332, 2.91), (1.364, 2.55), (1.418,
2.16), (1.467, 1.65), (1.523, 1.17), (1.569, 0.82), (1.626,
0.47), (1.678, 0.21), (1.723, 0.01), (1.776, 0.19), (1.814,
0.28), (1.869, 0.36), (1.933, 0.36), (1.972, 0.31), (2.021,
0.18), (2.081, 0.13), (2.129, 0.46), (2.169, 0.79), (2.219,
1.24), (2.280, 1.84), (2.306, 2.11), (2.358, 2.67), (2.414,
3.37), (2.471, 4.05), (2.505, 4.51), (2.562, 5.22), (2.613,
5.84), (2.652, 6.31), (2.712, 7.01), (2.758, 7.52), (2.802,
7.99), (2.869, 8.63), (2.930, 9.16), (2.971, 9.57), (3.043,
10.35), (3.078, 10.69), (3.119, 11.00), (3.174, 11.26), (3.217,
11.40), (3.261, 11.53), (3.307, 11.55), (3.371, 11.51), (3.432,
11.40), (3.479, 11.26), (3.507, 11.20), (3.557, 11.00), (3.623,
10.55), (3.663, 10.28), (3.729, 9.79), (3.768, 9.57), (3.825,
9.24), (3.880, 8.85), (3.944, 8.41), (3.969, 8.04), (4.014,
7.55), (4.086, 6.67), (4.105, 6.37), (4.166, 5.50), (4.212,
4.88), (4.266, 4.20), (4.311, 3.69), (4.364, 3.06), (4.401,
2.65), (4.453, 2.09), (4.497, 1.68), (4.556, 1.18), (4.602,
0.85), (4.644, 0.57), (4.695, 0.29), (4.754, 0.04), (4.799,
0.11), (4.847, 0.17), (4.918, 0.11), (4.959, 0.04), (4.992,
0.19), (5.063, 0.64), (5.098, 0.90), (5.157, 1.40), (5.201,
1.79), (5.245, 2.20), (5.291, 2.65), (5.326, 3.00), (5.387,
3.65), (5.420, 4.02), (5.469, 4.62), (5.538, 5.44), (5.579,
5.96), (5.629, 6.57), (5.674, 7.14), (5.724, 7.73), (5.798,
8.60), (5.823, 8.88), (5.888, 9.62), (5.919, 9.94), (5.963,
10.41), (6.009, 10.85), (6.050, 11.22), (6.115, 11.71), (6.153,
11.99), (6.222, 12.39), (6.263, 12.61), (6.302, 12.77), (6.377,
12.99), (6.414, 13.03), (6.454, 13.02), (6.522, 12.89), (6.558,
12.74), (6.626, 12.41), (6.677, 12.05), (6.729, 11.64), (6.791,
11.00), (6.832, 10.58), (6.887, 9.92), (6.949, 9.13), (6.996,
8.48), (7.028, 8.09), (7.094, 7.13), (7.123, 6.70), (7.161,
6.16), (7.213, 5.35), (7.250, 4.81), (7.332, 3.61), (7.382,
2.93), (7.420, 2.45), (7.474, 1.88), (7.514, 1.40), (7.576,
0.71), (7.600, 0.50), (7.662, 0.12), (7.725, 0.16), (7.768,
0.26), (7.810, 0.30), (7.858, 0.26), (7.904, 0.18), (7.980,
0.10), (8.021, 0.29), (8.078, 0.65), (8.133, 1.06), (8.165,
1.33), (8.218, 1.83), (8.267, 2.31), (8.321, 2.87), (8.355,
3.27), (8.413, 3.91), (8.473, 4.61), (8.519, 5.22), (8.553,
5.65), (8.643, 6.74), (8.678, 7.23), (8.734, 7.94), (8.760,
8.27), (8.803, 8.81), (8.851, 9.35), (8.905, 9.94), (8.961,
10.45), (9.009, 10.92), (9.053, 11.34), (9.106, 11.75), (9.166,
12.14), (9.228, 12.48), (9.292, 12.71), (9.340, 12.86), (9.384,
13.01), (9.412, 13.05), (9.452, 13.03), (9.472, 13.00)]
Cheers
Export it into a CSV file. Your use case is very simple and you should be able to do it using standard Python.
with open('output.csv', 'w') as f:
for x, y in l:
f.write("%s, %s\n" % (x, y))
Note: list is a reserved word in python and you should not be using it.
Use openpyxl to write .xslx files from Python:
import openpyxl
my_list = [(0.496, 12.49), (0.531, 12.40), (0.578, 12.18), (0.615,
11.96), (0.657, 11.75), (0.731, 11.28), (0.785, 10.85), (0.812,
10.61), (0.883, 9.92), (0.930, 9.40), (0.979, 8.77), (1.026,
8.10), (1.081, 7.23), (1.134, 6.33), (1.189, 5.39), (1.220,
4.85), (1.273, 3.92), (1.332, 2.91), (1.364, 2.55), (1.418,
2.16), (1.467, 1.65), (1.523, 1.17), (1.569, 0.82), (1.626,
0.47), (1.678, 0.21), (1.723, 0.01), (1.776, 0.19), (1.814,
0.28), (1.869, 0.36), (1.933, 0.36), (1.972, 0.31), (2.021,
0.18), (2.081, 0.13), (2.129, 0.46), (2.169, 0.79), (2.219,
1.24), (2.280, 1.84), (2.306, 2.11), (2.358, 2.67), (2.414,
3.37), (2.471, 4.05), (2.505, 4.51), (2.562, 5.22), (2.613,
5.84), (2.652, 6.31), (2.712, 7.01), (2.758, 7.52), (2.802,
7.99), (2.869, 8.63), (2.930, 9.16), (2.971, 9.57), (3.043,
10.35), (3.078, 10.69), (3.119, 11.00), (3.174, 11.26), (3.217,
11.40), (3.261, 11.53), (3.307, 11.55), (3.371, 11.51), (3.432,
11.40), (3.479, 11.26), (3.507, 11.20), (3.557, 11.00), (3.623,
10.55), (3.663, 10.28), (3.729, 9.79), (3.768, 9.57), (3.825,
9.24), (3.880, 8.85), (3.944, 8.41), (3.969, 8.04), (4.014,
7.55), (4.086, 6.67), (4.105, 6.37), (4.166, 5.50), (4.212,
4.88), (4.266, 4.20), (4.311, 3.69), (4.364, 3.06), (4.401,
2.65), (4.453, 2.09), (4.497, 1.68), (4.556, 1.18), (4.602,
0.85), (4.644, 0.57), (4.695, 0.29), (4.754, 0.04), (4.799,
0.11), (4.847, 0.17), (4.918, 0.11), (4.959, 0.04), (4.992,
0.19), (5.063, 0.64), (5.098, 0.90), (5.157, 1.40), (5.201,
1.79), (5.245, 2.20), (5.291, 2.65), (5.326, 3.00), (5.387,
3.65), (5.420, 4.02), (5.469, 4.62), (5.538, 5.44), (5.579,
5.96), (5.629, 6.57), (5.674, 7.14), (5.724, 7.73), (5.798,
8.60), (5.823, 8.88), (5.888, 9.62), (5.919, 9.94), (5.963,
10.41), (6.009, 10.85), (6.050, 11.22), (6.115, 11.71), (6.153,
11.99), (6.222, 12.39), (6.263, 12.61), (6.302, 12.77), (6.377,
12.99), (6.414, 13.03), (6.454, 13.02), (6.522, 12.89), (6.558,
12.74), (6.626, 12.41), (6.677, 12.05), (6.729, 11.64), (6.791,
11.00), (6.832, 10.58), (6.887, 9.92), (6.949, 9.13), (6.996,
8.48), (7.028, 8.09), (7.094, 7.13), (7.123, 6.70), (7.161,
6.16), (7.213, 5.35), (7.250, 4.81), (7.332, 3.61), (7.382,
2.93), (7.420, 2.45), (7.474, 1.88), (7.514, 1.40), (7.576,
0.71), (7.600, 0.50), (7.662, 0.12), (7.725, 0.16), (7.768,
0.26), (7.810, 0.30), (7.858, 0.26), (7.904, 0.18), (7.980,
0.10), (8.021, 0.29), (8.078, 0.65), (8.133, 1.06), (8.165,
1.33), (8.218, 1.83), (8.267, 2.31), (8.321, 2.87), (8.355,
3.27), (8.413, 3.91), (8.473, 4.61), (8.519, 5.22), (8.553,
5.65), (8.643, 6.74), (8.678, 7.23), (8.734, 7.94), (8.760,
8.27), (8.803, 8.81), (8.851, 9.35), (8.905, 9.94), (8.961,
10.45), (9.009, 10.92), (9.053, 11.34), (9.106, 11.75), (9.166,
12.14), (9.228, 12.48), (9.292, 12.71), (9.340, 12.86), (9.384,
13.01), (9.412, 13.05), (9.452, 13.03), (9.472, 13.00)]
book = openpyxl.Workbook()
sheet = book.active
for i, value in enumerate(my_list):
sheet.cell(row=i+1, column=1).value = value[0]
sheet.cell(row=i+1, column=2).value = value[1]
book.save('test.xlsx')
When you have data like numbers or objects in memory, it's generally not correct to dump that data directly into disk, you'll want to serialize it.
The easiest way to serialize it is with print which automatically calls the "serialization" method __str__. The problem with this serialization method is that's not always easy to deserialize.
When you have a data structure, like the matrix you describe, you'll want a serialization method that will preserve the structure and allow to reconstruct it in memory. In this case you can use CSV (through the csv module), JSON (through the json module) or many others.
Use CSV.

Categories

Resources