Rank line plot customization - python

Currently I'm trying to plot a graph showing the rank of some equipment in operation, the rank goes from 1 to 300 (1 is the best, 300 is the worst) over a few days (df columns). What I'm trying to do, is a graph similar to this:
And what I got is this:
I would like to make the lines inclined as it is on the first graph instead of vertical, but I can't figure it out how. I found the base for the first graph on this question here and I started the code from there, this is what I end up having:
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
import numpy as np
def energy_rank(data, marker_width=.5, color='blue'):
y_data = np.repeat(data, 2)
x_data = np.empty_like(y_data)
x_data[0::2] = np.arange(1, len(data)+1) - (marker_width/2)
x_data[1::2] = np.arange(1, len(data)+1) + (marker_width/2)
lines = []
lines.append(plt.Line2D(x_data, y_data, lw=0.8, linestyle='dashed', color=color,alpha=1,marker='.'))
for x in range(0,len(data)*2, 2):
lines.append(plt.Line2D(x_data[x:x+2], y_data[x:x+2], lw=2, linestyle='solid', color=color))
return lines
head = 8
dfPlot = vazio.sort_values(dia, ascending = True).head(head)
data = dfPlot.to_numpy()
colorsHEX=('#FE5815','#001A70','#2F5C22','#B01338','#00030D','#2DE1FC','#2E020C','#B81D8C')
artists = []
for row, color in zip(data, colorsHEX):
artists.extend(energy_rank(row, color=color))
eixoXDatas = pd.to_datetime(list(vazio.columns),format='%d/%m/%y').strftime('%d/%b')
fig, ax = plt.subplots()
plt.xticks(np.arange(len(vazio.columns)),
eixoXDatas,
rotation = 35,
fontsize = 14)
plt.yticks(fontsize = 14)
plt.xlabel('Dias', fontsize=18)
plt.ylabel('Ranking', fontsize=18)
fig = plt.gcf()
fig.set_size_inches(16, 8)
for artist in artists:
ax.add_artist(artist)
ax.set_ybound([0,15])
ax.set_ylim(ax.get_ylim()[::-1])
ax.set_xbound([-0.1,float(len(vazio.columns))+2.5])
plt.yticks(np.arange(1,16,step=1))
ax.grid(axis='y',alpha=0.5)
lastDay = vazio.sort_values(vazio.iloc[:,-1:].columns.values[0], ascending = True).iloc[:,-1:]
lastDay = lastDay.head(head)
for inverter, pos in lastDay.iterrows():
ax.annotate(inverter, xy =(plt.gca().get_xlim()[1]-2.4, pos), color=colorsHEX[int(pos)-1])
I tried implementing on energy_rank function, removing the +/- parts on x_data but I only could end up with inclined lines with dots instead of the horizontal lines. Can anyone help me out how can I mantain the horziontal lines and instead of vertical dashed lines, implement inclined lines as the example above?
I imagine that is vertical because the points change on top of the x ticks. If you observe the 1st image, the horizontal bars are centralized on each x tick, so the lines "have some room" to be inclined.
vazio dataframe is as follows (contains the rank of each equipment):
Equipment 21-03-27 21-03-28 21-03-29 21-03-30 21-03-31 21-04-01 21-04-02
P01-INV-1-1 1 1 1 1 1 2 2
P01-INV-1-2 2 2 4 4 5 1 1
P01-INV-1-3 4 4 3 5 6 10 10

Here is an adaption of your energy_rank function creating horizontal line segments together with their connections. The line drawing part is inspired by this tutorial example. Optionally the area below the lines can be filled.
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
import numpy as np
def energy_rank(data, marker_width=.5, color='blue', ax=None, fill=False):
ax = ax or plt.gca()
y = data
x = np.arange(1, len(data) + 1)
segments1 = np.array([x - marker_width / 2, y, x + marker_width / 2, y]).T.reshape(-1, 2, 2)
lc1 = LineCollection(segments1, color=color)
lc1.set_linewidth(2)
lc1.set_linestyle('-')
lines_hor = ax.add_collection(lc1)
segments2 = np.array([x[:-1] + marker_width / 2, y[:-1], x[1:] - marker_width / 2, y[1:]]).T.reshape(-1, 2, 2)
lc2 = LineCollection(segments2, color=color)
lc2.set_linewidth(0.5)
lc2.set_linestyle('--')
lines_connect = ax.add_collection(lc2)
if fill:
ax.fill_between(segments1.reshape(-1,2)[:,0], segments1.reshape(-1,2)[:,1],
color=color, alpha=0.05)
return lines_hor, lines_connect
fig, ax = plt.subplots()
M, N = 5, 25
y = np.random.uniform(-2, 2, (M, N)).cumsum(axis=1)
y += np.random.uniform(0.5, 2, (M, 1)) - y.min(axis=1, keepdims=True)
colorsHEX = ('#FE5815', '#001A70', '#2F5C22', '#B01338', '#00030D')
for yi, color in zip(y, colorsHEX):
energy_rank(yi, ax=ax, color=color)
ax.set_xlim(0, N + 1)
ax.set_ylim(0, y.max() + 1)
plt.show()

