Summing lines in pyplot - python

I have a pyplot figure with a few lines on it. I would like to be able to draw an extra line, which would be a sum of all others' values. The lines are not plotted against the same x values (they are visually shorter in the plot - see the image). The resulting line would be somewhat above all others.
One idea I have for it requires obtaining a line's y value in a specific x point. Is there such a function? Or does pyplot/matplotlib support summing lines' values?

Superposition it the short answer to your question: read this for more.
Example:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(100) #range of x axis
y1 = np.random.rand(100,1) #some random numbers
y2 = np.random.rand(100,1)
#this will only plot y1 value
plt.plot(x,y1)
plt.show()
#this will plot summation of two elements
plt.plot(x,y1+y2)
plt.show()
I took a second look at your question, what I saw is your y values have different length so adding them would not be the case as shown in example above. What you can do is create equal sized 4 lists, where non existing values in that list is zero, then you can apply super position to this (simply add all of them and then plot)

For the future generations: numpy.interp() was my solution to this problem.

Related

How to dynamically plot multiple subplots in Python?

I need to plot a variable number of plots (at least 1 but it isn't known the number max) and I couldn't come up with a way to dynamically create and assign subplots to the given graphs.
The code looks like this:
check = False
if "node_x_9" in names:
if "node_x_11" in names:
plt.plot(df["node_x_9"], df["node_x_11"])
check = True
elif "node_x_10" in names:
if "node_x_12" in names:
plt.plot(df["node_x_10", "node_x_12"])
check = True
if check:
plt.show()
I thought about presetting a number of subplots (e.g. plt.subplots(3, 3)) but I still could not come up with a way to assign the plots without bounding them to a given subplot position.
My idea would be to create a 2x1 plot if I have two subplots, 1x1 if I have one, 3x1 if I have 3 and so on and not letting any subplot space empty.
I've come across cases like this, you want to generate one plot per case, but don't know how many cases exist until you query the data on the day.
I used a square layout as an assumption (alter the below if you require a different aspect ratio) then count how many cases you have - find the integer square-root, which, plus one, will give you the integer side-length of a square that is guaranteed to fit your requirements.
Now, you can establish a matplotlib Gridspec object with the requisite width and height, referencing it by index to place your individual plots.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import gridspec
import random
# Create some random data with size=`random` number between 5 and 100
size = random.randint(5,100)
data_rows = pd.DataFrame([np.random.normal(1,5,25) for s in range(0,size)])
# Find the length of a (near) square based on the number of the data samples
side_length = int(len(data_rows)**(1/2))+1
print(side_length)
#Create a gridspec object based on the side_length in both x and y dimensions
gs=gridspec.GridSpec(side_length, side_length)
fig = plt.figure(figsize=(10,10))
# Using the index i, populate the gridpsec object with
# one plot per cell.
for i,row in data_rows.iterrows():
ax=fig.add_subplot(gs[i])
plt.bar(x=range(0,25),height=row)

Scatter matrix with few variables with target in Y axis

I am trying to plot variable Vs SalePrice data. I tried pd.scatter_matrix but I am getting number of unnecessary plot with various combinations. I look for is SalePrice in Y axis and a scatter plot for each element from the data set. Here is the code I tried.
data_prep_num['Sales_test_data']=data_sales_price_old
att=['Sales_test_data','YearBuilt','LotArea','MSSubClass','BsmtFinSF1','TotalBsmtSF','1stFlrSF','2ndFlrSF','GrLivArea','GarageArea']
pd.scatter_matrix(data_prep_num[att],alpha=.4,figsize=(30,30))```
If you want to use pd.plotting.scatter_matrix but only want one of the rows (i.e. the Sales_test_data column), you can iterate over the plotting axes, and hide the combinations you don't want.
Assuming the SalePrice is the very first column (index 0):
import numpy as np
import matplotlib.pyplot as plt
axes = pd.plotting.scatter_matrix(data_prep_num[att], alpha=0.4, figsize=(30,30))
for i in range(np.shape(axes)[0]):
if i != 0:
for j in range(np.shape(axes)[1]):
axes[i,j].set_visible(False)
Note: This is obviously not super efficient when you start having lots of columns though.

Plotting histogram error - want to swap x and y axis

I have an array with values [100,101,102,103,104,105]. When I am plotting it on histogram the values 100,101... are coming on x axis but i want them on y...any suggestion how to do it?
First of all are you sure you want the histogram. By definition the histogram will have values vs count of occurrences. If indeed want the histogram, but with rotated axis it can be done using 'orientation' keyword.
import numpy as np
import matplotlib.pyplot as plt
vals = np.random.randint(100,110,(100))
print(vals)
plt.hist(vals, orientation='horizontal')
plt.show()
Following further clarifications looks like you want horizontal bar plot. Using your example I modified it just to use barh() instead of bar() and also added another source of data, otherwise the result is too trivial. For bar plot you need two columns - one is the position of the bar, another is the length of the bar.
array = [101,102,103,104,105]
values = np.random.randint(0,10, size=len(array))
print(values)
plt.barh(array,values)
plt.show()
Output:
[3 9 2 4 7]

Plotting a set of functions using a 'violin-plot' style plot in python

Let's say I want to visualize the functions f[n] = e^{-(x-n)^2}/n for n=1...10. Notice that these are not probability distributions.
(not actually the plot I want to do, but close enough).
I'd like to demonstrate it with something like a violin-plot (https://matplotlib.org/gallery/statistics/violinplot.html) where for each n I have a vertical line and I plot the function on both sides of the vertical line.
But violin plots seem to only be used for showing the locations of a sample of data. So all the tools for it require me to give it a data set. The data I want to plot isn't of that type - it's an actual known function.
[if you want more context this is related to an earlier question of mine - https://stats.stackexchange.com/questions/403359/visualizing-2d-data-when-one-dimension-is-discrete-and-the-other-continuous].
The question is a bit broad, so maybe this is not actually what you're looking for. But as I understand it, you just want to plot your function at position f(x,n) at different positions n and have x on the vertical axis.
import numpy as np
import matplotlib.pyplot as plt
f = lambda x, n: np.exp(-(x-n)**2)/n
x = np.linspace(-2,12,101)
ns = np.arange(1,11)
for n in ns:
plt.fill_betweenx(x, -f(x,n)+n, f(x,n)+n, color="C0", alpha=0.5)
plt.xlabel("n")
plt.ylabel("x")
plt.xticks(ns)
plt.show()
IIUC, you want something like this:
df = pd.DataFrame({n: [np.exp(-(x-n)**2)/n for x in np.arange(-1,1,0.1)] for n in range(1,11)})
fig, ax = plt.subplots(1,1, figsize=(10,10))
ax.violinplot(df.T)
plt.show()
Output:

Color every point in a scatterplot differently in numpy/python [duplicate]

This question already has an answer here:
matplotlib scatter plot colour as function of third variable [duplicate]
(1 answer)
Closed 4 years ago.
I am solving an optimization problem. There are n iterations and at each one I have a point x_n=[x_n1, x_n2]. I want to plot the iterates x_n so that as n increases the color of the points gets dark or lighter or whatever. Currently I can plot the iterates but they are all the same color so i cannot tell which points correspond to higher values of n.
The variable x_test is an array which contains all the iterates from x_0 to x_n. Here is my code to plot it:
pl.scatter(x_test[:,0], x_test[:,1])
pl.show()
I have found a lot of info on how to color graphs but not in the way I am desiring, where each point corresponds to a different light/darkness.
Here is a minimal, complete, verifiable example:
import numpy as np
import pylab as pl
x = np.array([[1,1],[1,2],[1,3],[2,4]])
pl.plot(x[:,0], x[:,1])
This gives the scatter plot of the points in the array x but I need them to each be a different color corresponding to the position in x, i.e. [1,1] should be the lightest, then [1,2] will be slightly dark, etc until [2,4] is the darkest.
edit: here is the solution I went with:
scaled_itera = np.array(range(x_test.shape[0]))/(float(x_test.shape[0])-1)
colors = pl.cm.coolwarm(scaled_itera)
pl.scatter(x_test[:,0], x_test[:,1], color=colors)
pl.show()
You can directly give a list of color to plt.scatter(). For example: you can do:
import seaborn as sns
color_list = sns.color_palette("Paired", n_colors=x_test.shape[0])
plt.scatter(x_test[:,0], x_test[:,1], color=color_list)
Check colormap_reference if you want to stay with matplotlib for colors.
An example using plt.cm:
plt.scatter(x_test[:,0], x_test[:,1], color=plt.cm.Paired.colors[:x_test.shape[0]])
you need to loop through each point
assuming your data are something like
x = [[1,2],[3,4],[5,6],[7,8]]
then, something like
c_aray = ['black','green','red','blue']
for i in range(len(x)):
xn = x[i]
plt.plot(xn[0],xn[1],color = c_aray[i],'o')

Categories