How do I make a simple plot using Matplotlib? - python

I am trying to plot a text files with X,Y values(attached inline). I am first converting the text file into 2 array objects XAxis1 and YAxis1. The plot doesn't come out properly. Unable to set the X and Y axis values.
Any help is much appreciated.
(Sample1.txt)XAxis1 : 0.51,0.52,0.53,0.54,0.55,0.56,0.57,0.58,0.59,0.6,0.61,0.62,0.63,0.64,0.65,0.66,0.67,0.68,0.69,0.7
(Sample2.txt)YAxis1:
29.63,30.03,30.94,31.67,33.59,35.09,35.35,35.04,36.71,36.77,36.84,37.45,33.87,31.68,30.98,27.97,29.24,38.52,33.37,27.8
Sorry could not paste the code, some errors..

Some people have downvoted your question, because a google search can solve much bigger problems and this is not very exotic.
See https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
from matplotlib.pyplot import plot
ax_x = [
0.51,0.52,0.53,0.54,0.55,0.56,0.57,0.58,0.59,0.6,0.61,0.62,0.63,0.64,0.65,0.66,0.67,0.68,0.69,0.7]
ax_y = [
29.63,30.03,30.94,31.67,33.59,35.09,35.35,35.04,36.71,36.77,36.84,37.45,33.87,31.68,30.98,27.97,29.24,38.52,33.37,27.8]
plot(ax_x, ax_y)
In case reading from file is a problem, I recommend saving everything in a useful format.
See https://www.programiz.com/python-programming/json

Related

Why are there Horizontal Stripes on my Palettized Image?

I am trying to make a palettized version of my height image data (using Python/Matplotlib) and for some reason...it is giving me quite weird horizontal lines which I know are not actually present in the dataset.
Both images (mine and the "better" one).
Is this something weird with how Matplotlib normalizes the data? I just don't quite understand how this could happen, so I am at a loss for where to start. I have provided my code below (sorry if there is a typo, I slightly changed it to make sense outside of the code).
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# file location of the raw data
fileloc = r'C:\Users\...\raw_height_profile.csv'
# generate height profile map
palettized_image = getheightprofile(fileloc)
def getheightprofile(fileloc, color_palette='jet'):
# read data from file
data = pd.read_csv(fileloc, skiprows=0)
# generate colormap (I'm using the jet colormap rn)
colormap = plt.get_cmap(color_palette)
# normalize the height data to the range [0, 1]
norm = (data - np.min(data)) / (np.max(data) - np.min(data))
# convert the height data to RGB values using the palette
palettized_data = (colormap(norm)*255).astype(np.uint8)
# save the file as a png (to check quality)
saveloc = r'C:\Users\...\palletized_height_profile.png'
plt.imsave(saveloc, palettized_data)
# return the nice numbers for later analysis
return palettized_data
But instead of returning the nice image that I think I should get, it returns a super weird image with lines across it. note: I know these images aren't quite the same palettization, but I think you can understand the issue.
Does anyone understand how, why, etc.? I have also attached a link to the dataset, because maybe that is helpful...but I am quite sure there is nothing wrong with the data.

Displaying Matplotlib Line Graph in Jupyter

I'm working on taking some data from a dataset and plotting certain aspects of it. Here's my code below:
import matplotlib.pyplot as plt
df1 = pd.read_csv('dataset_1.csv')
soil_moisture = list(df1.Soil_Moisture)
soil_temperature = list(df1.Soil_Temp)
print(len(soil_moisture))
print(len(soil_temperature))
plt.plot([soil_moisture], [soil_temperature])
plt.show()
As you can see, it takes data from each of those columns and tries to make a line graph. However, when I run, it just displays an empty graph. This is weird since when I print the soil_moisture and soil_temperature, it tells me that there's actual data, and none of my other plots in the same notebook are experiencing this. All help is appreciated!
Here's an image of the jupyter output
Please revise line 7 of your code as:
plt.plot(soil_moisture, soil_temperature)
When you use [soil_moisture] that means you are generating another list with list soil_moisture as its first element.

editing plot - python