Related

Colored gradient on multiple lines from a csv in 3D plot?

Disclaimer: I'm a total newb to this, 2nd day so pls bear with me, thank you in advance!
So, I managed to get my 3D plot to have multiple lines, but I would like to give them some color gradients. I've managed to get it onto one example line, but I cannot convert it to my own plots.
My plots come from a .csv
I followed this question for the gradients: https://stackoverflow.com/a/8505774/20387853 (Answer by Yann) but I can't seem to understand how to merge the two for i in range bits (one from my old code with the new code) (if it even can be?)
I also dont understand ax.plot(x[i:i+2],y[i:i+2]) so I couldn't adjust this like I thought I could.
SO ATM i have two scripts
Script 1 - in which I'm trying to merge my two data sets.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import sys
import pandas
points = pandas.read_csv('D:Documents\PYTHON_FILES/test3d.csv')
def highResPoints(x,y,factor=10):
# r is the distance spanned between pairs of points
r = [0]
for i in range(1,len(x)):
dx = x[i]-x[i-1]
dy = y[i]-y[i-1]
r.append(np.sqrt(dx*dx+dy*dy))
r = np.array(r)
# rtot is a cumulative sum of r, it's used to save time
rtot = []
for i in range(len(r)):
rtot.append(r[0:i].sum())
rtot.append(r.sum())
dr = rtot[-1]/(NPOINTS*RESFACT-1)
xmod=[x[0]]
ymod=[y[0]]
rPos = 0 # current point on walk along data
rcount = 1
while rPos < r.sum():
x1,x2 = x[rcount-1],x[rcount]
y1,y2 = y[rcount-1],y[rcount]
dpos = rPos-rtot[rcount]
theta = np.arctan2((x2-x1),(y2-y1))
rx = np.sin(theta)*dpos+x1
ry = np.cos(theta)*dpos+y1
xmod.append(rx)
ymod.append(ry)
rPos+=dr
while rPos > rtot[rcount+1]:
rPos = rtot[rcount+1]
rcount+=1
if rcount>rtot[-1]:
break
return xmod,ymod
#CONSTANTS
NPOINTS = 10
COLOR='red'
RESFACT=10
MAP='winter' # choose carefully, or color transitions will not appear smoooth
cm = plt.get_cmap(MAP)
################ These are old data sets, just to use for this example
x = points['x'].values
y = points['y'].values
z = points['z'].values
x2 = points['x2'].values
y2 = points['y2'].values
z2 = points['z2'].values
fig = plt.figure()
#ax1 = fig.add_subplot(111,projection='3d') # regular resolution color map
ax = fig.add_subplot(111, projection='3d')
ax.plot(x, y, z, c='red',marker='v', linewidth=1.0, markersize=2)
ax.plot(x2, y2, z2, c='blue', marker='o', linewidth=1.0, markersize=2)
ax.set_prop_cycle(color=[cm(1.*i/(NPOINTS-1)) for i in range(NPOINTS-1)])
for i in range(NPOINTS-1):
#ax1.plot(x[i:i+2],y[i:i+2])
ax.plot(x[i:i+2],y[i:i+2])
########################The part I want to merge in
#for i in range(1, 5):
#if i == 1: i = '' #x is your first value not x1
#ax.plot(points[f"x{i}"], points[f"y{i}"], points[f"z{i}"], c='red', marker='o', linewidth=1.0, markersize=2)
#########################
fig.savefig('colorgradienttest.png')
plt.show()
[Link to Image]
I want to make the blue and red lines have a color gradient like the example 3rd line (markers are not important)
Script 2 - to which I want to apply the gradient (the one with the .csv)
from mpl_toolkits.mplot3d import Axes3D
import sys
import matplotlib.pyplot as plt
import pandas
import numpy as np
points = pandas.read_csv('D:Documents\PYTHON_FILES/test3d.csv')
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
#OPTION 1 - not sure why this isn't working for me so Im not using it yet
#for idx in range(29):
# suffix = '' if idx == 0 else str(idx + 1) # ranges start at 0
# x = points[f"x{suffix}"].values
# y = points[f"y{suffix}"].values
# z = points[f"z{suffix}"].values
#ax.plot(x, y, z, c='red', marker='o', linewidth=1.0, markersize=2)
#OPTION 2 - current approach <<<<<<<<<<<<<<<< want to apply gradient to this segment
for i in range(1, 5):
if i == 1: i = '' #x is your first value not x1
ax.plot(points[f"x{i}"], points[f"y{i}"], points[f"z{i}"], c='red', marker='o', linewidth=1.0, markersize=2)
plt.show()

