Seaborn saving plots with gray background when it should be white - python

When I save a figure to .png, it saves with an unexpected gray background instead of white.
import matplotlib.pyplot as plt
import seaborn as sns
import random
sns.set_theme()
sns.set_style("whitegrid")
sns.set_context("poster", font_scale=1.8)
xvals = list(range(15,41))
yvals = random.sample(range(0, 50), len(xvals))
sns.set_theme()
sns.set_style("whitegrid")
sns.set_context("poster", font_scale=1.8)
fig = plt.figure()
ax = sns.lineplot(x=xvals, y=yvals)
plt.suptitle('Sample Plot')
ax.set_xlabel('x unit')
ax.set_ylabel('y unit')
ax.autoscale(enable=True, axis='y', tight=True)
fig.savefig('Results/Sample Plot.png', dpi=300, bbox_inches='tight', pad_inches=0)
The resulting figure saved as png:
What Spyder shows it looking like, and what I'm expecting to save:

You can use facecolor in figure to specify the background color, like fig = plt.figure(figsize=(10, 8), dpi=300, facecolor='#FFF') when you save the plot you can use it again like that fig.savefig(....,facecolor=fig.get_facecolor())
Your code fixed
import matplotlib.pyplot as plt
import seaborn as sns
import random
sns.set_theme()
sns.set_style("whitegrid")
sns.set_context("poster", font_scale=1.8)
xvals = list(range(15,41))
yvals = random.sample(range(0, 50), len(xvals))
sns.set_theme()
sns.set_style("whitegrid")
sns.set_context("poster", font_scale=1.8)
fig = plt.figure(facecolor='#FFF')
ax = sns.lineplot(x=xvals, y=yvals)
plt.suptitle('Sample Plot')
ax.set_xlabel('x unit')
ax.set_ylabel('y unit')
ax.autoscale(enable=True, axis='y', tight=True)
fig.savefig('Sample Plot.png', dpi=300, bbox_inches='tight', pad_inches=0,facecolor=fig.get_facecolor())

Related

Values of pie chart overlaps

I want to see the values clearly, how can I fix that? How can I locate my values manually?
(values of gray and black colors (0.4) overlaps)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
df=pd.DataFrame({'Fiziki Durum':[173,76,1,1]},index=['Bina İçerisinde','Müstakil','Gezici','Prefabrik'])
labels='Bina İçerisinde','Müstakil','Gezici','Prefabrik'
colors=('#3a88e2','#5c9e1e','#708090','#0e1111')
fig1, ax=plt.subplots()
sizes=[173,76,1,1]
explode = (0.05,0.05,0.05,0.05)
patches, texts, autotexts = ax.pie(sizes, colors=colors, startangle=90,explode=explode, autopct='%1.1f%%' )
plt.legend(patches, labels, loc='upper center', bbox_to_anchor=(0.5, 0.08), ncol=4, frameon=False)
#draw circle
centre_circle = plt.Circle((0,0),0.70,fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
ax.axis('equal')
plt.tight_layout()
plt.show()

df.plot.scatter how can I change the size of the second yaxis?

here is my python code:
fig, ax1 = plt.subplots(figsize=(15,10))
ax1.tick_params(axis='both', labelsize=18)
test.plot.scatter(x='USFM Durchfluß korrigiert', y='Ausgangsdruck', c='Dichte', ax = ax1, colormap="viridis")
ax1.set_xlabel('Durchfluss [m^3/h]', fontsize=18)
ax1.set_ylabel('Ausgangsdruck [bar]', fontsize=18)
I want to have "Dichte" in the same size as the other axis:
Try to set the general font size of matplotlib.pyplot and the font size of the ticks equal with this:
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.rc('font', size=10)
mpl.rc('xtick', labelsize=10)
mpl.rc('ytick', labelsize=10)

matplotlib pcolormesh grid not visible

Does anyone know why my grid is not plotted on top of the colormap here.
import matplotlib.pylab as plt
import numpy as np
Style = 'ggplot'
plt.style.use([Style])
data = np.random.random((40,40))
fig = plt.figure()
ax = fig.add_subplot(111)
ax.pcolormesh(data, cmap=plt.cm.viridis, zorder=1)
ax.grid(True, zorder=10)
You can use plt.rcParams["axes.axisbelow"] = False to force the grid to be on top. Note that this problem only occurs because of the use of the "ggplot" style.
Example with ggplot style:
import matplotlib.pylab as plt
import numpy as np
plt.style.use('ggplot')
plt.rcParams["axes.axisbelow"] = False
data = np.random.random((40,40))
fig = plt.figure()
ax = fig.add_subplot(111)
ax.pcolormesh(data, cmap=plt.cm.viridis, zorder=1)
ax.grid(True, color="crimson", lw=2)
plt.show()
Example using default style:
import matplotlib.pylab as plt
import numpy as np
data = np.random.random((40,40))
fig = plt.figure()
ax = fig.add_subplot(111)
ax.pcolormesh(data, cmap=plt.cm.viridis, zorder=1)
ax.grid(True, color="crimson", lw=2)
plt.show()

Wedges with label parameter, but none label in result

I am beginner with python (3.4) and matplotlib. I want to create a wedge with the following code:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.add_patch(patches.Wedge(center=(0,0), r=0.9, theta1=90, theta2=120, facecolor="red", label="Test"))
plt.xlim(-1, 1)
plt.ylim(-1, 1)
fig1.savefig('wedge1.png', dpi=90, bbox_inches='tight')
plt.show()
All Looks fine, but the Label isn't in the plot? Any idea?
You are missing a plt.legend(). You just need to add it anywhere before the plt.show (also before fig1.savefig if you want it saved in the image) and after all your plots:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.add_patch(patches.Wedge(center=(0,0), r=0.9, theta1=90, theta2=120, facecolor="red", label="Test"))
plt.xlim(-1, 1)
plt.ylim(-1, 1)
plt.legend() # <--- here
fig1.savefig('wedge1.png', dpi=90, bbox_inches='tight')
plt.show()
Have a look here for further details on how to use legends.

how can I increase the space between tick labels and axes labels in matplotlib for a 3D plot?

how can I increase the space between tick labels and axes labels in matplotlib for a 3D plot?
my plot is defined in this way
import pylab
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(np.log10(NP), np.log10(NB*10**12), np.log10(NL), c='r', marker='o', lw=0)
ax.scatter(np.log10(NPd), np.log10(NBd), np.log10(NLd), c='b', marker='o', lw=0)
ax.set_xlabel('Periods', fontsize=35)
ax.set_ylabel('Magnetic', fontsize=35)
ax.set_zlabel('Luminosity', fontsize=35)
atockx = ax.xaxis.get_major_ticks()
for tockx in atockx:
tockx.label.set_fontsize(30)
atocky = ax.yaxis.get_major_ticks()
for tocky in atocky:
tocky.label.set_fontsize(30)
atockz = ax.zaxis.get_major_ticks()
for tockz in atockz:
tockz.label.set_fontsize(30)

Categories

Resources