Given the following data frame:
import pandas as pd
df=pd.DataFrame({'A':['$0-$20','$20+']})
df
A
0 0−20
1 $20+
I'd like to create a bar chart in MatPlotLib but I can't seem to get the dollar signs to show up correctly.
Here's what I have:
import matplotlib.pyplot as plt
import numpy as np
y=df.B
x=df.A
ind=np.arange(len(x))
fig, ax = plt.subplots(1, 1, figsize = (2,2))
plt.bar(ind, y, align='center', width=.5, edgecolor='none', color='grey')
ax.patch.set_facecolor('none')
ax.patch.set_alpha(0)
ax.set_ylim([0,5])
ax.set_xlabel(x,fontsize=12,rotation=0,color='grey')
ax.set_xticklabels('')
ax.set_yticklabels('')
I can get the labels to display "better" if I use df.A.values.tolist(), but that just corrects the format.
I'd like each label to display under each bar with the intended original format (with dollar signs).
Thanks in advance!
To specify the xticklabels, pass tick_label=x to plt.bar.
Matplotlib parses labels using a subset of the TeX markup
language. Dollar
signs indicate the beginning (and end) of math mode. So pairs of bare dollar signs are
getting unintentionally swallowed. Currently, there is no a way to disable mathtex parsing. So to prevent the dollar signs from being interpreted as math markup, replace the
bare $ with \$:
df['A'] = df['A'].str.replace('$', '\$')
For example,
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({'A': ['$0-$20', '$20+'], 'B': [10,20]})
df['A'] = df['A'].str.replace('$', '\$')
y = df['B']
x = df['A']
ind = np.arange(len(x))
fig, ax = plt.subplots(1, 1, figsize=(2, 2))
plt.bar(ind, y,
tick_label=x,
align='center', width=.5, edgecolor='none',
color='grey')
plt.show()
Alternatively, you could use df.plot(kind='bar'):
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({'A': ['$0-$20', '$20+'], 'B': [10,20]})
df['A'] = df['A'].str.replace('$', '\$')
fig, ax = plt.subplots(1, 1, figsize=(2, 2))
df.plot(kind='bar', x='A', y='B',
align='center', width=.5, edgecolor='none',
color='grey', ax=ax)
plt.xticks(rotation=25)
plt.show()
Related
I have a code in python 3.11 for a contour plot generating from an excel table using matplotlib. The result shows only first days of months on the x axis (for example 1.6.2022, 1.7.2022 ...). I want all days from the excel source table. Her's the code:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import Normalize
import pandas as pd
import matplotlib.dates as mdates
# import data from excel file
df = pd.read_excel('temperature_data.xlsx', index_col=0)
# Assign columns to variables
time = df.columns
depth = df.index
temperature = df.to_numpy()
# Creating the graph
fig, ax = plt.subplots()
min_temp = temperature.min()
max_temp = temperature.max()
cs = plt.contourf(time, depth, temperature, levels=np.arange(round(min_temp), round(max_temp)+2, 2), cmap='coolwarm', vmin=min_temp, vmax=max_temp)
cs2 = plt.contour(time, depth, temperature, levels=np.arange(round(min_temp), round(max_temp)+2, 2), colors='black')
plt.gca().invert_yaxis()
plt.clabel(cs2, inline=1, fontsize=10, fmt='%d')
plt.title('Teplota vody [°C]')
plt.xticks(rotation=90, ha='right')
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%Y'))
#ax.set_xlim(df.index.min(), df.index.max())
#ax.set_xlabel('Time')
ax.set_ylabel('hloubka [m]')
norm = Normalize(vmin=min_temp, vmax=max_temp)
plt.colorbar(cs, cmap='coolwarm', norm=norm)
plt.show()
Thank you for your help.
I want to divide and color points,val_lab(611,3) by their labels,pred_LAB(611,)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = plt.axes(projection = '3d')
ax.set_xlabel('L')
ax.set_ylabel('A')
ax.set_zlabel('B')
for i in range(0, len(val_lab)):
ax.scatter3D(
val_lab[i,0],
val_lab[i,1],
val_lab[i,2],
s = 8,
marker='o',
c = pred_LAB
#cmap = 'rainbow'
)
#ax.legend(*points.legend_elements(), title = 'clusters')
plt.show()
The problem is it shows error,
c' argument has 611 elements, which is not acceptable for use with 'x'
with size 1, 'y' with size 1.
However, if the dataset only have ten points,it can show the figure correctly, I don't know how to solve this problem, besides, how to add legend of this figure?
In your solution you would want to replace c = pred_LAB with c = pred_LAB[i]. But you do not have to use a for loop to plot the data. You can just use the following:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# generate random input data
val_lab = np.random.randint(0,10,(611,3))
pred_LAB = np.random.uniform(0,1, (611,))
# plot data
fig = plt.figure()
ax = plt.axes(projection = '3d')
ax.set_xlabel('L')
ax.set_ylabel('A')
ax.set_zlabel('B')
# create one 3D scatter plot - no for loop
ax.scatter3D(
val_lab[:,0],
val_lab[:,1],
val_lab[:,2],
s = 8,
marker='o',
c = pred_LAB,
cmap = 'rainbow',
label='my points'
)
# add legend
plt.legend()
plt.show()
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
sns.set(style="darkgrid")
df = pd.read_csv('Leap_Static_trials.csv')
Length = sns.swarmplot(x='name', y= 'length', data= df, color = 'green')
Width = sns.swarmplot(x='name', y= 'width', data= df, color = 'red')
plt.legend(labels=['Length','Width'])
plt.show()
From my dataset df I am plotting the length and width of the fingers taken from Leap Motion Controller. I am unable to change the legend to include the second color (red) which signifies the width.
Please find the attached figure as well. Your help is much appreciated. :)
Adding the parameter label= to a plot command usually creates the legend handles and labels automatically. In this case, seaborn creates handles for each column (so 5 of each). A trick is to create the legend with only the first and the last of the handles and the labels.
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
sns.set(style="darkgrid")
N = 100
# df = pd.read_csv('Leap_Static_trials.csv')
names = list('abcde')
ax = plt.gca()
df = pd.DataFrame({'name': np.random.choice(names, N),
'length': np.random.normal(50, 0.7, N),
'width': np.random.normal(20, 0.5, N)})
Length = sns.swarmplot(x='name', y='length', data=df, color='green', label='Length', order=names, ax=ax)
Width = sns.swarmplot(x='name', y='width', data=df, color='red', label='Width', ax=ax)
handles, labels = ax.get_legend_handles_labels()
plt.legend([handles[0], handles[-1]], [labels[0], labels[-1]])
plt.show()
I am trying to add tool tip to the graph, so whenever we hover around the graph it will give the info. How do i add one and make it an interactive one?
import matplotlib.pyplot as plt
import pandas as pd
import pandas as pd
from numpy import nan
from matplotlib import dates as mpl_dates
df = dataset
df["Date"] = pd.to_datetime(df["Date"]).dt.strftime('%m/%d/%Y')
#df["Date"] = pd.to_datetime(df["Date"]).apply(lambda x: x.strftime('%B-%Y'))
df.loc[df['Actuals'] == 0, ['Actuals']] = nan
df.loc[df['Actuals'] > 0, ['Predicted_Lower']] = nan
df.loc[df['Actuals'] > 0, ['Predicted_Upper']] = nan
# gca stands for 'get current axis'
ax = plt.gca()
y1 = df['Predicted_Lower']
y2 = df['Predicted_Upper']
x = df['Date']
ax.fill_between(x,y1, y2, facecolor="blue", alpha=0.7)
df.plot(kind='line',x='Date',y='Predicted', color='black', ax=ax)
df.plot(kind='line',x='Date',y='Actuals', color='green', ax=ax)
df.plot(kind='line',x='Date',y='Predicted_Lower',color='white',ax=ax)
df.plot(kind='line',x='Date',y='Predicted_Upper',color='white', ax=ax)
date_format = mpl_dates.DateFormatter('%Y-%m-%d')
plt.gca().xaxis.set_major_formatter(date_format)
locs, labels = plt.xticks()
plt.xticks(locs[::3], labels[::3], rotation=45)
plt.show()
plt.xticks(rotation=45)
plt.legend(['Predicted','Actuals'])
plt.xlabel('Date')
df.head(30)
plt.show()
using pandas, matplotlib, I am getting the data from sql server that is connected to Power BI and writing pyscripts to display graphs.
It's possible using matplotlib as discussed here.
However, you might want to look into other plotting packages such as plotly where it is builtin, default behavior.
import plotly.express as px
df = pd.DataFrame(np.arange(20), columns=['x'])
df['y'] = df['x']**2
px.line(df, x='x', y='y')
In your example, you could try something like
px.line(df, x='Date', y=Predicted, ...)
Given this heat map:
import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data)
How would I go about making the color bar values display in percent format?
Also, what if I just wanted to show the first and last values on the color bar?
Thanks in advance!
iterating on the solution of #mwaskom, without creating the colorbar yourself:
import numpy as np
import seaborn as sns
data = np.random.rand(8, 12)
ax = sns.heatmap(data, vmin=0, vmax=1)
cbar = ax.collections[0].colorbar
cbar.set_ticks([0, .2, .75, 1])
cbar.set_ticklabels(['low', '20%', '75%', '100%'])
Well, I had a similar problem and figured out how to properly set a formatter. Your example would become something like:
import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
uniform_data = 100 * uniform_data
sns.heatmap(uniform_data,
cbar_kws={'format': '%.0f%%'})
So, what you have to do is to pass an old-style string formatter to add percentages to colorbar labels. Not exactly what I would name self-evident, but works...
To show only the first and last, then you add vmax, vmin and an extra parameter to cbar_kws:
sns.heatmap(uniform_data,
cbar_kws={'format': '%.0f%%', 'ticks': [0, 100]},
vmax=100,
vmin=0)
You should get the colour bar object and then get the relevant axis object:
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter
fig, ax = plt.subplots()
sns.heatmap(df, ax=ax, cbar_kws={'label': 'My Label'})
cbar = ax.collections[0].colorbar
cbar.ax.yaxis.set_major_formatter(PercentFormatter(1, 0))
You need to be able to access the colorbar object. It might be buried in the figure object somewhere, but I couldn't find it, so the easy thing to do is just to make it yourself:
import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data, cbar=False, vmin=0, vmax=1)
cbar = ax.figure.colorbar(ax.collections[0])
cbar.set_ticks([0, 1])
cbar.set_ticklabels(["0%", "100%"])