Python Forum
1D plot frome 3D data - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: 1D plot frome 3D data (/thread-40343.html)



1D plot frome 3D data - layla - Jul-14-2023

Hi all, I use python and I want to draw a 1D curve from 3D data i.e. I have data in the form of x, y, z and in the fourth column a variable D Also, I want that before drawing the curve to have the data of the curve in a .txt file? Please, can someone help me, I need a method that will allow me to do this task? Thanks in advance


RE: 1D plot frome 3D data - buran - Jul-14-2023

what is 1D curve? Probably you mean 2D


RE: 1D plot frome 3D data - layla - Jul-14-2023

Yes, sorry, I mean 2D plot


RE: 1D plot frome 3D data - Pedroski55 - Jul-16-2023

So what data are you starting with?

Where are you getting the data to plot the 3D curve?

How are y and z related to x?


RE: 1D plot frome 3D data - layla - Jul-16-2023

Hello,
Here is the data I want plotted also I want a 2D plot and not 3d I for example x as function of D.[attachment=2458]


RE: 1D plot frome 3D data - Pedroski55 - Jul-16-2023

Well X an Y are all zero, so I can only think you want to plot z against D!

Probably, you can get the numpy array coords below directly using numpy, but I never use numpy, so I don't know how to do that. Also I hardly ever use mathplotlib, so I am no expert.

Try out the following in your Python shell, step for step:

import csv
import glob
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.ticker import (MultipleLocator, AutoMinorLocator)

path = '/home/pedro/myPython/csv/csv/'
csvs = glob.glob(path + '*.csv')
for file in csvs:
    print('The csv files are', file)
myfile = input('Copy and paste the file you want here ... ')
# csv_reader is gone after you use it one time
# it is convenient to copy the data to a list
with open(myfile) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    data = []
    for row in csv_reader:
        data.append(row)
Zs = []
Ds = []
for d in range(1, len(data)):
    print(data[d][2], data[d][3])
    Zs.append(int(data[d][2]))
    Ds.append(float(data[d][3]))
# show the data in the lists           
for i in range(len(Zs)):
    print(Zs[i], Ds[i])

minimumZ = min(Zs)
maximumZ = max(Zs)
minimumD = min(Ds)
maximumD = max(Ds)
print('The minimum and maximum Z are', minimumZ, maximumZ)
print('The minimum and maximum D are', minimumD, maximumD)

# no difference if you use () or [] so only use 1 of these
coords = np.array([Zs, Ds])
coords = np.array((Zs, Ds))
for c in coords:
    print(c)

def tp1():
    plt.rcParams["figure.figsize"] = [10, 10]
    maximumD = max(Ds)
    x, y = coords
    fig, ax = plt.subplots(num="Showing y = D ")  # Create a figure and an axes.
    ax.plot(x, y, label='y = D')  # Plot some data on the axes.    
    ax.set_xlabel('x values')  # Add an x-label to the axes.
    ax.set_ylabel('y values')  # Add a y-label to the axes.
    ax.set_title("As z gets bigger, D gets bigger")  # Add a title to the axes.
    plt.axhline(y = maximumD, color = 'r', linestyle = 'dashed', label='y = D')
    ax.legend()  # Add a legend.
    fig.text(.4, .25, "The ups and downs", va="center", ha="center",
             bbox=dict(boxstyle="round, pad=1", facecolor="w"))
    plt.show()

# now just run the function tp1()
tp1()
mathplotlib can do many things as decoration. Look up the docs!


RE: 1D plot frome 3D data - layla - Jul-16-2023

Thank you very much!! , but I see that the value of Z repeats and I want to gather the values of D for each value of Z So I can have the curve that I want


RE: 1D plot frome 3D data - Pedroski55 - Jul-16-2023

I missed out the lines above which select the csv file and open it. Have another look now, lines 9, 10, 11.

No, there are no repeated Z values:

Quote:len(Zs)
80
setZ = set(Zs)
len(setZ)
80

The length of the set(Zs) is the same as the length of Zs. If there were repeated Z values, len(setZ) would be smaller.