Plotting a line between 2-D point to its corresponding value in 3-D

I am trying to plot a dashed line in a 3-D Matplotlib plot. I would like to get a dashed line between each (x_pt, y_pt) to its corresponding z_pt.
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import matplotlib
matplotlib.rcParams['mathtext.fontset'] = 'cm'
matplotlib.rcParams['axes.labelsize'] = 13
def z_function(x, y):
a = 1
b = 5.1/(4*np.pi**2)
c = 5/np.pi
r = 6
s = 10
t = 1/(8*np.pi)
return a*(y - b*x**2 + c*x - r)**2 + s*(1 - t)*np.cos(x) + s
x = np.linspace(-5, 10, 100)
y = np.linspace(0, 15, 100)
indexes = np.random.randint(0, 100, 5)
x_pt = x[indexes]
y_pt = y[indexes]
z_pt = z_function(x_pt, y_pt)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(x_pt, y_pt, color='k', marker='x', depthshade=False)
ax.scatter(x_pt, y_pt, z_pt, color='k', marker='^', depthshade=False)
ax.set_xticks([-5, 0, 5, 10])
ax.set_yticks([0, 5, 10, 15])
ax.set_zticks([100, 200, 300])
ax.view_init(30, -120)
ax.set_xlabel(r'$x_1$')
ax.set_ylabel(r'$x_2$')
ax.zaxis.set_rotate_label(False)
ax.set_zlabel(r'$f(x)$', rotation=0)
ax.w_xaxis.pane.fill = False
ax.w_yaxis.pane.fill = False
ax.w_zaxis.pane.fill = False
plt.show()
Can anyone help me with this?
If I understand your problem correctly, you need to connect the point (x,y,0) to (x,y,z) like so:
for x_,y_,z_ in zip(x_pt, y_pt, z_pt):
ax.plot([x_,x_],[y_,y_],[0,z_], '--', c='grey')
It should be as simple as:
ax.plot(x_pt, y_pt, zs=z_pt, color='blue', marker='--', depthshade=False)
alternatively using:
ax.plot3D(x_pt, y_pt, z_pt, marker='--')
UPDATE:
You will need to create extra dummy coordinates for each point on the x-y axis, like so:
import numpy as np
n = 10 # number of points in the line
for i in len(x_pt):
x_range = np.linspace(0, x_pt[i], n)
y_range = np.linspace(0, y_pt[i], n)
ax.plot3D(x_range, y_range, [z_pt[i]]*n, marker='--')
NOTE: Untested

Matplotlib: Shared axis for imshow images

