This question already has answers here:
How can I set the aspect ratio in matplotlib?
(7 answers)
Closed last month.
I created a scatter plot which has equal min, max, steps for both x and y axis. However the result keeps making my graph look rectangle rather than a square. How can I fix this?
'''
ax = ctrl_ra.plot.scatter(x='Control', y='Infection(Ra)', color='black', alpha=0.5)
ax2 = df_sig.plot.scatter(ax=ax, x= 'Control', y='Infection(Ra)', color='red', alpha=0.5, s=5)
#Set the x-axis and y-axis scales
ax.set_xticks(np.arange(0,21,5))
ax.set_yticks(np.arange(0,21,5))
ax2.set_xticks(np.arange(0,21,5))
ax2.set_yticks(np.arange(0,21,5))
plt.show()
'''
This is what I got when I ran my code.
At first I thought it was the axis scale and/or size but it wasn't so I'm not sure where to go about to fix this problem.
Any help is appreciated. Thank you.
It is the figure size, not axes scale.
Try adding
plt.figure(figsize=(12, 12))
before plotting. Or, better, use
fig, ax = plt.subplots(figsize=(12, 12))
Related
This question already has an answer here:
How to show only the outline of a bar plot matplotlib
(1 answer)
Closed 2 years ago.
I'm trying to make a bar graph that only shows me upper contour and that within the bars is shown in white. I attach an image of what I want to obtain and what I have now.
photons=pd.read_csv("Average_weights_photons.txt", header=0, delim_whitespace=True)
xp=photons.R
yp=photons.total_weight
width=100
fig, ax = plt.subplots(figsize=(6,4))
ax.bar(xp,yp, width, alpha=0.4, color="black", align="center", edgecolor='none')
ax.set_yscale("log")
ax.set_ylabel("Number of particles")
ax.set_xlabel("Distance of the shower axis (m)")
ax.set_xlim(0, 4000)
plt.show()
I have this now
I want to obtain some like this.
What you need here is a step graph, and not a bar graph.
Replacing
ax.bar(xp,yp, width, alpha=0.4, color="black", align="center", edgecolor='none')
with
ax.step(xp,yp, 'k',where="mid",linewidth=1)
should help. But, I am unable to verify since I do not have the dataset you have, but this seems to be working with an example set I generated. You can find my example below:
import random
import matplotlib.pyplot as plt
yp=[random.randrange(i-4,i+4) for i in range(0,40)]
xp=[400-i for i in range(0,400,10)]
width=100
fig, ax = plt.subplots(figsize=(6,4))
ax.step(xp,yp, 'k',where="mid",linewidth=1)
ax.set_yscale("log")
ax.set_ylabel("Number of particles")
ax.set_xlabel("Distance of the shower axis (m)")
ax.set_xlim(0, 400)
plt.show()
I am attaching the example plot below.
just two quick questions I am struggeling with.
1. How can I scale the PercentFormatter, I want to set the y axis from 0 to 100 percent. I could do it without the PercentFormatter, but there must be a way including it, right?
2. The saveplot method cuts the x labels. How can i prevent that?
fig, ax = plt.subplots()
ax.bar(df['ErrorNames'], df["Frequency"], color="C0")
ax.set_xticklabels(df['ErrorNames'], rotation=90 ) ;
ax.set_ylabel('Error Frequency')
ax2 = ax.twinx()
ax2.set_ylabel('Accumulated Percentage of Frequency')
ax2.plot(df['ErrorNames'], df["cumpercentage"], color="C1", marker="D", ms=7)
ax2.yaxis.set_major_formatter(PercentFormatter())
ax.tick_params(axis="y", colors="C0")
ax2.tick_params(axis="y", colors="C1")
plt.savefig('Pareto')
plt.show()
Pareto Chart
I am trying to plot a histogram with my data.
Using python on Jupyter notebook
viz = cdf[['GyrNative', 'GyMutant', 'Hbond_native', 'HMutant', 'RMSDNative','RMSDMutant', 'RMSFNative', 'RMSFMutant', 'SASANative', 'SASAMutant']]
plt.figure(figsize = (15,10))
viz.hist(grid=True, rwidth = 0.9, color ='red')
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=0.1)
plt.show()
The plot generated are really tiny... How may I increase the size of each plot at once?
Following from the comments, if you just want to make the whole thing bigger, you should just add figsize to this and rearrange your plt. calls:
plt.tight_layout(pad=0.9, w_pad=0.5, h_pad=0.1)
viz.hist(grid=True, rwidth = 0.9, color ='red', figsize=(15,10))
plt.show()
I am using the autofmt_xdate() to get better looking x-axis (in date) like below:
fig, ax = plt.subplots(1,2, figsize=(12, 5))
ax[0].plot(my_df[['my_time']], my_df[['field_A']])
ax[0].set_xlable('time')
fig.autofmt_xdate()
This works fine. However, if I do two rows like below:
fig, ax = plt.subplots(2,2, figsize=(12, 5))
ax[0][0].plot(my_df[['my_time']], my_df[['field_A']])
ax[0][0].set_xlable('time')
fig.autofmt_xdate()
Then the labels and ticks of ax[0][0] x-axis disappeared. Any idea what I did wrong? Thanks!
You didn't do anything wrong here. What you see is the expected behaviour of fig.autofmt_xdate().
As the documentation says,
The ticklabels are often long, and it helps to rotate them on the bottom subplot and turn them off on other subplots, as well as turn off xlabels.
fig, ax = plt.subplots()
ax = fig.add_subplot(111)
ax.scatter(X[1],y)
y_projection = X.dot(theta_after)
ax.plot(X[1], y_projection)
plt.show()
Above is my code. What I'm trying to do is basically fitting a line to the data. I use gradient descent method to find the suitable theta.
The problem I came across is that the code above created two x-axis and y-axis and that they were overlapping on each other
This is the result generated from the above code. I'm not allowed to embed a pic now, please click on this to open the pic.
X - is a 97*2 matrix in which the first column is all 1.
You are creating an extra Axes with your second line. Just remove the following line:
ax = fig.add_subplot(111)
You already have an Axes when you run fig, ax = plt.subplots()