Line Alpha Not Saving Properly in Matplotlib - python

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.

Related

Matplotlib version of latex command "\ell" looking extra-slanted

I use latex in matplotlib by setting
plt.rcParams.update({'mathtext.fontset': 'stix'})
plt.rcParams.update({'font.family': 'STIXGeneral'})
I am using the letter $\ell$ very often in my research and there is a small detail bothering me. As you can see below, matplotlib renders the symbol with the little loop smaller and the letter more slanted. To me it almost looks like a vertically stretched $e$. I tried using the "\mathrm{\ell}" command instead but it did not change anything.
Is there any way I could get the symbol to look normal?
PS: it looks like stackoverflow is not detecting the math mode $ for some reason. If you know how to fix it (or if I am doing something wrong) please point it out or edit the question. Thanks!
The reason is the font you are using in matplotlib. With the following settings, for example, you get the same letter as in overleaf:
import matplotlib.pyplot as plt
import numpy as np
# Example data
t = np.arange(0.0, 10, 1)
s = np.arange(0.0, 10, 1)
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.xlabel(r'$\ell$', fontsize=30)
plt.ylabel(r'$\ell$', fontsize=30)
plt.plot(t, s)
plt.show()
You get:
However, In Jupyterlab I could not reproduce. It used the overleaf fonts even with your settings.
This proved to be the simplest solution for me. Thanks the others for pointing out the font being the issue.
Rather than
plt.rcParams.update({'mathtext.fontset': 'stix'})
plt.rcParams.update({'font.family': 'STIXGeneral'})
I now write the first line as
plt.rcParams.update({'mathtext.fontset': 'cm'})
which works like charm. This is helpful if you are someone like me not using TeX but just the mathtext matplotlib built-in function.

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.

plt.figure() doesn't show any figure in this example code

I'm new to python and programing and I'm trying to make a code to display an image with some data from a .fits file. I'm first trying to make this example I found from this site: https://docs.astropy.org/en/stable/generated/examples/io/plot_fits-image.html#sphx-glr-download-generated-examples-io-plot-fits-image-py. When I run it, it shows everything it should, except the figure, which is the most important part. How do I make the figure show up?
The code is the following:
import matplotlib.pyplot as plt
from astropy.visualization import astropy_mpl_style
plt.style.use(astropy_mpl_style)
from astropy.utils.data import get_pkg_data_filename
from astropy.io import fits
image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')
fits.info(image_file)
image_data = fits.getdata(image_file, ext=0)
print(image_data.shape)
plt.figure()
plt.imshow(image_data, cmap='gray')
plt.colorbar()
Appending plt.show() at the end of your code should work ...
I ignored the fact that the figure was not showing up in the example and went straight to my .fits file. With that file the figure worked fine. Turns out there was probably something wrong with the example file.

eps export issue with Python and matplotlib

I am having problems while exporting eps files from matplot lib. I want to edit in Corel Draw an eps file that was exported from matplotlib using, for example:
plt.savefig('test01.eps', format='eps', dpi=600)
After opening the file in corel, I get the following image
as it can be seen, there is a problem with letters. They are imported with wrong size and positioning; also they are converted to curves (although I have explicitly said to Corel to import the as text).
Importing the eps file with Microsoft Words gives the same results. It seems to be a matplotlib problem.
I have tried changing to Qt4Agg using
mpl.use('Qt4Agg')
font = {'family' : 'Times New Roman','weight' : 'normal','size': 12}
mpl.rc('font', **font)
but it doesn't work...
Anyone having the same issue?
Add these two lines:
matplotlib.rcParams['text.usetex'] = True
matplotlib.rcParams['text.latex.unicode'] = True

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

Categories

Resources