I'm trying to plot multiple images with Matplotlib's imshow() method, and have them share a single y axis. Although the images have the same number of y pixels, the images don't end up the same height.
Demonstration code;
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import poisson
def ibp_oneparam(alpha, N):
"""One-parameter IBP"""
# First customer
Z = np.array([np.ones(poisson(alpha).rvs(1))], dtype=int)
# ith customer
for i in range(2, N+1):
# Customer walks along previously sampled dishes
z_i = []
for previously_sampled_dish in Z.T:
m_k = np.sum(previously_sampled_dish)
if np.random.rand() >= m_k / i:
# Customer decides to sample this dish
z_i.append(1.0)
else:
# Customer decides to skip this dish
z_i.append(0.0)
# Customer decides to try some new dishes
z_i.extend(np.ones(poisson(alpha / i).rvs(1)))
z_i = np.array(z_i)
# Add this customer to Z
Z_new = np.zeros((
Z.shape[0] + 1,
max(Z.shape[1], len(z_i))
))
Z_new[0:Z.shape[0], 0:Z.shape[1]] = Z
Z = Z_new
Z[i-1, :] = z_i
return Z
np.random.seed(3)
N = 10
alpha = 2.0
#plt.figure(dpi=100)
fig, (ax1, ax2, ax3) = plt.subplots(
1,
3,
dpi=100,
sharey=True
)
Z = ibp_oneparam(alpha, N)
plt.sca(ax1)
plt.imshow(
Z,
extent=(0.5, Z.shape[1] + 0.5, len(Z) + 0.5, 0.5),
cmap='Greys_r'
)
plt.ylabel("Customers")
plt.xlabel("Dishes")
plt.xticks(range(1, Z.shape[1] + 1))
plt.yticks(range(1, Z.shape[0] + 1))
Z = ibp_oneparam(alpha, N)
plt.sca(ax2)
plt.imshow(
Z,
extent=(0.5, Z.shape[1] + 0.5, len(Z) + 0.5, 0.5),
cmap='Greys_r'
)
plt.xlabel("Dishes")
plt.xticks(range(1, Z.shape[1] + 1))
Z = ibp_oneparam(alpha, N)
plt.sca(ax3)
plt.imshow(
Z,
extent=(0.5, Z.shape[1] + 0.5, len(Z) + 0.5, 0.5),
cmap='Greys_r'
)
plt.xlabel("Dishes")
plt.xticks(range(1, Z.shape[1] + 1))
plt.show()
Output;
I expect these images to each have the same height, and have varying widths. How can I achieve this?
Aside: The code above is demonstrating the Indian Buffet Process. For the purposes of this post, consider the three images to be random binary matrices with the same number of rows, but variable numbers of columns.
Thank you,
I got a decent result with grid-spec width_ratios.
"""fig, (ax1, ax2, ax3) = plt.subplots(
1,
3,
dpi=100,
sharey=True,
constrained_layout=True
)"""
# I commented the above code and replaced with below.
import matplotlib.gridspec as gridspec
fig = plt.figure(constrained_layout=True)
gs = gridspec.GridSpec(ncols=3, nrows=1, figure=fig, width_ratios=[7./4.,1,6./4.])
ax1 = fig.add_subplot(gs[0,0])
ax2 = fig.add_subplot(gs[0,1])
ax3 = fig.add_subplot(gs[0,2])
It's some what counter intuitive that you need to use width ratios to adjust the heights but in the context of a grid with multiple rows it makes sense that you can only scale columns independently by width. and rows independently by height.
https://matplotlib.org/tutorials/intermediate/gridspec.html

How do I plot a standard histogram data to a polar histogram using Python and pyplot?