Hi I have a code that plots 2D data from a .dat file (I'll call it filename.dat which is just a .dat file with 2 columns of numbers). It works fine, I just have some questions as to how to improve it.
How can I edit my code to make the axes label larger and add a title? This code is not so easy to edit the way I have it written now. I have tried adding the fontsize,title into the plotfile(...) command, but this did not work. Thanks for the help! My code is below.
import numpy as np
import matplotlib.pyplot as plt
#unpack file data
dat_file = np.loadtxt("filename.dat",unpack=True)
plt.plotfile('filename.dat', delimiter=' ',cols=(0,1), names=('x','y'),marker='0')
plt.show()
I assume you want to add them to the plot.
You can add a title with:
plt.title("MyTitle")
and you add text to the graph with
# the following coordinates need to be determined with experimentation
# put them at a location and move them around if they are in
# the way of the graph
x = 5
y = 10
plt.text(x,y, "Your comment")
This can help you with the font sizes:
How to change the font size on a matplotlib plot

Line Alpha Not Saving Properly in Matplotlib

I'm having a baffling issue with saving figures with a specified line alpha in matplotlib. I would really appreciate any input. I found this previous thread but changing the rasterization does not seem to fix the issue.
Here's the minimal example I have of where this issue shows up:
import numpy as np
import matplotlib.pyplot as plt
arr = np.random.randn(10000, 2)
plt.plot(arr[:,0], arr[:,1],alpha=.09, color='black')
plt.savefig('dots_vector.pdf')
plt.show()
In the the display window, it looks like the following (what I want):
However, when I save it as a PDF or PNG, it looks like the following:
Note, I've tried the following setting:
ax.set_rasterized(True)
As that's what the previous thread suggested, but this does not fix the problem.
Bizarrely, when I change the marker type of the line, to the following:
plt.plot(arr[:,0], arr[:,1], 'o', alpha=.09, color='black')
The alpha seems to behave exactly as expected either as a PDF or a PNG, seen here:
Any ideas would be greatly appreciated on how to get the alpha to save properly for the line plot, as I'm pretty stumped.

How can I write a velocity field to a VTI image with anaconda Python?

I am trying to write a VTK Image Data file (.vti) with python. For my python coding I am using the Anaconda distribution. I am using the evtk package, which has the ability to write a vtk file.
The data I need to write is a velocity for which I have the 3d X,Y,Z and U,V,W 3d arrays. I have found some sample code which uses the evtk package to write a .vti file.(http://www.vtk.org/Wiki/VTK/Writing_VTK_files_using_python)
The problem is that the sample code and built in functions only take scalar point or cell data. So I am able to write a file with scalars, but I need it to have the data as vectors.
I am digging through the actual package files and trying to find a solution or tools to code one.I would extremely appreciate if somebody had suggestions or solutions to give me a hand.
I enclose the test code I have written from info on the wiki just in case I am missing a way of inputing to the function, but I fear I am going to need to start from scratch.
Thanks in advance
(removed the code since the one bellow is more recent)
Managed to write an unstructured file (.vtu), but I would really like to be a able to write an Image Data file.(Found the following link helpful during the process. http://www.aero.iitb.ac.in/~prabhu/tmp/python_cep07/course_handouts/viz3d_handout.pdf)
Thanks again in advance
I attach the code to see if anybody has any suggestions.
from tvtk.api import tvtk, write_data
import numpy as N
##Generation of data
#array of x,y,z coordinates
[Z,Y,X] = N.mgrid[-2.:2+1, -2.:2+1, -2.:2+1]
#array of zeros to add the u,v,w components
[W,V,U] = N.zeros_like([Z,Y,X],dtype=float)
#loop through data to have correct format
points = N.array([N.zeros(3) for i in range(len(Z)*len(Z[0])*len(Z[0][0]))])
velF = N.zeros_like(points)
c=0
for k in range(len(Z)):
for j in range(len(Z[0])):
for i in range(len(Z[0][0])):
#coordinates of point
x = X[k][j][i]
y = Y[k][j][i]
z = Z[k][j][i]
points[c] = N.array([x,y,z])
#test velocity field
u = k -2.
v = 0.
w = 0.
velF[c] = N.array([u,v,w])
#update counter
c = c+1
##Generate and write the vtk file
Ugrid = tvtk.UnstructuredGrid()
Ugrid.points = points
Ugrid.point_data.vectors = velF
Ugrid.point_data.vectors.name = 'velocity'
write_data(Ugrid, 'vtktest.vtu')
If you want to write the unstructured grids using evtk, here you can find a full demo with point and cell data (both vector and scalar fields): https://gist.github.com/dromanov/0fb8bacff5342a56a690.
Description of the technique and explanations are here: http://spikard.blogspot.ru/2015/07/visualization-of-unstructured-grid-data.html.
Good luck!

Categories

Resources