I have a list of angles in degrees. I want to display a polar histogram in which the [0°, 360°) range of values is subdivided into equal bins, and display how many values in the angles list fall into each bin. I get histogram data using the following code (and I've checked it is correct):
bins_number = 8 # the [0, 360) interval will be subdivided into this number of equal bins
bins = np.linspace(0.0, 360.0, bins_number + 1)
n, _, _ = plt.hist(angles, bins)
Now, I've tried to plot this data into a polar histogram using the following code:
plt.clf()
width = 2 * np.pi / bins_number
ax = plt.subplot(1, 1, 1, projection='polar')
bars = ax.bar(bins[:bins_number], n, width=width, bottom=0.0)
for bar in bars:
bar.set_alpha(0.5)
plt.show()
but what I get is shown in this image:
As you can see, bars are not placed at the correct angle, and some of them overlap each other, while they should be all contiguous without overlapping.
What am I doing wrong? Thank you in advance.
As in the comment, using radians instead of degrees:
import numpy as np
import matplotlib.pyplot as plt
n_numbers = 100
bins_number = 8 # the [0, 360) interval will be subdivided into this
# number of equal bins
bins = np.linspace(0.0, 2 * np.pi, bins_number + 1)
angles = 2 * np.pi * np.random.rand(n_numbers)
n, _, _ = plt.hist(angles, bins)
plt.clf()
width = 2 * np.pi / bins_number
ax = plt.subplot(1, 1, 1, projection='polar')
bars = ax.bar(bins[:bins_number], n, width=width, bottom=0.0)
for bar in bars:
bar.set_alpha(0.5)
plt.show()
Here were are only plotting centres of bins versus the number of occurrence of the angles in each bin
import numpy as np
import matplotlib.pyplot as plt
degrees = np.random.randint(0, 360, size=200)
radians = np.deg2rad(degrees)
bin_size = 20
a , b=np.histogram(degrees, bins=np.arange(0, 360+bin_size, bin_size))
centers = np.deg2rad(np.ediff1d(b)//2 + b[:-1])
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111, projection='polar')
ax.bar(centers, a, width=np.deg2rad(bin_size), bottom=0.0, color='.8', edgecolor='k')
ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
plt.show()

Pyplot boxplots centered around xticks

I have a series of boxplots that I want to be centered around xticks (2 per xtick specifically). Consider the following:
# fake up some more data
spread= rand(50) * 100
center = ones(25) * 40
flier_high = rand(10) * 100 + 100
flier_low = rand(10) * -100
d2 = concatenate( (spread, center, flier_high, flier_low), 0 )
data.shape = (-1, 1)
d2.shape = (-1, 1)
#data = concatenate( (data, d2), 1 )
# Making a 2-D array only works if all the columns are the
# same length. If they are not, then use a list instead.
# This is actually more efficient because boxplot converts
# a 2-D array into a list of vectors internally anyway.
data = [data, d2, d2[::2,0]]
# multiple box plots on one figure
figure()
boxplot(data)
Which produces
However I would like to have 6 boxplots, with 2 centered around 1, 2 around 2, etc... If I add in three more it simply adds them to 4,5,6... Any help would be appreciated
EDIT To be clear by what I mean by "centered". I would want one boxplot just to the left of the xtick labled "1", and another just to the right. They would likely overlap in the y range so I don't want them to be drawn on top of each other.
To control the x-position of the boxplots, use the positions kwarg.
For example:
import numpy as np
import matplotlib.pyplot as plt
dists = [np.random.normal(i, 1, 100) for i in range(0, 10, 2)]
fig, ax = plt.subplots()
ax.boxplot(dists, positions=[0, 1, 2, 0, 1])
plt.show()
If you'd prefer to have the groups side-by-side, you'll need to calculate the positions yourself. One approach might be something like this:
def grouped_boxplots(data_groups, ax=None, max_width=0.8, pad=0.05, **kwargs):
if ax is None:
ax = plt.gca()
max_group_size = max(len(item) for item in data_groups)
total_padding = pad * (max_group_size - 1)
width = (max_width - total_padding) / max_group_size
kwargs['widths'] = width
def positions(group, i):
span = width * len(group) + pad * (len(group) - 1)
ends = (span - width) / 2
x = np.linspace(-ends, ends, len(group))
return x + i
artists = []
for i, group in enumerate(data_groups, start=1):
artist = ax.boxplot(group, positions=positions(group, i), **kwargs)
artists.append(artist)
ax.margins(0.05)
ax.set(xticks=np.arange(len(data_groups)) + 1)
ax.autoscale()
return artists
And as a quick example of using it:
data = [[np.random.normal(i, 1, 30) for i in range(2)],
[np.random.normal(i, 1.5, 30) for i in range(3)],
[np.random.normal(i, 2, 30) for i in range(4)]]
grouped_boxplots(data)
plt.show()
...And just for the sake of showing an excessively fancy example:
import numpy as np
import matplotlib.pyplot as plt
def main():
data = [[np.random.normal(i, 1, 30) for i in range(2)],
[np.random.normal(i, 1.5, 30) for i in range(3)],
[np.random.normal(i, 2, 30) for i in range(4)]]
fig, ax = plt.subplots()
groups = grouped_boxplots(data, ax, max_width=0.9,
patch_artist=True, notch=True)
colors = ['lavender', 'lightblue', 'bisque', 'lightgreen']
for item in groups:
for color, patch in zip(colors, item['boxes']):
patch.set(facecolor=color)
proxy_artists = groups[-1]['boxes']
ax.legend(proxy_artists, ['Group A', 'Group B', 'Group C', 'Group D'],
loc='best')
ax.set(xlabel='Year', ylabel='Performance', axisbelow=True,
xticklabels=['2012', '2013', '2014'])
ax.grid(axis='y', ls='-', color='white', lw=2)
ax.patch.set(facecolor='0.95')
plt.show()
def grouped_boxplots(data_groups, ax=None, max_width=0.8, pad=0.05, **kwargs):
if ax is None:
ax = plt.gca()
max_group_size = max(len(item) for item in data_groups)
total_padding = pad * (max_group_size - 1)
width = (max_width - total_padding) / max_group_size
kwargs['widths'] = width
def positions(group, i):
span = width * len(group) + pad * (len(group) - 1)
ends = (span - width) / 2
x = np.linspace(-ends, ends, len(group))
return x + i
artists = []
for i, group in enumerate(data_groups, start=1):
artist = ax.boxplot(group, positions=positions(group, i), **kwargs)
artists.append(artist)
ax.margins(0.05)
ax.set(xticks=np.arange(len(data_groups)) + 1)
ax.autoscale()
return artists
main()

Categories

Resources